教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php记录并显示用户上次登录的时间

php记录并显示用户上次登录的时间

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php记录并显示用户上次登录的时间等资源,欢迎您收藏本站,我们将为您提供最新的php记录并显示用户上次登录的时间资源
我们会看到有少的网站在用户登录一次之后下一次会显示你上次登录的时间了,实现这种php登录显示用户上次登录的时间的方法有很多实现技巧,如直接使用数据库记录或直接使用php cookie记录,下面我们来介绍利用cookie记录用户上次登录了。

创建脚本文件 form_login.php

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7183')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7183><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <title>用户登录</title>
 <link rel='stylesheet' type='text/css' href='style.css'>
 <style>
  #showTime{
   width:291px;
   border:1px solid #ccc;
   line-height:23px;
   position:absolute;
   left:500px;
   top:48px;
   text-align:center;
  }
 </style>
</head>
<body>
 
 <?php
 if(isset($_COOKIE['login_now'])){
  echo "<div id='showTime'>";
  echo '您上次登录的时间为:'.$_COOKIE['login_now'];
  echo "</div>";
 }
 ?>
</div> 
<div id="login">
<h2>
 <input id="fnquit" type="button" title="退出登录" />
 <a href="#" class="cur" name="Email">Email登录</a>
 </h2>
 <ul>
  <form id="myForm" name="myForm"  method="post" action='login_time.php'>
   <li>
    <input id="login_type" name="login_type" type="hidden"/>
    <tt>
     <label id="log_type" for="email">Email:</label>
    </tt>
    <div>
     <input id="username" name="username" type="text" class="cell" />
    </div>
   </li>
   <li>
    <tt>
     <label for="password">密 码:</label>
    </tt>
    <div>
     <input id="password" name="password" type="password" class="cell" />
    </div>
   </li>
   <li>
    <div>
     <input id="auto_login" name="auto_loginmberme" type="checkbox"/>
     <label for="auto_login">下次自动登录</label>
    </div>
   </li>
   <li>
    <div>
     <input id="sub" type="submit" value=''/><a href="#" target="_blank" >忘记密码?</a>
    </div>
   </li>
</form>
</ul>
</div>
 
</body>
</html>


创建脚本文件 login_time.php

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2978')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2978>

<?php
date_default_timezone_set('Asia/Shanghai');//设置时区为东八区
if(!empty($_POST['username'])){
    $name = $_POST['username'];
    $pwd = $_POST['password'];
    if($name = 'admin' && $pwd = 'admin'){
        setCookie('login_now',date('Y-m-d H:i:s'),time() 3600*24);
        echo "<script>alert('登录成功!');location='login_in.php'</script>";
    }else{
        echo "<script>alert('登录失败!');location='login.php'</script>";
    }
}
?>


创建脚本文件 login_in.php

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2149')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2149><?php
    echo "用户登录时间:".$_COOKIE['login_now']."<br>";
    echo "<a href='form_login.php'>注销返回</a>";
?>

补充:如果是数据库会更好一些因为cookie如果用户把它清除cookie了就没有下载一次了哦。

您可能感兴趣的文章:
php怎么查询当前用户名
asp.net单点登录实现
php会话技术之cookie的例子
php记录并显示用户上次登录的时间
查看linux日志的方法
php利用$_COOKIE实现自动登录程序代码
php实现记住密码自动登录的功能
使用日志保护Linux的安全
深入学习linux日志系统(1)
session cookie实现自动登录(下次自动登录)

[关闭]
~ ~