Jump to content

Cookie Code


Recommended Posts

Hello All


    I wrote a tiny piece of cookie code (to set and record cookies and hits) that works pretty well but seems to have a bug in it that I cannot figure out. First, here is the code:


$my_cookie = (isset($_COOKIE['my_cookie'])) ? true : false;

$countFile = "./data/count.txt";
$cookieFile = "./data/cookie.txt";

$dateTime = date('Y-m-d\,H:i:s\,U\,T\,O\G\M\T');
$remoteIPAddress .= $_SERVER['REMOTE_ADDR'];
$remoteUserAgent .= $_SERVER['HTTP_USER_AGENT'];

$cookieVal = $dateTime;
$cookieVal .= ",";
$cookieVal .= $remoteIPAddress;
$cookieVal .= ",";
$cookieVal .= $remoteUserAgent;
$cookieVal .= "\r\n";

    	// read the visit counter
    	$countHandle = fopen($countFile, 'r');
    	$visitCount = intval(trim(@fread($countHandle, filesize($countFile))));

    	// incremenet the visit counter

// write the new visit count and cookie value to their respective files
    	$countHandle = fopen($countFile, 'w');
    	$cookieHandle = fopen($cookieFile, "a+b");

    		fwrite($countHandle, $visitCount);
    		fwrite($cookieHandle, $cookieVal);


    	setcookie("my_cookie", $cookieVal, time()+31556926);
    	// read the visit counter
    	$countHandle = fopen($countFile, 'r');
    	$visitCount = fread($countHandle, filesize($countFile));


What is happening is that sometimes, not every time, but, sometimes my cookie.txt count gets a 0 (zero) written back to it and I can't figure out why. Can anybody see anything in there that would have this effect? Like I said, it only gets zeroed out from time to time.


Thanks to all for taking the time to read.  :)

Link to comment
Share on other sites

This problem usually occurs when you have concurrent visitors. The file is locked by the operating system for the visitor that got their first and when your code attempts to access the file for the second visitor it cannot read the file (you would be getting errors if your code had error checking logic in it. This results in the $visitCount being a null, which your code should be incrementing to a one, which gets written back to the file by the code running for the second visitor.


You need both some error checking logic in your code to make sure the file operations are occurring without error before blindly writing a wrong value back to the file and you would need to use and test file locking status to insure that the count is not lost.


You can avoid this kind of problem by using a database instead of a file.

Link to comment
Share on other sites

PFMaBiSmAd ... thanks for reading and replying. I will look into adding some error checking to the site to correct for this. Although, I am suspicious as the site gets, maybe, one or two visitors a day, and mostly from bots. The reason I don't use a db is because this is a "play" site and I am not sure if I will even keep it after my years registration is up.


Thanks again.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.