phpQuestioner Posted January 7, 2008 Share Posted January 7, 2008 Is there a way some one; if they had no idea what the text file name was; could alter a text file? I have been using a php text file based hit counter and the text file will have like "2250" in it and then just all of a sudden; maybe a few days/weeks/months later; the text file will have like a lot smaller number. Here is the code I am using: <?php $fp = fopen ('1a3c5e.txt', 'r'); $now = fgets ($fp); fclose ($fp); if (empty ($_COOKIE['visit1a3c5e'])) { $fp = fopen ('1a3c5e.txt', 'w'); $now = $now + 1; fwrite ($fp, $now); setcookie ('visit1a3c5e', 1, time()+315360000); fclose($fp); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/ Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 Is there a way some one; if they had no idea what the text file name was; could alter a text file? ^ Is that your question? If so, then I doubt it. Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432275 Share on other sites More sharing options...
phpQuestioner Posted January 7, 2008 Author Share Posted January 7, 2008 Is there a way some one; if they had no idea what the text file name was; could alter a text file? ^ Is that your question? If so, then I doubt it. well how is it being altered then? Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432277 Share on other sites More sharing options...
redarrow Posted January 7, 2008 Share Posted January 7, 2008 it probly the hits ur getting is to much for a textfile to handle try using mysql it free........ ignore this statement was stupid, i thort flat files didnt have the abilty to work or act as fast as a real database and load server time was at fault sorry....... Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432280 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2008 Share Posted January 7, 2008 Your code is not bothering to check the status returned by the fopen() statement and when there are concurrent visitors, the file is locked by one and cannot be opened by another. This results in $now being a null value and you add 1 to it, causing your counter to start over. You then blindly write this incorrect value back to your file. You need to check that the fopen() worked and only proceed with writing to the file when the read was successful. An integer can hold 2,147,483,647. If you site was getting that many hits, you would know it. This has nothing to do with what a text file can handle. Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432284 Share on other sites More sharing options...
phpQuestioner Posted January 7, 2008 Author Share Posted January 7, 2008 Your code is not bothering to check the status returned by the fopen() statement and when there are concurrent visitors, the file is locked by one and cannot be opened by another. This results in $now being a null value and you add 1 to it, causing your counter to start over. You then blindly write this incorrect value back to your file. You need to check that the fopen() worked and only proceed with writing to the file when the read was successful. An integer can hold 2,147,483,647. If you site was getting that many hits, you would know it. This has nothing to do with what a text file can handle. that kind of does make sense; I am going to try this instead: <?php $fp = file_get_contents("1a3c5e.txt"); $now = $fp + 1; if (empty ($_COOKIE['visit1a3c5e'])) { $fp = fopen ('1a3c5e.txt', 'w'); fwrite ($fp, $now); setcookie ('visit1a3c5e', 1, time()+315360000); fclose($fp); } ?> it probly the hits ur getting is to much for a textfile to handle try using mysql it free........ that may be what I have to do; if this does not work Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432289 Share on other sites More sharing options...
redarrow Posted January 7, 2008 Share Posted January 7, 2008 quick go lol probly wrong theo <?php $file = 'hits.dat'; // File where data is stored $fp = fopen($file, "r"); // Open file as read only $count = fread ($fp, filesize($file)); // Read hits fclose($fp); // Close file $count++; // Add 1 to the count $fp = fopen($file, "w"); // Open file for writing fwrite($fp, $count); // Write the count fclose($fp); // Close file echo "Load $count times"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432290 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2008 Share Posted January 7, 2008 file_get_contents() returns a FALSE if it fails to read the file, resulting in the same problem. From the file_get_contents in the php manual - On failure, file_get_contents() will return FALSE. Your code must check that you actually read the value before blindly incrementing it and writing it back out. Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432294 Share on other sites More sharing options...
phpQuestioner Posted January 7, 2008 Author Share Posted January 7, 2008 redarrow, I am going to try my text file version first and if that does not work; I will try your version - thanks. thank you to all of you for your input. file_get_contents() returns a FALSE if it fails to read the file, resulting in the same problem. From the file_get_contents in the php manual - On failure, file_get_contents() will return FALSE. Your code must check that you actually read the value before blindly incrementing it and writing it back out. if the file exist and has a value; why would it return false? I guess I could use file_exists() to make sure the file is there and I could use a if condition to check and make sure $fp variable (the file_get_contents variable) value is not null. Quote Link to comment https://forums.phpfreaks.com/topic/84796-issue-with-text-file-base-hit-counter/#findComment-432295 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.