If it’s still not working in the same way (i.e. it’s displaying zero), add some debug echos into your code to see which of the file handler routines it’s going into. When you first create the counter, it will be zero because that’s what you write into the counter file. Is it creating the file correctly, and writing the counter into it?
Note that in your code, the $chars array will start at $chars[0] as arrays are base zero by default, so you need to use [0], [1], [2]. [3] and [4] to build the counter image.
Also note that in testing, it’s only going to increment the counter once, because you test for (and later set) a session variable so the same user only registers once. You could take out the bit where it sets the session, to see if that helps.
All I mean by that is to add echo statements so you can see each bit of code as it’s being executed. For example, in the start of your code:
<?php
session_start();
$debug = true; // Change this to false when you want to stop the extra echos
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
if ($debug) echo "Counter file did not exist"; // ** DEBUG
$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);
if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG
// 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);
if ($debug) echo "Incremented counter"; // ** DEBUG
}
...
So I’ve added the lines that end in “** DEBUG” as a comment, which will echo whichever ones work. Once you’ve figured out the problem, you can stop them outputting by changing the line at the top to set $debug to false instead of true.
Hey @taz1 I’m not sure where you are at now. I would make the following observations/suggestions:
The code on the website should work as is without any editing.
Make sure you read the tutorial as well as copying the code. I realise you are new to PHP but all the more reason to try to understand what the code is doing. That’s how we learn.
Try the text version first before going on to the graphical version - there’s less code and less to go wrong!
If you run into difficulties, add the debug statements as @droopsnoot suggests.
<?php
session_start();
$debug = true; // Change this to false when you want to stop the extra echos
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
if ($debug) echo "Counter file did not exist"; // ** DEBUG
$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);
if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG
// 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);
if ($debug) echo "Incremented counter"; // ** DEBUG
}
?>
`
I have created counter.txt with 0 typed inside, but its still not working, any suggestions please
That line would appear to be output by the debug line
if ($debug) echo "Counter value is " . $counterVal; // ** DEBUG
I suspect it has not been increased because the session variable is still set from the last attempt to run the script. A session can be closed by closing and restarting the browser, but as you’re not running from a browser I’m not sure how you close a session on your setup.
Perhaps you can try the graphical version now. I suggest saving your current counter.php as something else - say textcounter.php so you don’t lose what you have done so far.
Remember to read the tutorial as it mentioned resizing the images.
i am using a browser from my windows PC would that work
i closed my browser and deleted the history using cclean ran it again and the webpage says:-
Counter value is 0 Incremented counter
then when i hit refresh it says:-
Counter value is 0
No. I very much doubt it is the script. I followed the script myself a couple of days ago and it works fine for me.
I am a bit puzzled by the message you get when you run your web page saying it’s using an old P4 LAMP. This could be the problem, or it could be that you don’t have write permissions for the folder you’re working in.
the old P4 is just something i wrote to remind me. i have read write permission on the folder and file as shown below:-
-rwxrwxrwx 1 root root 2 Feb 8 16:33 counter.txt
and as you can see the file modified time, but the counter.txt still says zero inside
thank you both for your help i am just starting to understand the code, its weird all i had to do was delete counter.txt, i have moved on to the second part of the code, but still does not work, how do i test the second half can i put the debug lines in, and one other thing i don’t get i have resized all png files 0.png to 9.png but the code only uses 0 to 4 surly i have got to put the rest of the numbers in or is PHP that clever it adds them like 4.png + 1.png = 5.png
Probably by deleting counter.txt and allowing the script to create it, it would have a different owner.
I have just tried putting debug code in myself and it doesn’t work. Edit: Having played around with the script a bit more I have found that adding in any debug code will actually BREAK the script.
I’m not sure I can explain clearly but I’ll give it a go.
The line:
$chars = preg_split('//', $counterVal);
creates an array named $chars with each character in a different array element, so $char[1] is the first character, $char[2] the second character, etc. So “$chars[1].png” will be 0.png, 1,png … 9.png depending on the value of the 1st character.
Yes, because it has no output until it’s built the image and tries to send headers after it’s done so, that won’t go well if the debug echos are still in place. Although I confess I didn’t really notice that, the original problem was that the counter was always zero, so it was more to see where it was going through that bit. So the echo statements won’t be possible here.
As @Gandalf noted, you’re building an array when you get the counter value, which will look something like this, assuming your counter value is, say, 123 (because the str_pad line adds the two leading zeroes):
So when you load the graphics for each of those, you’re loading the corresponding pictures of each image. You only have five pictures loaded, but you need all ten digits to cover all values. So your code effectively says
$src4 = imagecreatefrompng("3.png");
for the example above.
Can you remove all the debug echos and show your complete current code? Also you’ve confirmed that you have canvas.png, and 0.png to 9.png in the correct format and an accessible location?