Why my php4 graphic counter with cookie stop working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elim
Forum Newbie
Posts: 8
Joined: Tue Jun 24, 2008 10:19 am

Why my php4 graphic counter with cookie stop working?

Post by elim »

I used to use my counter.php in static html pages like this:
<img src=/myCounter/counter.php>
It works just fine but after upgrade to php5, the script below stop reading cookie and so
increases count everytime I refresh the page.

Could anyone give me some hit what's wrong?


The code is here:

Code: Select all

 
<?php 
/******************************************************** 
    Variables 
*********************************************************/ 
 
$digit_dir = "./digits"; 
$file = "counter.txt"; 
$min_width = 5; 
$lifetime = 30; 
$domain = "www.offsite.be"; 
 
$use_mail = 1; 
$trigger = 1000; 
$your_email = "admin <at> offsite <dot> be"; 
 
 
/******************************************************** 
    Code --> DO NOT EDIT BELOW THIS POINT 
*********************************************************/ 
 
$fp = fopen($file,  "r") or die("Failed to open counter-file"); 
$size = filesize($file); 
$count = fread($fp,  $size); 
fclose($fp); 
 
if(!Isset($_COOKIE['counter'])){ 
  $fp = fopen($file,  "w"); 
    $count++; 
    fwrite($fp,  $count); 
    fclose($fp); 
    setcookie("counter", "dummy", time()+$lifetime,"/",$domain); 
   
  if($use_mail AND ($count%$trigger==0)){ 
    $headers = "From: Counter <noreply@$domain>\n"; 
    $headers .= "X-Sender: <noreply@$domain>\n"; 
    $headers .= "X-Mailer: Offsite Counterscript\n"; 
    $headers .= "Return-Path: <noreply@$domain>\n"; 
    $subject = "Counter information from $domain"; 
    $message = "Congratulations!\n\nThe number of visitors on your site has reached $count."; 
    mail($email, $subject, $message, $headers); 
  } 
} 
 
$len = strlen(strval($count)); 
if($len > $min_width) $width = $len; 
else $width = $min_width; 
 
if(!file_exists("$digit_dir/0.png")){ 
  die("No images in digit-dir"); 
} 
 
$d0 = ImageCreateFrompng("$digit_dir/0.png"); 
$dx = ImageSX($d0); 
$dy = ImageSY($d0); 
 
$img = ImageCreateTrueColor($width*$dx,  $dy); 
ImageDestroy($d0); 
 
$xoff = $width*$dx; 
while($xoff > 0) { 
  $digit = $count % 10; 
  $count = $count / 10; 
  $temp = ImageCreateFrompng("$digit_dir/$digit.png"); 
  $xoff = $xoff - $dx; 
  ImageCopyResized($img,  $temp,  $xoff,  0,  0,  0,  $dx,  $dy,  $dx,  $dy); 
  ImageDestroy($temp); 
    } 
 
Header("Content-type: image/png"); 
Imagepng($img); 
ImageDestroy($img); 
?>
Last edited by Benjamin on Sat Jun 06, 2009 1:24 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Why my php4 graphic counter with cookie stop working?

Post by Benjamin »

:arrow: Moved to PHP - Code
elim
Forum Newbie
Posts: 8
Joined: Tue Jun 24, 2008 10:19 am

Re: Why my php4 graphic counter with cookie stop working?

Post by elim »

actually i found the solution: SetCookie() should specify the path carefully
Post Reply