如何显示用户使用MySQL登录的最后登录日期和时间? [英] How can I show the last login date and time that user logged in with MySQL?

查看:810
本文介绍了如何显示用户使用MySQL登录的最后登录日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录页面上有这个

   $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:

 [...]

 <?php 
 $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?

我什至尝试将prev_login_timeprev_login_date添加到表中,但是我不知道如何使其正确更新,因为它会在每次登录时更新,从而显示出他们刚刚登录的时间. .(这样会向他们显示当前时间)

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)

谢谢.

这是登录if语句,用于设置最后一个IP地址会话变量:

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();
     } 

上次登录的日期和时间有效,这就是为什么我不知道为什么它不能与IP一起使用的原因.我正在受保护的页面上正确访问它:

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.

推荐答案

在更新数据库之前,您需要将登录时间从数据库中的表中提取到$info数组中.并用新的时间覆盖时间.然后,您将在$info数组中拥有先前的登录时间(并且,如果您希望将其登录到许多不同的页面加载中,则可以将其放在

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

这篇关于如何显示用户使用MySQL登录的最后登录日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆