Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Simple PHP counter not working

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Simple PHP counter not working

Postby epicunion » Thu Jul 17, 2003 10:37 pm

I admit I am very new to PHP. There are some things I understand and others (cookies, sessions, ect) that seem easy but I can't accomplish for some reason. I have a simple code to check if a cookie is present, if it is then the hit counter reads from a file only; if it is not present then the file is incremented by one.

It does not seems to work however (I have it set up at nikzigallery.com). It keeps incrementing when you refresh the page. I also tried detecting a session and that did not work either. Is there something I am missing? Do I need to configure the php.ini file?

One other thing. The cookie takes a while to show in my cookies folder on my local machine; I though it would show up right when I load the page. Is there a reason for the delay? Thanks for all your help. -Steve

Code: Select all
<?php
if($COOKIE['theUser'] != 'unique visitor'){
   setcookie('theUser','unique visitor', 99999999);
   
   //READS COUNTER FILE
   $counterFile = fopen("hits.txt", "r");
   $theHits = (int)fgets($counterFile, 20) + 1;
   
   //WRITES COUNTER FILE
   $counterWrite = fopen("hits.txt", "w+");
   fwrite($counterWrite, $theHits);
   fclose($counterFile);
   }
else{
   //READS COUNTER FILE
   $counterFile = fopen("hits.txt", "r");
   $theHits = (int)fgets($counterFile, 20);
   }
?>
epicunion
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Jul 17, 2003 10:24 pm
Location: Portland, OR

Postby swirlee » Fri Jul 18, 2003 5:12 am

The problem would appear to be that you're using $COOKIE instead of $_COOKIE.

As far as the cookie file not showing up, this could be for a number of reasons. It may be that the browser keeps the cookie in memory for awhile before saving it to the hard drive. Or maybe you just didn't refresh the directory listing? Either way, I doubt you need to worry about it.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Thanks for the help...

Postby epicunion » Fri Jul 18, 2003 3:35 pm

...I finally got the sessions to work. I see the cookie mistake as well.
epicunion
New php-forum User
New php-forum User
 
Posts: 3
Joined: Thu Jul 17, 2003 10:24 pm
Location: Portland, OR

Postby bezmond » Sat Jul 19, 2003 12:13 am

your other cookie mistake was:

setcookie('theUser','unique visitor', time() + 99999999);

Andrew
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron