Problem with script counter.php

Hi this is my first post i am new to php and counter would like to make friends on sitepoint and share tec ideas please,
I have downloaded the script counter.php and using danielle free otto numbers (thank you D ), i can’t get the counter to work my index.html just says hit counter in a box no picture. i have saved the following files on my web server in /srv/http
1.png
2.png
3.png
4.png
5.png
i have renamed 0.png to canvas.png (which i think maybe wrong
created counter.txt and typed 0 inside it
cut and pasted the text for counter.php from hibbard.eu
then pasted the html code into my index.

Please could someone help its 3AM and i can’t fix it please help me

1 Like

Hi there @taz1 welcome to the forums. It is not a good idea to ask a new question in someone else’s thread as it is likely to get overlooked. I have asked the mods to make this a new thread.

One thing though, as you are going to want to display a zero, I suspect renaming 0.png was not a good idea.

Where did you get your counter.php script from?

Topic split as requested.

####http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/

2 Likes

I am so sorry i messed up my first post. as john_betong state i got the counter from hibbard.eu. i have now put back canvas.png to 0.png so i don’t have a canvas.png. is the canvas.png just a blank png file. please accept my apoliges finding my way around the site. I do think this is a valuable site for learning many thank for starting to help me.

@taz1 - no need to apologise. Just that as I said your post might have got missed being tacked on the end of someone else’s.

Can you show us the code you have so far? You can put it in a post here if you put three tick marks before an three after your code. On most keyboards the tick mark is top left under the Esc key, although it’s not on some keyboards. (I hope this makes sense).

Have you got the first step to work - ie with just a text counter?

EDIT: I have just run through the tutorial myself. For the canvas.png you need to create this. The tutorial says:

In preparation for dynamically creating the final image, I have made one transparent png file (canvas.png) which will act as a canvas. This measures 296px by 75px.

Here you go, try this (it’s transparent, so you can’t see it, but it is there)

vvvvvvvvv

^^^^^^^^^^

1 Like

Thanks @Mittineague I managed to make one for my use but wasn’t sure how to make it available.

@taz1 just right click on the blank area in Mittineague’s post above and save as canvas.png

below is the text from counter.php i have used, no i have not managed to get counter.txt working as it still has a zero in it. i am just about to create a blank canvas.png using paint as i can’t see your i am googling how to create one, thanks for helping

<?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);
$im = imagecreatefrompng("canvas.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);
// Output and free from memory
header('Content-Type: image/png');
echo imagepng($im);
imagedestroy($im);
?>

Thank you i have done as you say i now have the canvas.png, its a black background when opened in mspaint. the counter is still not working any suggestions please

AFAIK Paint (and other image apps) don’t do transparent like a browser does.

Try it in a test HTML page and it should be OK

tried canvas.png in my index.html and it looks like a black rectangle which is fine as long as its transparent because the number files 1.png 2.png etc,etc are in gold.
but my hit counter is not working yet, i am starting to bang my head,:rage: i messed around with the counter.php i have gone back to the drawing board so i am using the exact php file as the one i posted, any ideas please

Most of the image editors I have worked with other than Paint do support transparency. Paint appears to be one of the few that doesn’t.

1 Like

thanks i have managed to do a transparent canvas.png in photo shop, i think my problem is PHP. the PHP browser page http://ipaddress/info.php used to open, but now it just says:-

<?php phpinfo(); ?>

I have got no idea why, because it used to work i have done a full system update, as my webserver is arch linux with a L.A.M.P install, its just one thing after another. i am not giving up by using a online counter as i what to learn please help

It does look as though PHP is not running.

What do you get if you point your browser to http://localhost ?

Hi Gandalf nice to her from you,as its arch linux command line linux distro i have to put the ip address of the linux web server and that works it bring up my webpage.

I assume the webpage is just HTML? No PHP? If you don’t have a localhost address, then do you have another way to access the LAMP control panel (assuming there is one. I’m using XAMPP on Windows).

yes just html, i am just starting my web page at the moment its just index.html, i have not seen a LAMP control panel. what i did i had an old PC so i install arch linux with LAMP, the install worked fine and everything was working ok the PHP was working when i opened from my windows pc
http://192.168.3.3/info.php the php webpage used to come up, but now it just show the text from info.php

managed to get php working via this link:-
https://wiki.archlinux.org/index.php/Apache_HTTP_Server

but the counter is still not working any ideas please