I have this on my login page:
$current_time = date("g:i A");
$current_date = date("l, F jS, Y");
mysql_query("UPDATE employees
SET last_login = '$current_time', last_login_date = '$current_date' WHERE username='$username' AND password='$password'");
And then on the page when the user is logged in (the protected page) I have this:
[...]
$last_login_date = $info['last_login_date']; // get last_login_date from mysql table
$last_login_time = $info['last_login_time']; // get last_login_time from mysql table
?>
[...]
You last signed in on <?php echo $last_login_date; ?> at <?php echo $last_login_time; ?>.
But this doesn't work properly because it shows the time they just logged in. How can I make it show the last time they logged in?
I also even tried adding a prev_login_time and prev_login_date to the table too, but I don't know how to make it update properly because it would update every time they login making it show the time they just logged in. (so it would be showing the current time to them)
Thanks.
Edit: [for EdoDodo]
Here's the login if statement that is setting the last IP address session variable:
if($_POST['submit']) {
$sql = "SELECT * FROM employees WHERE username='$username' and password='$password'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$info = mysql_fetch_array($result);
if($count == 1) {
$_SESSION['loggedin'] = 1;
$_SESSION['user'] = $username;
$_SESSION['last_login_time'] = $info['last_login'];
$_SESSION['last_login_date'] = $info['last_login_date'];
$_SESSION['last_ip'] = $info['last_login_ip'];
$return = $_SESSION['returnurl'];
if(!$return) {
$return = "/employee/";
}
date_default_timezone_set('America/New_York');
$current_time = date("g:i A");
$current_date = date("l, F jS, Y");
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("UPDATE employees
SET last_login = '$current_time', last_login_date = '$current_date', last_login_ip = '$ip' WHERE username='$username' AND password='$password'");
header("Location: $return");
exit();
}
The last login date and time is working, which is why I don't know why it isn't working with the IP one. I am accessing it correctly on the protected page:
$last_login_date = $_SESSION['last_login_date'];
if($last_login_date = date("l, F jS, Y")) {
$last_login_date = "Today";
}
$last_login_time = $_SESSION['last_login_time'];
if($_SESSION['last_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_ip'];
}
echo "Last account login: ".$last_login_date." at " . $last_login_time . " from " . $last_login_ip . ".";
Am I doing something wrong? Maybe I'm not setting the session variable or retrieving it properly? But it seems like I am. I hope you can help. Thanks in advance.
解决方案
You'll want to fetch the login time from the table in the database into the $info array before you update the database and overwrite the time with the new one. Then, you'll have the previous login time in the $info array (and, if you want to carry it across lots of different page loads, you could put it in a session variable).