Jump to content

An internal countdown timer


ionicle

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ionicle
Link to comment
Share on other sites

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 by ionicle
Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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 by ionicle
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.