Problem with script counter.php

I think this is progress!

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

any ideas do you think its the script?

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

Hmm. Certainly seems as though there are problems writing the file. What happens if you delete the counter.txt file - does the script create it?

1 Like

Maybe the “b” binary?

1 Like

yes i deleted and now its working thanks your the man! let try the rest of the php

1 Like

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.

I hope that hasn’t confused too much!

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):

$chars[0] = "0";
$chars[1] = "0";
$chars[2] = "1";
$chars[3] = "2";
$chars[4] = "3";

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?

below is the code i am using, i think i am going wrong at the png bit because i read somewhere the code does not need changing but i think it does.

`

<?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)) { $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); $im = imagecreatefrompng("canvas2.png"); $src1 = imagecreatefrompng("$chars[0].png"); $src2 = imagecreatefrompng("$chars[1].png"); $src3 = imagecreatefrompng("$chars[2].png"); $src4 = imagecreatefrompng("$chars[3].png"); $src5 = imagecreatefrompng("$chars[4].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); // Output and free from memory header('Content-Type: image/png'); echo imagepng($im); imagedestroy($im); ?>

`
i am sure it should say:-
$chars[0] = “0”;
$src1 = imagecreatefrompng(“0.png”); and so on, or have i got this wrong

I think you have got it wrong.

$chars holds the single digits from a five digit number. If you force $chars[0] to be’0’ then when the number goes over 09999 it would reset to 00000 instead of continuing to 10000.

1 Like

You’ve still got this code slightly wrong:

echo imagepng($im);

I believe it needs to read just

imagepng($im);
1 Like

yes i read that somewhere so i removed the echo,
unfortuatly its still not working i can’t get it to show any images, just says “hit counter” on my webpage. does anyone have this working on a live webpage.
below is the code i am using:-
`

<?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)) { $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); $im = imagecreatefrompng("canvas2.png"); $src1 = imagecreatefrompng("$chars[0].png"); $src2 = imagecreatefrompng("$chars[1].png"); $src3 = imagecreatefrompng("$chars[2].png"); $src4 = imagecreatefrompng("$chars[3].png"); $src5 = imagecreatefrompng("$chars[4].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); // Output and free from memory header('Content-Type: image/png'); imagepng($im); imagedestroy($im); ?>

`
still not understanding if my counter went to 88 how does the script know to use the image file 8.png if its not in the code??

I can’t obviously see anything wrong with your code. It looks exactly as in the tutorial. You have got your canvas.png and digit image files in the same directory as your script, haven’t you?

I tried to explain this but didn’t do very well, @droopsnoot did a better job in post #40

thanks gandalf, yes i have canvas and 0.png to 9.png in the same folder, if the code is ok thne its got to be something to do with the images i have resize them and the canvas is transparent, any ideas ?? i did change the code to canvas2.png because i had 2 canvas files.

Droopsnoot say i should have this in my code:-
$src4 = imagecreatefrompng("3.png");

but mine say:-
$src4 = imagecreatefrompng("$chars[3].png");

thats the bit i am not clear on

You have just indicated something that IS wrong with your code which I failed to spot. You have, I think, got the following:

$src1 = imagecreatefrompng("$chars[0].png");
$src2 = imagecreatefrompng("$chars[1].png");
$src3 = imagecreatefrompng("$chars[2].png");
$src4 = imagecreatefrompng("$chars[3].png");
$src5 = imagecreatefrompng("$chars[4].png");

but the code (which works!) in the tutorial is

$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"); 

I’m not sure why you changed the original, but using the exact code from the tutorial should do the job.

ok i have changed it, i thought i needed to have the zero first, any its still not working, do you think its a file permission problem

I’ve been playing with this for a little while

First, the $chars[] array key “number” is not the value of that array key.
(OK, that even confuses me and I just wrote it :wink:)

In other words, as an example, the “[1]” does not mean it’s the 1.png it means it’s the second member of the array (most likely one of the pad added zeroes at this point).

As for [0] it is true that the array starts with zero.

However, the preg_split returns an empty string - ‘’ - as the first and last array members.

If you are going to keep the preg_split, you will want to skip over the empty string and start off with [1].

My personal preference for something trivial is to use str_split, but that would require changing some code as no empty strings get returned


$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
//$chars = preg_split('//', $counterVal);
$chars = str_split($counterVal);
$im = imagecreatefrompng("canvas.png");  // 296 x 75
//$src1 = imagecreatefrompng($chars[1] . ".png");  // 56 x 75
//$src2 = imagecreatefrompng($chars[2] . ".png");
//$src3 = imagecreatefrompng($chars[3] . ".png");
//$src4 = imagecreatefrompng($chars[4] . ".png");
//$src5 = imagecreatefrompng($chars[5] . ".png");
$src1 = imagecreatefrompng($chars[0] . ".png");  // 56 x 75
$src2 = imagecreatefrompng($chars[1] . ".png");
$src3 = imagecreatefrompng($chars[2] . ".png");
$src4 = imagecreatefrompng($chars[3] . ".png");
$src5 = imagecreatefrompng($chars[4] . ".png");

*I also changed the variable - string bit. PHP parses variables inside double quotes, it’s just that I don’t like doing it that way. No offense intended @James_Hibbard

Given the problem you had when you created counter.txt rather than allowed the script to create it, perhaps the problem is the file ownership. If you check the ownership of counter.txt and change the ownership of the image files to the same…