php实现记住密码下次自动登陆
这篇博客里面还写到 实现“记住我的登录状态”的功能方法,简言之,就是对首先对session进行用户信息赋值,检测session,失效后,利用cookie对其赋值;
在实现过程中,根据网上一些代码贴,整理出以下代码:另外可以参考PHP 登录记住密码实现思路
在登录login.php页面中,进行表单设置:
<?php
session_start();
?>
<form action="login_chk.php" method="post"> <div class="inpu"> <input id="username" name="username" type="text" class="username" value="<?php if($_SESSION['type'] == 1){echo $_SESSION['number'];}else{echo $_SESSION['name'];}?>" placeholder="用户名"> </div> <div class="inpu"> <input id="pass" name="pass" type="password" class="password" placeholder="登录密码" > </div> <div class="remember"> <input class="inpu_remember" type="checkbox" name="remember" ><label>记住我</label> </div> <button id="submit" type="submit">登录</button> </form>
login_chk.php页面用于验证登录页面表单信息,并创建cookie:
1 <?php
2 header("Content-type:text/html;charset=gb2312");
3
4 session_start();
5 include_once("conn/conn.php"); //加载数据库连接文件
6
7 error_reporting(0);
8
9 if(empty($_POST['username']) or empty($_POST['pass'])){
10 echo "<script language='javascript'>alert('用户名和密码不能为空!');history.go(-1);</script>";
11 }
12 else{
13 $username=$_POST['username'];
14 $pass=$_POST['pass'];
15 $password = md5($pass);
16 $remember = $_POST['remember'];
17
18 $testrst = sqlsrv_query($conn, "select * from Employee where name like '$username' or number like '$username'"); //执行查询操作
19
20 if(!empty($remember)){ //如果用户选择了,记录登录状态就把用户名和加了密的密码放到cookie里面
21 setcookie("username", $username, time()+3600*24*30);
22 setcookie("password", $pass, time()+3600*24*30);
23 }
24
25
26
27
28
29 if(sqlsrv_has_rows($testrst)){
30
31 $rst = sqlsrv_query($conn, "select * from Employee where (name like '$username' or number like '$username') and pwd = '$password'");
32
33
34 if(sqlsrv_has_rows($rst)){ //判断登录用户名和密码是否正确
35 $adminrow = sqlsrv_fetch_array($rst);
37 $userwhethe = 0;
38 $_SESSION['id']=$adminrow[0];
41 $_SESSION['number']=$adminrow[1];
42 $_SESSION['name']=$adminrow[2];
43 if($username == $adminrow[1]){
44 $_SESSION['type'] = 1;
45 }else{
46 $_SESSION['type'] = 2;
47 }57
59 echo "<meta http-equiv=\"refresh\" content=\"0;url=menu.php\" />";
60 64 }else{
65 echo "<script>alert('密码错误,请重新登录。');history.go(-1);</script>";
66 }
67 }else{
68 echo "<script>alert('该用户名不存在!,请重新登录。');history.go(-1);</script>";
69 }
70 }
71
72 ?>
menu.php以及其他功能页面都会检查一遍session:
1 <?php
2 session_start();
3 include_once("conn/conn.php");
4 error_reporting(0);
5 if(empty($_SESSION['name']) and empty($_SESSION['id'])){ //判断当前用户是否为登录状态
6 echo "<script>alert('请登录后再进行执行操作!');history.go(-1);</script>";
7 }else{
8 ?>
9 网页主体
10 ?>
至于检验sessionf,失效利用cookie进行赋值的操作,在index1.php(index的检查页)中实现:
1 <?php
2 session_start();
3
4 if(empty($_SESSION['username'])){ //检查一下session是不是为空
5 if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){
6 header("location:login.php");
7 }else{
8 $_SESSION['name'] = $_COOKIE['username'];
9 header("location:menu.php");
10 }
11
12 }
13 ?>
另外,考虑到用户有退出系统或者注销再登录的需要,设置了退出页面logout.php:
1 <?php
2 session_start();
3 unset($_SESSION['username']);
4 unset($_SESSION['password']);
5 setcookie('username','',0);
6 setcookie('password','',0);
7 header("location:index.php");
8 ?>
实现后,使用流畅。但对密码的cookie和session保存感觉不是很恰当,在用户登录界面也没有对密码表单进行值得默认保存,这个方面的功能还需要完善。