Problem with script counter.php

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

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.

i changed the $chars[0] to start at zero. i am new to php i dont understand debug echos not sure whats going wrong

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.

2 Likes

Hey @taz1 I’m not sure where you are at now. I would make the following observations/suggestions:

  1. The code on the website should work as is without any editing.

  2. 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.

  3. Try the text version first before going on to the graphical version - there’s less code and less to go wrong!

  4. If you run into difficulties, add the debug statements as @droopsnoot suggests.

Enjoy and good luck. Let us know how you get on…

1 Like

i have added the debug not sure how to test the script as i am using arch linux command line i will test it on some online php test page. thanks

thanks guys i will go back to the text version first and let you know

this is what my webpage displays

this is the code in my webpage:-
`

Number of visitors to this page so far:
Hit counter `

and this is the counter.php:-
`

<?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

The code in your web page

<img alt="Hit counter" src="counter.php" />

is running the counter.php as though it was the graphical version, so that’s probably why you’re not seeing anything.

Can you simply run the counter.php instead of your web page?