Hit counter is not working

<?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,"00001");
  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, "00001", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);

$im = imagecreatefrompng("counter.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);
?>

Take a look at the parameters for fopen()

http://php.net/manual/en/function.fopen.php

Personally i’d just replace it all with [FPHP]file_get_contents[/FPHP] and [FPHP]file_put_contents[/FPHP] myself…

2 Likes

I have a counter which uses fopen(), fwrite(), etc. It doesn’t work on my Windows/XAMPP test server but works fine on the live system. @StarLion 's right it’s easier with file_get_contents and file_put_contents.

problem in these lines I have a image counter.png its shows first time when page loaded but value is not increasing in image… :pensive: …please help…

$im = imagecreatefrompng("counter.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);

Is the value in the text file increasing?

yes value is increasing in text one time after refreshing page value remain same…if possible can u send the zip file with image… thank you

Pullo… value increasing in counter.txt file please check im waiting for your response :pensive:

Hi,

I just tested the code from your first post and it works ok for me.

Could this be because of the session? The counter was intended to count visitors as opposed to page views.

Other than that, could you provide steps as to how I can recreate your problem.

Pullo thanks for ur response…I’m recheck code… code is working…but when I use image to show hit counter… Code not working…if possible to email me a zip of all resources related to hit counter then please send…code and images please send…please help me to complete this task…as soon as possible…

Hi there,

I removed your email address. Posting it in a public forum is a great way to get spammed.

The thing is, it sounds like you are asking me to do all the work for you. This I won’t do. If you are stuck with one part of the project, I’m more than willing to help, but I won’t just email you everything.

Thanks for reply pullo…so help me to complete this task… … Can u tell me what’s is counter.png… I change the image but value in image isn’t change… It’s remains same… so Help me to create counter.png file…

If you don’t know what you’re code is doing, why not try a different approach? Find - or make - yourself a set of 10 images 0 - 9 and use code something like the following to print out one digit at a time:

$length = strlen($count);
for ( $i=0; $i<$length; $i++ ) {
  $digit = substr($count, $i, 1);
  $image = 'digits/' . $digit . '.gif';
  $size  = getimagesize($image);
  echo '<img src="', $image, '" alt="', $digit, '" ', $size[3], '>';
}

Just a thought…

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.