Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    913
    Thanks
    76
    Thanked 28 Times in 28 Posts

    user last login date

    Good evening,

    I want to be able to display the date at which a user last logged into the site. I have created a table to store the details of course and i've decided to add a field at the end called last_login.

    My intentions are after verifying the details are correct, to then update their last_login field with a timestamp, then display the profile in my usual way. My first thoughts are that this would require another query which I don't want to do. I have posted here to see if anyone can advise me on my code and see what the best way would be to update that field.

    Does it require another query?

    Here is my code:
    login_process.php
    PHP Code:
    if($errors == "")
        {
            
    //check what the values were
            
    require("queries/login_check.php");
            
            if(
    $nrow == 1)
            {
                
    //successful, set session
                
    $_SESSION['user_name'] = $username
                
    header("Location: ?page=home");
                die();
            }
            else 
            {
                
    $errors .= "You did not enter the correct username and/or password.<br />";    
            } 
    login_check.php (query file)
    PHP Code:
    $conn = new mysqli("localhost","root","","****") or die("Error creating connection.");
    $stmt $conn->prepare("SELECT username FROM users WHERE username=? && password=?") or die("Error: ".mysqli_error($conn));

    $stmt->bind_param("ss",$_POST['login_user'], $_POST['login_pass']);
    $stmt->execute();
    $stmt->bind_result($username);
    $stmt->store_result();

    $nrow $stmt->num_rows;
    $stmt $stmt->fetch();

    mysqli_close($conn); 
    Thank you for your time and expertise.

    Kind regards,

    LC.

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    135
    Thanks
    3
    Thanked 19 Times in 19 Posts
    Yes it would require another query, we do get fixated on keeping them to a minimum, however 2 queries wont break the bank! You have three things you are trying to accomplish

    1) Login client (checking credentials)
    2) Display clients last login time
    3) Update clients last login time

    If you did it the way you are you would in fact need 3 queries. However if you also get the last login time from the column that stores it (join might be necessary, dont know your table layouts) during login you reduce that down to 2 querys.

    1) Login client (checking credentials) & get last login time
    2) Update login

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you intend to perform an update, your only option is to issue an update query.

    Since you are using sessions, your only option is to update with either the current time, or the maximum anticipated time they will be around, which is the current time plus your timeout. If they log out explicitly, you can also set it then. The only alternative is to disable garbage collection, move the sessions to your own local directories, then iterate and parse every file older than your timeout and extract information out of them to update with prior to unlinking them. Note if you do this, despite how similar it looks to it, sessions are *not* encoded with serialize. You will need to write a parser for it, or invoke the session_decode whilst juggling faux sessions. And no that isn't fun.

    If you were using database constructed and controlled, it would be a bit of a different story. Still need to issue the updates though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •