Home  >  Article  >  Backend Development  >  How to set cookie to remember password in php

How to set cookie to remember password in php

藏色散人
藏色散人Original
2021-09-14 10:30:513309browse

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.

How to set cookie to remember password in php

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;

During the implementation process, based on some code posts on the Internet, the following code was sorted out: In addition You can refer to the PHP login remember password implementation idea

In the login.php page, perform form settings:

<?php
session_start();
?>
<form  action="login_chk.php" method="post">
  

login_chk.php page is used to verify the login page form information and create cookies:

 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[&#39;username&#39;]) or empty($_POST[&#39;pass&#39;])){10     echo "<script language=&#39;javascript&#39;>alert(&#39;用户名和密码不能为空!&#39;);history.go(-1);</script>";11 }12 else{ 
13     $username=$_POST[&#39;username&#39;];14     $pass=$_POST[&#39;pass&#39;];15     $password = md5($pass);16     $remember = $_POST[&#39;remember&#39;]; 
17     
18     $testrst = sqlsrv_query($conn, "select * from Employee where name like &#39;$username&#39; or number like &#39;$username&#39;");    //执行查询操作  
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 &#39;$username&#39; or number like &#39;$username&#39;) and pwd = &#39;$password&#39;");32         
33         
34         if(sqlsrv_has_rows($rst)){                                                              //判断登录用户名和密码是否正确35             $adminrow = sqlsrv_fetch_array($rst);37             $userwhethe = 0;38             $_SESSION[&#39;id&#39;]=$adminrow[0];      
41             $_SESSION[&#39;number&#39;]=$adminrow[1];42             $_SESSION[&#39;name&#39;]=$adminrow[2];43             if($username == $adminrow[1]){44               $_SESSION[&#39;type&#39;] = 1;45             }else{46                $_SESSION[&#39;type&#39;] = 2;47             }57 
59               echo "<meta http-equiv=\"refresh\" content=\"0;url=menu.php\" />";60 64         }else{65           echo "<script>alert(&#39;密码错误,请重新登录。&#39;);history.go(-1);</script>";66         }67    }else{68        echo "<script>alert(&#39;该用户名不存在!,请重新登录。&#39;);history.go(-1);</script>";69    }70 }71 
72 ?>

menu.php and other functional pages will check the session:

<?php
session_start();
include_once("conn/conn.php");
error_reporting(0);
if(empty($_SESSION[&#39;name&#39;]) and empty($_SESSION[&#39;id&#39;])){              //判断当前用户是否为登录状态
echo "<script>alert(&#39;请登录后再进行执行操作!&#39;);history.go(-1);</script>";
}else{
?>
网页主体
?>

As for checking the session and invalidating the cookie for assignment, it is implemented in index1.php (index check page) :

 1 2d85d25d97dd14ec9f1ac797789cbed0
In addition, considering the user’s need to log out of the system or log out and log in again, the logout page logout.php:

<?php
session_start();
unset($_SESSION[&#39;username&#39;]);
unset($_SESSION[&#39;password&#39;]);
setcookie(&#39;username&#39;,&#39;&#39;,0);
setcookie(&#39;password&#39;,&#39;&#39;,0);
header("location:index.php");
?>

After implementation, the use is smooth. However, the cookie and session saving of passwords is not very appropriate, and the password form is not saved by default on the user login interface. This aspect of the function still needs to be improved.

Recommended learning: "

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn