php method to set cookies to remember passwords: 1. In the login.php page, set the form; 2. Verify the form information on the login page and create cookies; 3. Check the session and use cookies to assign values. That’s it.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php realizes remembering the password automatically next time Login
This blog also writes about the method to implement the function of "remember my login status". In short, it is to first log in Session assigns user information, detects the session, and uses cookies to assign values after it expires;
<?php session_start(); ?> <form action="login_chk.php" method="post">
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 ?>
<?php session_start(); include_once("conn/conn.php"); error_reporting(0); if(empty($_SESSION['name']) and empty($_SESSION['id'])){ //判断当前用户是否为登录状态 echo "<script>alert('请登录后再进行执行操作!');history.go(-1);</script>"; }else{ ?> 网页主体 ?>
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 ?>
<?php session_start(); unset($_SESSION['username']); unset($_SESSION['password']); setcookie('username','',0); setcookie('password','',0); header("location:index.php"); ?>
PHP Video Tutorial"
The above is the detailed content of How to set cookie to remember password in php. For more information, please follow other related articles on the PHP Chinese website!