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">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['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 and other functional pages will check the session: <?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{
?>
网页主体
?>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['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 ?> 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['username']);
unset($_SESSION['password']);
setcookie('username','',0);
setcookie('password','',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!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)






