Page 1 of 1

Time/Minutes in php

Posted: Fri Jan 08, 2010 5:59 pm
by synical21
I have done a few google searches to find my answer but the time topic goes way off what i need. I am trying to secure my user login by banning users for 15min after 5 login attempts. I thought this would be simple but i am getting stuck on times. Here is what i have so far:

Code: Select all

 
        }
        else
        {
        $msg = urlencode("Invalid Login. Please try again with correct user name and password. ");
        header("Location: login.php?msg=$msg");
        
        $updatefail = "UPDATE `users`
    SET failed_login_attempts = failed_login_attempts + 1 AND last_failed_login = NOW() WHERE user_name = '$user_email'";
 $result3 = mysql_query($updatefail)
     or die('Invalid query: ' . $updatefail . ' - Error is ' . mysql_error());
     
        }
        while ($line = mysql_fetch_assoc($result)) 
    { 
    foreach ($line as $key => $val) { $$key = htmlentities($val); }
    
        $failedloginattempts = "$failed_login_attempts";
        $lastfailedlogin = "$last_failed_login";
        if ($failedloginattempts > 5 && $lastfailedlogin > (NOW() - 10 minutes)) {  
        
     // now ban user for 15min?
        }
 
So on the last part the IF statement i am trying to say if the login attempt is over 5 and the last attempt was within 10minutes from now the user will get banned for 15min. I dont know how to do the "&& $lastfailedlogin > (NOW() - 10 minutes))". I also dont know how to ban a user for just 15 minutes i only know how to ban forever.

As i said i have googled this but not got very far. Any help would be great.

Re: Time/Minutes in php

Posted: Fri Jan 08, 2010 9:01 pm
by requinix
last_failed_login is (essentially) a string that looks like YYYY-MM-DD HH:MM:SS. You can create a string just like that with date() and time().

Code: Select all

date("Y-m-d H:i:s", time() - 600) // time() measures seconds and 10m = 600s
You can do a regular comparison between these two strings - there are very few times you can do that with dates and times: this is one of them.

Re: Time/Minutes in php

Posted: Fri Jan 08, 2010 9:13 pm
by synical21
Thank you very much