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

How to set cookie to remember password in php

藏色散人
Release: 2023-03-12 14:28:01
Original
3369 people have browsed it

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">
  
Copy after login

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 ?>
Copy after login

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{
?>
网页主体
?>
Copy after login

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

 1 <?php 2 session_start(); 3 
 4  if(empty($_SESSION[&#39;username&#39;])){                                      //检查一下session是不是为空 
 5      if(empty($_COOKIE[&#39;username&#39;]) || empty($_COOKIE[&#39;password&#39;])){  
 6         header("location:login.php");                         
 7      }else{                                                               
 8         $_SESSION[&#39;name&#39;] = $_COOKIE[&#39;username&#39;];   
 9         header("location:menu.php"); 
10     } 
11       
12  }13 ?>
Copy after login

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");
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template