I followed this example i found online, but it is not working
http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/ co counter displays, it does create the counter.txt file i am testing t on localhost in chrome
herewith my code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login Form</title>
</head>
<body background="images/stockvault.jpg">
<b><ul>THIS IS A WORK IN PROGRESS</ul></b>
<br />
<br />
<img alt="hit counter" src="php/counter.php" />
<br />
<br />
</body>
</html>
then my php file:
<?php
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
// Change directory
chdir("../");
// Get current directory
//echo getcwd();
chdir("images");
$im = imagecreatefrompng("images.png");
$src1 = imagecreatefrompng("$chars[1].png");
$src2 = imagecreatefrompng("$chars[2].png");
$src3 = imagecreatefrompng("$chars[3].png");
$src4 = imagecreatefrompng("$chars[4].png");
$src5 = imagecreatefrompng("$chars[5].png");
imagecopymerge($im, $src1, 0, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src2, 60, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src3, 120, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src4, 180, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src5, 240, 0, 0, 0, 56, 75, 100);
header('Content-Type: image/png');
echo (imagepng($im));
imagedestroy($im);
?>
output is:
You forgot this part of the code in the beginning of your PHP code, at least in your copy&paste.
session_start();
$counter_name = "counter.txt";
You need the session_start() to handle $_SESSION variables, and asign the generated file counter.txt to $counter_name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.