PHP Visitor Counter Code Problem
I'm having problems getting a PHP Page Visitor Counter working.
My website has multiple directories due to the large number of pages involved.
http://www.ronkskitchen.com is the Main directory where the main file is named "index.htm".
Underneath the main directory, there are 30+ sub-directories:
ronkskitchen.com/appetizers/index.htm
ronkskitchen.com/barbecues/index.htm
ronkskitchen.com/hungarian/index.htm
ronkskitchen.com/seafoods/index.htm
etc.
Each sub-directory has a variable number of unique pages attached.
In each of the unique index.htm files I have included the following PHP code in the HTML Code Head section under Web Properties (Page Tab):
<?php
session_start();
$counter_name = "hungarian.txt";
// Check to see if a text file actually exists.
//If not create one and initialize it to a zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value stored in our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has a previous visitor already 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);
?>
The "directoryname.txt" files do not exist now, but the code doesn't seem to create one and initialize it to zero. Either that, or it actually is creating the file somewhere in a directory / sub-directory located where I am unable to find it. All I really want are plain black numbers (0 thru 9) on a white background.
In each different index.htm file, I have also included the following PHP code in the HTML Code Body section under Web Properties (Page Tab) to display the visitor counter value:
<img alt="VISITS" src="hungarian.php" />
This code works, but doesn't display a value (I assume it's only because the hungarian.txt file doesn't exist yet).
This "image VISITS" is displayed centered at the very top of the index.htm page.
How can I move the display image to the bottom of that page?
Thanks
Ron
Re: PHP Visitor Counter Code Problem
Ron, do you have any PNGs with filenames 0.png through to 9.png anywhere?
You also should have a canvas.png file.
$counterVal will be a string "00000" incrementing up to "99999".
It is split into its digits and each digit.png is painted onto the canvas in a line.
I am guessing canvas.png is around 256px wide.
Remove the <img> Tag detail from the Page Body.
Create a small Placeholder box 256 x 64 px, add back the <img> Tag.
Position the placeholder at the bottom of the page.
You need to ensure the PHP and PNGs share the same folder as the index.htm file.
Acorn
Re: PHP Visitor Counter Code Problem
Re: PHP Visitor Counter Code Problem
Acorn
I created 10 "0.png" through "9.png" files each one is 50 x 75 pixels
I created a transparent "canvas.png" file 250 x 75 pixels
Uploaded those 11 files to:
www.ronkskitchen/hungarian/
(/hungarian has only 4 recipes, so it generates & publishes quickly)
I'm not sure how to ensure that the "PHP shares the same folder as the index.htm file" because I am inserting my PHP code string into the HTML CODE (head) for just the Page & not the Website.
The PHP code still doesn't create the missing "counter.txt" file.
WOW, I just changed my PHP head code to eliminate the "IF the file exists" statement simply did an fopen() to simply create hungarian.txt & then initialize its value to "159". It still won't create the txt file.
ALSO
When I create a text box & enter my display the counter value code <img alt="VISITS" src=hungarian.php / > in the Placeholder Tab Replace with HTML code (head) and check the regenerate automatically box nothing displays inside the text box .
Thanks
Ron
Re: PHP Visitor Counter Code Problem
Ron, your link was wrong.
The index page is http://www.ronkskitchen.com/hungarian/index.htm.
This page has no PHP.
It has no widget with the image calling the PHP.
The folder http://www.ronkskitchen.com/hungarian/ is showing the relevant PNG files:
http://www.ronkskitchen.com/hungarian/0.png
...
http://www.ronkskitchen.com/hungarian/9.png
&
http://www.ronkskitchen.com/hungarian/canvas.png
If you are sticking the PHP into the page, it probably won't work.
If it did, it would be put into the http://www.ronkskitchen.com/hungarian/index_htm_files/ folder.
The Placeholder <img alt="VISITS" src="hungarian.php" /> goes into its Body, not the head.
Its src would need to be aligned with where you have put the PHP script.
I have tried to find http://www.ronkskitchen.com/hungaria.../hungarian.txt or http://www.ronkskitchen.com/hungarian/hungarian.txt with no success.
if you have got the Placeholder on the page, with code in its Body, the PHP is not being called.
If you have put the PHP into the page then its path is not being set up properly.
Either being wrong will never create or increment hungarian.txt.
The PHP, images and nascent hungarian.txt should all be in the same folder, the level where http://www.ronkskitchen.com/hungarian/index.htm is.
Acorn