ionicle Posted January 26, 2015 Share Posted January 26, 2015 Hey guys and gals! I am currently working on implementing the following functionality in one of my pages: Whenever a person with a specific IP address visits the page, an internal countdown timer of 2 hours should be started. Until that timer is active, the only response from the page ANYONE can get would be a predefined echo value. Once the timer has run out, the normal script execution of the rest of the page should be restored. Any pointers and tips on how to approach that would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2015 Share Posted January 26, 2015 You don't an actual timer - just keep track of when they visited the first time. 1. When they visit the page, check in your database when they first visited. 2. If they have visited at all, check whether that time was within the last two hours or not: show the predefined value if so, continue the script if not. 3. If they haven't, record the current time and show them the predefined value. Quote Link to comment Share on other sites More sharing options...
ionicle Posted January 26, 2015 Author Share Posted January 26, 2015 (edited) I'm not sure if I got that correctly, but let me clarify this: When a specific IP visits the page, it should start showing that predefined echo value for EVERYONE in the next 2 hours. Not just for the person from that specific IP address. And I am not using a database right now - how do I implement that? Can't I just use a plain .txt file to record the time when that IP visits the page, and just use that value to determine how long it's got left until 2 hours have expired, using time()? And when the 2 hours have expired, auto-erase the entry from the .txt file and start over? Edited January 26, 2015 by ionicle Quote Link to comment Share on other sites More sharing options...
ionicle Posted January 26, 2015 Author Share Posted January 26, 2015 (edited) Figured my plan of action would be the following: 0. declare $current_time and $recorded_time 1. open db file 2. read value in first line of db file 3. determine whether the value is a valid time value 4. if not, continue with the rest of the script 5. if yes, do the following: 5a. calculate difference between $current_time and $recorded_time 5b. if difference is larger than 2 hours, erase contents of db file and continue with the script 5c. if difference is smaller than 2 hours, echo out "404" and die() 6. check whether visitor's IP belongs to the predefined range. if it does, do the following: 6a. erase contents of db file 6b. create a new entry for $recorded_time 6c. echo out "404" and die() How do I go about step 3, specifically? Anyone? Edited January 26, 2015 by ionicle Quote Link to comment Share on other sites More sharing options...
requinix Posted January 26, 2015 Share Posted January 26, 2015 If all you need is the timestamp then use the file modification time. file_exists() to check that it even exists in the first place, filemtime() to get the modification time, touch() to set it. Don't have to ever delete the file, just check that it's been less than two hours since it was "modified". Quote Link to comment Share on other sites More sharing options...
ionicle Posted January 26, 2015 Author Share Posted January 26, 2015 (edited) Here's what I came up with ( it's incomplete, but it's almost there ): The DB file actually does contain a Unix timestamp right now, and it's smaller than the current one. For some reason though, all I'm getting is a blank page. Why is that? I am not quite sure of the timestampState function - what I am basically doing there, is checking if the $recorded_time variable is an integer. <? $current_time = time(); $recorded_time = recordedTime(); $time_difference = $current_time - $recorded_time; $timestamp_state = timestampState($recorded_time); $threshold = "7200"; function recordedTime() { $db_file = 'db_file.txt'; $handle_check = fopen($db_file, "r"); $contents_check = fread($handle_check, filesize($db_file)); fclose($handle_check); return $contents_check; } function timestampState($recorded_time) { if($recorded_time === (int)$recorded_time) { return true; } else return false; } if ($timestamp_state == 'true') { if ($time_difference > $threshold) { echo "Time to erase this value and reset things."; } else { echo "Time to ban everyone."; die(); } } ?> Edited January 26, 2015 by ionicle Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 26, 2015 Share Posted January 26, 2015 I would use requinix' suggestion of just creating a blank file, and just use filemtime() to determine when the file was created. No need to store the timestamp as the filesystem does that automatically when a file is created/changed. It's also easier to implement and shorter code as well. Quote Link to comment 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.