...

View Full Version : Saving current time in variable



Dezit
06-23-2005, 01:32 PM
This is probably a real newbie question, but I was wondering how one would set the value of the current time in a variable to compare with the new time after some interval has passed. The following code doesn't work but illustrates my reasoning more or less:

<?

$var = time() + 30;

if (time() < $var) {
echo "Not enough time has passed.";
exit;
}
else {
echo "You may try logging in again.";
}

?>

delinear
06-23-2005, 04:50 PM
If you're wanting to store the time and then check when the person last tried logging in then the best way is to use a session variable. If you assign a session variable when the person posts their login form, then if they try and post it again before X time has passed, generate an error message, eg, this would need some work to fit in with your login form but should give you the basic idea:

<?php
session_start();
if(isset($_POST['login'])) {

// let's assume we process the form here and the person isn't logged in

if(!isset($_SESSION['login_time'])) {
$_SESSION['login_time'] = time(); // if the time isn't stored (ie it's the first login attempt) then store it now
} else {
$limit = 30; // limit logins to once every 30 seconds

if((time() - $_SESSION['login_time']) < $limit) {
echo 'You can only make one login attempt every ' . $limit . ' seconds.';
} else {
// show your login form here
unset($_SESSION['login_time']); // let's unset the session now so that if the login fails again the process starts over
}
}
}
?>

emehrkay
06-23-2005, 09:23 PM
try
$now = time();
$future = time() + 30;

if(($future-$now)>30){
error
}else{
success
}

Kurashu
06-24-2005, 01:59 AM
That makes no sense. Using delinear's example would be better.

Dezit
06-24-2005, 07:04 AM
That seems to work, thanks!

emehrkay
06-24-2005, 03:04 PM
That seems to work, thanks!
which way worked for you, i personally used the one i posted

delinear
06-24-2005, 03:14 PM
which way worked for you, i personally used the one i posted
I'm not entirely sure what that's meant to do. Surely if you do:

$now = time();
$future = time() + 30;

Then $future - $now will almost always return exactly 30. Since both $now and $future will almost always have the same timestamp in that script it would be the same as saying (0+30) - 0.

The only time you won't return 30 is if the server has a problem and it takes over a second to process that bit of code, or if the script just happens to run as the time increments by one second, in which case it will always return 31.

emehrkay
06-24-2005, 03:19 PM
I'm not entirely sure what that's meant to do. Surely if you do:

$now = time();
$future = time() + 30;

Then $future - $now will almost always return exactly 30. Since both $now and $future will almost always have the same timestamp in that script it would be the same as saying (0+30) - 0.

The only time you won't return 30 is if the server has a problem and it takes over a second to process that bit of code, or if the script just happens to run as the time increments by one second, in which case it will always return 31.
lol yeah you're right. i used it with a timestamp that i put in the database, so thats why it works for me. yeah the old time would have to be something that has been set before.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum