简体   繁体   English

如何在PHP登录后获取最后登录的管理时间和日期

[英]how to get last logged in admin time and date after login me in php

I have 5 Admin in the database. 我在数据库中有5个Admin。 So now I want to get the last user logged in date and time detail. 所以现在我想让最后一个用户登录日期和时间细节。 Who worked before me.means after my logged in the dashboard I want to see previous USER login and logout time and date. 谁在我之前工作。在登录仪表板后我想看到以前的USER登录和注销时间和日期。

I am able to get my (current user) login date and time. 我能够获得我的(当前用户)登录日期和时间。 But I don't have an idea how can I get previous user(Admin) login and logout date/time detail. 但我不知道如何获得以前的用户(管理员)登录和注销日期/时间细节。

I am sending my login.php code.Where i wrote current user time/date code.Please see the code and give me suggestion how can i get previous user login and logout date/time. 我发送了我的login.php代码。我写了当前的用户时间/日期代码。请看代码并给我建议如何获得以前的用户登录和注销日期/时间。 Thanks. 谢谢。

<?php include "db.php"; ?>
<?php session_start(); ?>
<?php
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];

$username = mysqli_real_escape_string($connection,$username);
$password = mysqli_real_escape_string($connection,$password);
$password = md5($password);
$login_query = "SELECT * FROM users WHERE user_username = '$username'" ;
$result_query = mysqli_query($connection,$login_query);
$count = mysqli_num_rows($result_query);
if(!$count){
die("QUERY FAILED". mysqli_error($connection));
}
while($row= mysqli_fetch_array($result_query)){
$log_user_id = $row['user_id'];
$log_user_username = $row['user_username'];
$log_user_password = $row['user_password'];
$log_user_firstname = $row['user_firstname'];
$log_user_lastname = $row['user_lastname'];
$log_user_role = $row['user_role'];
$log_user_time = $row['time'];
$log_user_ip = $row['ip'];
}
if($username !== $log_user_username && $password !== $log_user_password){
header ("location: ../index.php");
}elseif($username == $log_user_username && $password == $log_user_password){
if($count == 1) {
$_SESSION['username']=$log_user_username;
$_SESSION['firstname']=$log_user_firstname;
$_SESSION['lastname']=$log_user_lastname;
$_SESSION['user_role']=$log_user_role;
$_SESSION['last_login'] = $log_user_time;
$_SESSION['last_login_ip'] = $log_user_ip;

date_default_timezone_set("Asia/Kolkata");
$current_date = date("F d, Y, h:i:s A"); 
$ip = $_SERVER['REMOTE_ADDR'];
$query = "UPDATE users SET time= NOW() ,ip='$ip' WHERE user_username='$username'";
mysqli_query($connection,$query);

$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60)); 
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60); 
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) { 
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) { 
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';
} 
}
if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "from this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_login_ip'];
} 
}
header ("location: ../admin");
}else{
header ("location: ../index.php");
}
?>

So after my logged in on Dashboard (index.php) the page i did echo the variables and i can able to see current user login date/time detail.Which is my login detail. 因此,在我登录Dashboard(index.php)后,我确实回显了变量的页面,我可以看到当前用户登录日期/时间的详细信息。这是我的登录详细信息。

Please give me suggestion how can i get previous user login and logout detail. 请给我建议我如何获得以前的用户登录和注销详细信息。 Thanks 谢谢

notification.php notification.php

   <span class="pull-right text-muted small"><em></em></span></a>


                               <?php


date_default_timezone_set("Asia/Kolkata");
$last_login_date = date("F d, Y, h:i:s A");                               

$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60)); 
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60); 
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));

if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) { 
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) { 
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';

} 

if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
    $last_login_ip = "From this IP address (".$_SERVER['REMOTE_ADDR'].")";
    }
    else {
    $last_login_ip = "IP address ".$_SESSION['last_login_ip'];
    }




?>


                                  <a href="#" class="list-group-item">
                                    <i class="fa fa-user"></i> User Name
                                    <span class="pull-right text-muted small"><em><?php echo $_SESSION['username']; ?></em>
                                    </span>
                                </a>

                                <a href="#" class="list-group-item">
                                    <i class="fa fa-sign-in"></i> Last Login Time
                                    <span class="pull-right text-muted small"><em><?php echo $time1; ?></em>
                                    </span>
                                </a>
                                <a href="#" class="list-group-item">
                                    <i class="fa fa-sign-out"></i> Last Logout Time
                                    <span class="pull-right text-muted small"><em>27 minutes ago</em>
                                    </span>
                                </a>
                                <a href="#" class="list-group-item">
                                    <i class="fa fa-tasks fa-fw"></i> User Ip
                                    <span class="pull-right text-muted small"><em><?php echo $last_login_ip; ?></em>
                                    </span>
                                </a>

here iam getting admin login date and time detail.But i unable to get previous visitor login detail . 在这里我获得管理员登录日期和时间详细信息。但我无法获得以前的访客登录详细信息。 i can just see only my login time/date detail after login in here. 登录后我只能看到登录时间/日期详细信息。 So please help. 所以请帮忙。

I can give you the basic logic for this. 我可以为你提供基本的逻辑。 In this you can set the session variable that it will act as flag. 在此,您可以设置它将充当标志的会话变量。

// start session 
session_start(); 

// if not yet logged in update database 
if(!isset($_SESSION['logged'])) 
{ 
    // update your database here 
   $query = "UPDATE table  
            SET LastLogin=now()  
            WHERE ID='$ID'"; 
   $result = mysql_query($query); // etc... 

   // if the table was updated 
   if($result === true) 
   { 
      // then create a session var 
      $_SESSION['logged'] = 1; 
   } 
} 

OR 要么

You can insert the user id, date and time at the time of login and get it back as last id that will be your previous login. 您可以在登录时插入用户ID,日期和时间,并将其作为您之前登录的最后一个ID返回。

Create one activity table in database. 在数据库中创建一个活动表。 Click the link to view example here-> User Activity Table Structure 单击链接以查看此处的示例 - > 用户活动表结构

Every time when any user logs in, just enter the login time and user id along with other required details in the activity table. 每次任何用户登录时,只需在活动表中输入登录时间和用户ID以及其他所需的详细信息。 At the time of login you can change the login status to deactivated for that particular session. 在登录时,您可以将该登录状态更改为该特定会话的停用状态。 Next time when any user logs in you can get the last login time from database and current time from PHP date() function. 下次当任何用户登录时,您可以从PHP date()函数获取数据库和当前时间的最后登录时间。 Just get the time difference between those 2 dates and you will get the last login time and duration by previous user. 只需获得这两个日期之间的时差,您将获得上一个用户的上次登录时间和持续时间。 That's it. 而已。

Hope this would work. 希望这会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM