I have been trying to figure out how to implement the example code for the exercise that displays how many times a page has been viewed using functions. I have typed the code as shown but when I view the page it always just says I have viewed the page 1 time(s). I think somehow I need to initialize a counter for this to work. Below I have included the code for you to look at. I am enjoying the book so far and would greatly appreciate any help.
the code I have typed is as follows:
Code:
<?php
function display_times($num) {
echo '<h1>You have viewed this page ' . $num . ' time(s).</h1>';
}
//get the cookie value and add 1 to it for this visit
$num_times = 1;
if (isset($_COOKIE['num_times'])) {
$num_times = $_COOKIE['num_times'] + 1;
}
//set the value back to the cookie for the next time
setcookie('num_times', '&num_times', time() + 60);
?>
<html>
<head>
<title>Viewed Times</title>
</head>
<body>
<?php
display_times($num_times);
?>
</body>
</html>
thanks again