Home >Backend Development >PHP Tutorial >Example of remembering the password and automatically logging in next time in php, remembering the password and logging in automatically_PHP tutorial

Example of remembering the password and automatically logging in next time in php, remembering the password and logging in automatically_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:56970browse

Example of remembering password and automatically logging in next time in php, remembering password and logging in automatically

When building a website, we often encounter the need to remember the password, automatically log in next time, no need to log in within a week, and no need to log in within a month. This function is generally implemented through cookies. This article will briefly talk about how to use php to achieve this requirement. Of course, there are many ways to achieve this requirement.

The whole process is that when the user logs in, if he chooses to remember the password or does not need to log in for a week, etc., after the user successfully logs in, the data of a cookie that implements automatic login is stored in the database. In the user table, it is used for verification during the next automatic login. If the verification is passed, you will log in automatically. Otherwise, you need to enter your username and password to log in. The saved cookie value can take a random code.

The specific sample code is as follows:

Copy code The code is as follows:

$username=trim($_POST['username']);
$password=md5(trim($_POST['password']));
$ref_url=$_GET['req_url'];
$remember=$_POST['remember'];//Whether to automatically log in mark
$err_msg='';
if($username==''||$password==''){
$err_msg="Both username and password cannot be empty";
}else{
$row=getUserInfo($username,$password);
if(empty($row)){
$err_msg="Username and password are incorrect";
}else{
$_SESSION['user_info']=$row;
if(!empty($remember)){//If the user chooses, record the login status and put the username and encrypted password in the cookie
setcookie("username",$username,time()+3600*24*365);
setcookie("password",$password,time()+3600*24*365);
}
if(strpos($ref_url,"login.php")===false){
header("location:".$ref_url);
}else{
Header("location:main_user.php");
}
}
}

In addition, when visiting each page of the website, the following functions must be checked first.

Copy code The code is as follows:

//Check if the user is logged in
function checklogin(){
if(empty($_SESSION['user_info'])){//Check whether the session is empty
if(empty($_COOKIE['username'])||empty($_COOKIE['password'])){//If the session is empty and the user does not choose to record the login status
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);//Go to the login page, record the requested url, and jump to it after logging in. The user experience is good.
}else{//The user chose to remember login status
$user=getUserInfo($_COOKIE['username'],$_COOKIE['password']);//Get the user's personal information
if(empty($user)){//The username and password are incorrect and I haven’t gotten the information yet. Go to the login page
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);
}else{
$_SESSION['user_info']=$user;//If the username and password are correct, put the user’s personal information into the session
}
}
}
}

php automatically log in next time

Judging whether the user is logged in or not is generally determined by cookies, so this implementation generally relies on setting the cookie time. First determine whether to stay logged in. If so, set the time to be longer when setting the cookie. Some. You can set it to be as long as you want, it can be one day, one month, or one year.
setcookie("TestCookie", $value, time()+3600);
where time()+3600 is the time, which means to stay logged in for one hour from now.

How to remember username and password in php

Learn technology well. SESSION and COOKIE are specifically designed to solve this problem. Use COOKIE to save it locally. Use COOKIE

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907280.htmlTechArticleAn example of remembering the password and automatically logging in next time in php. Remembering the password and automatically logging in often happens when building a website. Encountered the need to remember the password, automatically log in next time, no need to log in for a week, one...
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