Jump to content

TimeStamp Help


Go to solution Solved by Barand,

Recommended Posts

I have a table where a timestamp is automatically stored with the UserID if they fail to login correctly.  I want to track each failed login attempt and at what time it occurred.

 

I set $LastAttempt to their last login attempt and the format is simply this: 

2015-07-28 01:46:08

 

I also set $date to the current date/time in the same format as above.

 

If they have more than 5 login attempts, I want to set a penalty.  I have a switch that sets $penalty to the number of minutes before they can attempt to log in again.  So if their failed log in attempts exceed 5, then it penalizes them 10 minutes before they can even try again.

 

What I want is for that user to have to wait 5 minutes before they can log in again.  I just cannot figure out the best way to go about this and could use some direction.  I've tried several pieces of code, but because I'm not understand how it works, I'm probably not modifying it correctly.

 

I'm open to suggestions, but what I'm assuming I would do is this:

 

I know they have 5 failed log in attempts, so I would take the timestamp, add the penalty to it and if the result is greater than the current timestamp then they cannot log in yet.  Then I would like to post how much longer before they can make another attempt.  Does this sound right?  

Edited by TapeGun007
Link to comment
https://forums.phpfreaks.com/topic/297506-timestamp-help/
Share on other sites

Store each attempted login just as a date, additionally a 0/1 (fail/success)

Get that users count of timestamps up to 10 minutes ago...if is a fail last attempt and is a count of 5 or more then it keeps preventing them (store the timestamp and fail)...otherwise allows them (store the timestamp and success).

Edited by QuickOldCar
Link to comment
https://forums.phpfreaks.com/topic/297506-timestamp-help/#findComment-1517528
Share on other sites

I found a different way, but I'm betting this is a LONG way around the issue:

 

I basically just take a new var, $penaltytime and add that time to $LastAttempt.

 

$penaltytime = new DateTime($LastAttempt);
$penaltytime->add(new DateInterval($penalty));     
$penaltytime = $penaltytime->format('Y-m-d H:i:s');

 

Then I just compare if the $penaltytime is now greater than the current time, $date.

 

 

if ($penaltytime > $date){
            echo "<p>You have a penalty</p>";
}else{
            echo "<p>You have no penalty</p>";
        }

 

This actually worked.

 

However, I read several other articles about doing this in the mySQL mentioned by Barand, I would like to know exactly how to implement that.

 

I probably didn't write the best mySQLi here, but it does work.

 

            $sql="SELECT * FROM LoginAttempts WHERE LASalesID = '$ID' AND LACleared = 'no' ORDER BY LATimeStamp ASC";
            $rs=$con->query($sql);
            $rs->data_seek(0);
            
            // Get the total number of attempts
            $attempts = $rs->num_rows;
            
            // Cycle through all the login times and get the last login date/time.
            while($row = $rs->fetch_assoc()){
                $LastAttempt = $row['LATimeStamp'];
            }

 

How would I implement your method Barand?

 

The table is simply (LA = "last attempt")

LAID

LASalesID = The Sales Persons ID

LATimeStamp (The db auto generates this by default when a LASalesID is INSERTED)

LAFailed (This is so I can keep track of the number of times a log in has failed regardless of time/date, default is 'yes')

LACleared (If the log in failed and I reset this field to "yes" then they can log in again, default is 'no')

Link to comment
https://forums.phpfreaks.com/topic/297506-timestamp-help/#findComment-1517534
Share on other sites

  • Solution

this will give you a count of the attempts and the minutes since the last attempt

$sql = "SELECT 
          COUNT(*)
        , TIMESTAMPDIFF(MINUTE, MAX(LATimeStamp), NOW()) 
        FROM LoginAttempts 
        WHERE LASalesID = '$ID' 
            AND LACleared = 'no' ";
$rs = $con->query($sql);
if ($rs->num_rows > 0) {
    list($count, $mins_elapsed) = $rs->fetch_row();
}

Link to comment
https://forums.phpfreaks.com/topic/297506-timestamp-help/#findComment-1517536
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.