Results 1 to 6 of 6

Thread: [RESOLVED] keep track of last login date somehow?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Resolved [RESOLVED] keep track of last login date somehow?

    Hello, I have an index page where users enter in username and passwords to get into their account page (called account.php). If login fails, the user is redirected back to index.php

    I use php and mysql.

    I would like to add a record called "lastlogin" to my "members" table that will be updated with the current date every time someone logs on using the login form on my index page.

    How can I achieve this?

    Here is my php script:

    Code:
    <?php require_once(connect.php); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    ?>
    <?php
    // *** Validate request to login to this site.
    if (!isset($_SESSION)) {
      session_start();
    }
    
    $loginFormAction = $_SERVER['PHP_SELF'];
    if (isset($_GET['accesscheck'])) {
      $_SESSION['PrevUrl'] = $_GET['accesscheck'];
    }
    
    if (isset($_POST['username'])) {
      $loginUsername=$_POST['username'];
      $password=$_POST['password'];
      $MM_fldUserAuthorization = "";
      $MM_redirectLoginSuccess = "account.php";
      $MM_redirectLoginFailed = "index.php";
      $MM_redirecttoReferrer = false;
      mysql_select_db($database_connectDB, $connectDB);
      
      $LoginRS__query=sprintf("SELECT username, password FROM members WHERE username=%s AND password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
       
      $LoginRS = mysql_query($LoginRS__query, $connectDB) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        
        //declare two session variables and assign them
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
    
        if (isset($_SESSION['PrevUrl']) && false) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
        }
        header("Location: " . $MM_redirectLoginSuccess );
      }
      else {
        header("Location: ". $MM_redirectLoginFailed );
      }
    }
    ?>
    Here is my form action:

    Code:
     <form ACTION="<?php echo $loginFormAction; ?>" id="username" name="form1" method="POST">
                <label for="username">Login Username:</label>
                <input type="text" name="username" id="username" />
                <label for="password"><br />
                  <br />
                  Login Password:</label>
                <input type="password" name="password" id="password" />
                <input type="submit" name="button" id="button" value="Submit" />          
    
              </form>
    Thank you.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: keep track of last login date somehow?

    just this?
    PHP Code:
    mysql_query("UPDATE members SET lastlogin='" time() . "' WHERE username='{$username}' LIMIT 1"); 

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: keep track of last login date somehow?

    Thanks... I was working on a line like that, but couldn't figure out where to place it successfully. I'm a beginner... learning fast.

    Thank you.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: keep track of last login date somehow?

    Sorry I've been working on this yet and cannot seem to figure out where to place it in my code. I tried placing it liked this:

    Code:
     $LoginRS__query=sprintf("SELECT username, password FROM members WHERE username=&#37;s AND password=%s",
        GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
       
      $LoginRS = mysql_query($LoginRS__query, $connectDB) or die(mysql_error());
      $loginFoundUser = mysql_num_rows($LoginRS);
      if ($loginFoundUser) {
         $loginStrGroup = "";
        
        //declare two session variables and assign them
        $_SESSION['MM_Username'] = $loginUsername;
        $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
    
        if (isset($_SESSION['PrevUrl']) && false) {
          $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
        }
        header("Location: " . $MM_redirectLoginSuccess );
    mysql_query("UPDATE members SET lastlogin='" . time() . "' WHERE username='{$username}' LIMIT 1"); 
      }
      else {
        header("Location: ". $MM_redirectLoginFailed );
      }
    }
    ?>
    ... but that doesn't work. Can someone give me some advice?

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: keep track of last login date somehow?

    that's exactly where you would be putting it. but, unless you're defining $username that won't do anything (obviously?). I hope you're using $loginUsername instead in the query.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    790

    Re: keep track of last login date somehow?

    Oops to something silly that I've done.

    I forgot to make the lastlogin set to timestamp (which is what I want). I used this code where I placed the old one and it works perfectly:

    Code:
    mysql_query("UPDATE members SET lastlogin= NOW() WHERE username='{$loginUsername}' LIMIT 1");
    ... thanks to you kows!

Posting Permissions

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



Click Here to Expand Forum to Full Width