Php – How to build a last login script using PHP

PHP

I was wondering how can I create a last login script using php.

Best Answer

For storing last access time, save the current time against logged in user in database at the end of the script.

For storing last login time, save the current time against verified user after identity confirmation is done.

Suppose you have a user table in database which has a last_login field, and $user array contains identity of the logged-in/verified user identity.

// assuming you are using PDO for database access
$stmt = $conn->prepare("UPDATE user SET last_login = ? where user_id = ?");
$stmt->execute(array($user['id']), strftime("%Y-%M-%D"));