简体   繁体   English

PHP-从“ ipaddress”登录的最后“ date”?

[英]PHP - Last “date” logged on from this “ipaddress”?

I wanna know if php can remotely store when the user is logged in such as date and ipaddress, maybe use "COOKIES" or a database, I don't know and I'm not sure. 我想知道当用户登录时php是否可以远程存储日期和ipaddress等信息,也许使用“ COOKIES”或数据库,我不知道,我不确定。 Btw, my question means that if last time I logged on was "4/5/2014 from 108.25.1.123" 顺便说一句,我的问题是,如果我上次登录的时间是“ 2014年5月4日,来自108.25.1.123”

it would echo 它会回声

  • Last Login: 4/5/2014 from 108.25.1.123 上次登录时间:2014年5月4日的108.25.1.123

ignore the database and cookies if im wrong. 如果错误,则忽略数据库和cookie。

You can have a foot.php file in all of your files in the website. 您可以在网站上的所有文件中都有一个foot.php文件。 That foot.php will have a code something like this: 该foot.php将具有类似以下的代码:

<?php
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $last_login = date("Y-m-d H:i:s");

    $connection = mysql_connect($server, $username, $password);
    mysql_select_db($db, $connection);
    $query = sprintf('UPDATE table_users SET ip_address=%s, last_login=%s WHERE user_id=%s', $ip_address, $last_login, $_SESSION['user_id']);
?>

This will ensure that whenever a user visits a page in your website, the database will be updated automatically. 这样可以确保每当用户访问您网站上的页面时,数据库都会自动更新。 Hope this helped. 希望这会有所帮助。 :) :)

Edit: PDO version: 编辑: PDO版本:

<?php
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $last_login = date("Y-m-d H:i:s");
    $connection = new PDO('mysql:host=localhost;dbname=testdb', $username, $password);
    $query = sprintf('UPDATE table_users SET ip_address=%s, last_login=%s WHERE user_id=%s', $ip_address, $last_login, $_SESSION['user_id']);
    $count = $connection->exec($query);
    printf('Updated $count rows.');
?>

database method after the user successfully logs in you redirect the user I guess, for example 用户成功登录后的数据库方法,例如,我将用户重定向

login.hp login.hp

if(){
 //validations
 }
 else{
   update_last_sign_in();//call function which updates the last login
  //success redirect the user
  }

you could have this function on some other file or the same page if you want. 您可以在其他文件或同一页面上使用此功能。

 function update_last_sign_in()
 {
 $username = $_SESSION['logged_in_user'];
 $ip = $_SERVER['REMOTE_ADDR'];
 $date =   date('Y-m-d H:i:s');

 $database = new mysqli('localhost', 'user', 'pass', 'database_name');
 $stmt = $database->prepare('UPDATE last_login set username = ?, user_ip = ?, login_date = ?');
 $stmt->bind_param('sss', $username, $ip, $date);
 $stmt->execute();
 }

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

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