php 如何显示用户使用 MySQL 登录的上次登录日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7637791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:04:27  来源:igfitidea点击:

How can I show the last login date and time that user logged in with MySQL?

phpmysql

提问by Nathan

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:

然后在用户登录的页面上(受保护的页面)我有这个:

 [...]

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

但这不能正常工作,因为它显示了他们刚刚登录的时间。我怎样才能让它显示他们上次登录的时间?

I also even tried adding a prev_login_timeand prev_login_dateto 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)

我什至也尝试在表中添加一个prev_login_timeprev_login_date,但我不知道如何使其正确更新,因为它会在他们每次登录时更新,从而显示他们刚刚登录的时间。(所以它会显示当前时间给他们)

Thanks.

谢谢。



Edit: [for EdoDodo]

编辑:[EdoDodo]

Here's the login if statement that is setting the last IP address session variable:

这是设置最后一个 IP 地址会话变量的 login if 语句:

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:

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

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

难道我做错了什么?也许我没有设置会话变量或正确检索它?但好像我是。我希望你能帮忙。提前致谢。

回答by EdoDodo

You'll want to fetch the login time from the table in the database into the $infoarray beforeyou update the database and overwrite the time with the new one. Then, you'll have the previous login time in the $infoarray (and, if you want to carry it across lots of different page loads, you could put it in a session variable).

更新数据库并用新的时间覆盖时间之前,您需要从数据库中的表中获取登录时间到$info数组中。然后,您将在数组中获得之前的登录时间(并且,如果您想在许多不同的页面加载中携带它,您可以将它放在会话变量中)。$info