Home  >  Article  >  Backend Development  >  Code to implement automatic login by remembering password in php_PHP tutorial

Code to implement automatic login by remembering password in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:141030browse

There is more than one way to realize automatic login in PHP by remembering passwords. There are two emptyempty shown below, but they are actually one. That is because there is a bug in the code highlighting. Hope it helps everyone.

1. User login check

Copy code The code is as follows:

//Check whether the user is logged in
function checklogin(){
if(emptyempty($_SESSION['user_info'])){ //Check whether the session is empty
if(emptyempty($_COOKIE[ 'username']) || emptyempty($_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, jump to it after logging in, and have a good user experience.
}else{ //The user has chosen to remember the login status
$user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //Get the user's personal information
if(emptyempty($user)){ //The username and password are incorrect and the information is not retrieved. 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 in the session
}
}
}
}
//Check whether 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 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, which provides a good user experience.
}else{ //The user has chosen to remember the login status
$user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //Get the user's personal information
if(empty($user)){ //The username and password are incorrect. If the information is not obtained, 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 in the session
}
}
}
}

When accessing each page in the background, the above checks must be performed first

Second, the user submits login information
When the user fills in the username and password, submit it here,
Copy code The code is as follows:

$username = trim( $_POST['username']);
$password = md5(trim($_POST['password']));
$validatecode = $_POST['validateCode'];
$ref_url = $ _GET['req_url'];
$remember = $_POST['remember'];

$err_msg = ”;
if($validatecode!=$_SESSION['checksum']){
$err_msg = "Verification code is incorrect";
}elseif($username==" || $password=="){
$err_msg = "Both username and password cannot be empty";
}else{
$row = getUserInfo($username,$password);

if(emptyempty($row)){
$err_msg = "Both the username and password are incorrect";
}else{
$_SESSION['user_info'] = $row;
if(!emptyempty($remember)){ //If the user chooses, the username and password will be recorded and the login status will be recorded Put the 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");
}
}
}
$username = trim($_POST['username']);
$password = md5(trim ($_POST['password']));
$validatecode = $_POST['validateCode'];
$ref_url = $_GET['req_url'];
$remember = $_POST['remember '];

$err_msg = ”;
if($validatecode!=$_SESSION['checksum']){
$err_msg = “Verification code is incorrect”;
}elseif ($username==” || $password==”){
$err_msg = “Neither username nor password can be empty”;
}else{
$row = getUserInfo($username,$password );

if(empty($row)){
$err_msg = "Both the 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”);
}
}
}

A brief explanation about $ref_url. Suppose: User A accesses b.php, but user A is not logged in, jumps to the login page login.php, fills in the user and password on the login page, and then jumps to b.php page, instead of jumping to the default page main_user.php. Because b.php is the page that user A wants to go to, the user experience will be better.

Three, when the user clicks to log out, clear the login status

Why do you do this, because if someone else uses your computer, they may Your personal privacy will be browsed, so when the user specifically clicks to log out, it is best to cancel the recorded login status.
Copy code The code is as follows:

//Log out
function logout(){
unset($ _SESSION['user_info']);
if(!emptyempty($_COOKIE['username']) || emptyempty($_COOKIE['password'])){
setcookie("username", null, time ()-3600*24*365);
setcookie("password", null, time()-3600*24*365);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322892.htmlTechArticleThere is more than one way to remember the password and automatically log in in php. There are two emptyempty below. In fact, it is one. That is Because there is a bug in code highlighting. Hope it helps everyone. 1. User login...
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