mysql 登录时间设置,如何显示用户使用MySQL登录的最后登录日期和时间?

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值