0

I have a php file that user can login and logout. I'm currently running on it. I want to show a number when user login.

for example user A login and redirect to userpage.php, I want to show a number like 1,2,3 and still count. I already created this one.

But the problem is when user log out and try to login again, the number is not continue? it become 1,2,3 again.

Is it possible to have a number still counting even when the user logout?

thanks

OperasiOps
  • 33
  • 7

2 Answers2

0

To achieve this result you cant try logging the login time and logout time and take difference between time and and display the number of seconds have passed since first login.

Step 1: Log the first login time and save it in database.

Step 2: Log the logout time and save it in database.

Step 3: if you want the time elapsed for first login and last logout then calculate the time difference in seconds. if you want the time elapsed form last login then calculate the time difference between last login and current time.

Step 4: Display the time elapsed on the HTML user page.

0

Update: as you want the numbers to get incremented in background irrespective of login/logout, use cron jobs see this for more info


You have to use counter for each user login which has to be persistent so, save this counter in DB whichever you are using for your application.

Flow would go like:

Check login > Login Succeeded > Fetch current login counter from db for this specific user > Increment by one > Save it back to DB

General implementation in php would be like (Considering username is always unique)

Note: Below code is not tested

    .
    .
    .
    $stmt = $con->prepare("SELECT user_id, username, password, login_count FROM users WHERE username=? AND password=? LIMIT 1");
    $stmt->bind_param('ss', $username, $password);
    $stmt->execute();
    $stmt->bind_result($user_id, $username, $password, $loginCount);
    $stmt->store_result();
    if($stmt->num_rows == 1)  //To check if the row exists
    {
        if($stmt->fetch()) //fetching the contents of the row
        {
               $_SESSION['Logged'] = 1;
               $_SESSION['user_id'] = $user_id;
               $_SESSION['username'] = $username;
               $loginCount = $loginCount+1; //increment login counter by 1 
                                            //this should be default as 0 when a new user register

               //Now save it back to DB using update query

               $Updatesql = "UPDATE Table SET loginCount=? WHERE username=?";
               $Updatestmt = $con->prepare($Updatesql);
               $Updatestmt->bind_param('ds', $loginCount,$username);
               $Updatestmt->execute();

               if ($Updatestmt->error) {
               //Update failed
                echo "FAILURE!!! " . $Updatestmt->error;
               }
               else echo "Updated {$Updatestmt->affected_rows} rows"; //Update succeeded
               $Updatestmt->close();

               exit();
       }

}
else {
    echo "INVALID USERNAME/PASSWORD Combination!";
}
$stmt->close();
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30