phpsession

How to update last activity based on user ID?


I use the following to update when a user logs in.. but this only works if they log in.. I have a remember me function and if checked user does not have to login.. and then it don't update..

is there a way to use the users session ID to update the last activity?

$stmt = $db->prepare("UPDATE users SET DateLastVisit = ?");
$stmt->bind_param('s', $Date);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

Solution

  • To accomplish this, you will need to match the activity with the user id as without this, you will end up updating every users record.

    Try this:

    if(!isset($_SESSION)){//check if we already have a session at the moment
      session_start();
    }
        $user_id = $_SESSION['id'];//change to your session id key
        $stmt = $db->prepare("UPDATE users SET DateLastVisit = ? WHERE id_user = ?");//change id_user to what you have in your database
        $stmt->bind_param('si', $Date, $user_id); // we bind the user_id as well
        $stmt->execute(); //execute, returns true/false
        $result = $stmt->get_result();
        $stmt->close();