NoSalt Posted September 26, 2010 Share Posted September 26, 2010 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: <?php $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"; if(!$my_cookie){ // read the visit counter $countHandle = fopen($countFile, 'r'); $visitCount = intval(trim(@fread($countHandle, filesize($countFile)))); fclose($countHandle); // incremenet the visit counter $visitCount++; // write the new visit count and cookie value to their respective files $countHandle = fopen($countFile, 'w'); $cookieHandle = fopen($cookieFile, "a+b"); if(!preg_match('/bot/',$remoteUserAgent)){ fwrite($countHandle, $visitCount); fwrite($cookieHandle, $cookieVal); } fclose($countHandle); fclose($cookieHandle); setcookie("my_cookie", $cookieVal, time()+31556926); } else{ // read the visit counter $countHandle = fopen($countFile, 'r'); $visitCount = fread($countHandle, filesize($countFile)); fclose($countHandle); } ?> 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. Quote Link to comment https://forums.phpfreaks.com/topic/214411-cookie-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 26, 2010 Share Posted September 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214411-cookie-code/#findComment-1115794 Share on other sites More sharing options...
NoSalt Posted September 26, 2010 Author Share Posted September 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214411-cookie-code/#findComment-1115795 Share on other sites More sharing options...
PFMaBiSmAd Posted September 26, 2010 Share Posted September 26, 2010 Some browsers request a page twice (though I don't know if this would occur close enough in time to cause the problem) and some URL rewriting will also cause a page to be requested twice, so it could be that the two concurrent requests are actually due to a single visitor. Quote Link to comment https://forums.phpfreaks.com/topic/214411-cookie-code/#findComment-1115802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.