I have a counter that should be incrementing by 1 but yet it is incrementing by 2. I have the controller and view. It's a simple exercise but I'm not sure why it's incrementing by 2. Anyone have any ideas?
http://screencast.com/t/w9w7GndQK
CONTROLLER
function setCount($fileName = 'counter.txt') {
if (file_exists($fileName)) {
//read the value
$handle = fopen($fileName, 'r');
// increment it by one
$count = (int) fread($handle, 20) + 1;
// write the new value
$handle = fopen($fileName, 'w');
fwrite($handle, $count);
// close the file
fclose($handle);
} else {
// create the file
$handle = fopen($fileName, 'w+');
$count = 1;
// set a default value of 1
fwrite($handle, $count);
fclose($handle);
}
return $count;
}
$count = setCount();
require('index.tmpl.php');
VIEW:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Counter</title>
<link rel="stylesheet" href="#">
</head>
<body>
<p>You are the <?php echo $count; ?>visitor to this website.</p>
</body>
</html>