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();