简体   繁体   中英

Capture user last login date

How can I log a users last login date? I've tried using $query = "UPDATE users SET lastlogindate = NOW() WHERE username = "username" to test it works but nothing unfortunately. I want to obviously ensure I log the date for the authenticated user correctly also.

Where should I put it in this script?

<?php 
    require("config.php"); 
    $submitted_username = ''; 
    if(!empty($_POST)){ 
        $query = " 
            SELECT 
                id, 
                username, 
                password, 
                salt, 
                email 
            FROM users 
            WHERE 
                username = :username 
        "; 
        $query_params = array( 
            ':username' => $_POST['username'] 
        ); 

        try{ 
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
        catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
        $login_ok = false; 
        $row = $stmt->fetch(); 
        if($row){ 
            $check_password = hash('sha256', $_POST['password'] . $row['salt']); 
            for($round = 0; $round < 65536; $round++){
                $check_password = hash('sha256', $check_password . $row['salt']);
            } 
            if($check_password === $row['password']){
                $login_ok = true;
            } 
        } 

        if($login_ok){ 
            unset($row['salt']); 
            unset($row['password']); 
            $_SESSION['user'] = $row;
            $query = "UPDATE users SET lastlogindate = NOW() WHERE username = "deason";
            header("Location: main.php"); 
            die("Redirecting to: main.php"); 
        } 
        else{ 
            print("Login Failed."); 
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        } 
    } 
?>  

The syntax highlighting gives it away: it's a quoting issue. You're using double quotes for your string and your string value in your query.

$query = "UPDATE users SET lastlogindate = NOW() WHERE username = "deason";

should be:

$query = "UPDATE users SET lastlogindate = NOW() WHERE username = 'deason'";

通常,您必须添加引号:

 $query = "UPDATE `users` SET `lastlogindate` = NOW() WHERE `username` = 'deason'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM