1

Trying to run a Query at the moment, i've managed to get it working with just adding the exact username but when I try and use the current query to use the $_Session identified username it dosn't work.

<?php 
    include ("config.php"); 
    session_start();    
    $username = $_SESSION['username'];
    $stmt = $db->exec ("UPDATE users SET lastlogindate = NOW() WHERE username = '$username'");
?>

EDIT - Login.php code

<?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;
            header("Location: main.php"); 
            die("Redirecting to: main.php");    
        } 
        else{ 
            print("Login Failed."); 
            $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
        } 
    }; 
?>  
3
  • 1
    Use prepared statements and not just that error might go away. Commented Nov 14, 2013 at 19:37
  • What "doesn't work"? What do you see if you echo "UPDATE users SET lastlogindate = NOW() WHERE username = '$username'";? Have you checked for any errors from the database? Commented Nov 14, 2013 at 19:39
  • 1
    What does "it doesn't work" mean? Do you set $_SESSION['username'] elsewhere? Can you echo or var_dump $username and/or $_SESSION['username'] to see that they are set prior to update attempt? Commented Nov 14, 2013 at 19:40

1 Answer 1

3

You set $row into $_SESSION['user'] in login.php and then fetch that by $_SESSION['username'] by mistake, you should use $_SESSION['user'] instead.

Try this :

<?php
    include ("config.php"); 
    session_start();    
    $username = $_SESSION['user'];
    $stmt = $db->prepare("UPDATE users SET lastlogindate = NOW() WHERE username = ?");
    $stmt->bindParam(1, $username['username']);
    $stmt->execute();
?>
Sign up to request clarification or add additional context in comments.

10 Comments

Dosen't add date to table unfortunately. Maybe $_SESSION username doesn't bring up the logged on users username.
Nothing mate...probably my issue :(
Changed username to user and got output of Array...now im confused
When I do print_r it shows Array ( [username] => [user] => Array ( [id] => 7 [username] => deason [email] => [email protected] ) )
No luck...its like I have the two USERNAME within the session array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.