-1

I have a Dreamweaver login page and I'd like to start recording the last login date/time and IP address for users.

I have created two new columns in the database - lastlogin and lastip.

I think this is the section of the page I need to modify, but can anyone offer some advice?

<?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=md5($_POST['password']);
  $MM_fldUserAuthorization = "type";
  $MM_redirectLoginSuccess = "account.php";
  $MM_redirectLoginFailed = "index.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_loyalty, $loyalty);


  $LoginRS__query=sprintf("SELECT username, password, type FROM users WHERE username=%s AND password=%s",
  GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $loyalty) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {

$loginStrGroup  = mysql_result($LoginRS,0,'type');

//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
$ip = $_SERVER["REMOTE_ADDR"];
$update = "UPDATE users SET lastlogin = NOW() , lastip = '$ip' WHERE username = '$loginUsername'";
$result = mysql_query($update) or die (mysql_error());

if (isset($_SESSION['PrevUrl']) && false) {
  $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
}
header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
header("Location: ". $MM_redirectLoginFailed );
  }
}
?>

*Update - the above script works

  • I can't see any mention of the two columns you've created, could you please add the PHP you've tried and point out which part of it isn't working? – Joe May 27 '14 at 14:39
  • I haven't tried any yet Joe as I'm not sure about the best way to go about it. – user2475770 May 27 '14 at 14:40
  • When a user successfully logs in just update the columns with their IP and a timestamp. I'd imagine you'd want to do that inside `if($loginFoundUser){ /* here */ }`. – Joe May 27 '14 at 14:43
  • I've added $query = "UPDATE users SET lastlogin = NOW() WHERE username = '$loginUsername'"; $query = "UPDATE users SET lastip = '$ip' WHERE username = '$loginUsername'"; but that doesn't seem to work. Sorry to be dumb! – user2475770 May 27 '14 at 15:00
  • I'd strongly recommend you start using [mysqli_*](http://www.php.net/manual/en/book.mysqli.php) or [PDO](https://php.net/manual/en/book.pdo.php). Also, your `UPDATE` queries can be [combined into one](http://stackoverflow.com/questions/5254173/php-update-multiple-mysql-fields-in-single-query). The main reason your updated code doesn't work is that you haven't actually executed the database queries. – Joe May 27 '14 at 15:29

1 Answers1

0
    //declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
$ip = $_SERVER["REMOTE_ADDR"];
$update = "UPDATE users SET lastlogin = NOW() , lastip = '$ip' WHERE username = '$loginUsername'";
$result = mysql_query($update) or die (mysql_error());

Works, apologies for being daft!