Home >Backend Development >PHP Tutorial >How to implement php to remember password and automatically log in next time_PHP Tutorial

How to implement php to remember password and automatically log in next time_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:251249browse

How to implement PHP to remember the password and automatically log in next time

When building a website, we often encounter the problem of remembering the password and automatically logging in next time. No need to log in within a week. There is no need to log in for one 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:

$username=trim($_POST['username']);
$password=md5(trim($_POST['password']));
$ref_url=$_GET['req_url'];
$remember=$_POST['remember'];//是否自动登录标示
$err_msg='';
if($username==''||$password==''){
	$err_msg="用户名和密码都不能为空";
}else{
	$row=getUserInfo($username,$password);
	if(empty($row)){
		$err_msg="用户名和密码都不正确";
	}else{
		$_SESSION['user_info']=$row;
		if(!empty($remember)){//如果用户选择了,记录登录状态就把用户名和加了密的密码放到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.

//检查用户是否登录
function checklogin(){
	if(empty($_SESSION['user_info'])){//检查一下session是不是为空
		if(empty($_COOKIE['username'])||empty($_COOKIE['password'])){//如果session为空,并且用户没有选择记录登录状
			header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);//转到登录页面,记录请求的url,登录后跳转过去,用户体验好。
		}else{//用户选择了记住登录状态
			$user=getUserInfo($_COOKIE['username'],$_COOKIE['password']);//去取用户的个人资料
		if(empty($user)){//用户名密码不对没到取到信息,转到登录页面
			header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);
		}else{
			$_SESSION['user_info']=$user;//用户名和密码对了,把用户的个人资料放到session里面
		}
		}
	}
}

Articles you may be interested in

  • How to set up phpmyadmin to automatically log in and cancel automatic login
  • php extracts the birthday date in the ID number and verifies whether it is unknown Functions for adults
  • Solution to phpMyAdmin not allowing empty password login
  • How to set up automatic login in phpmyadmin
  • php realizes compressing and downloading files in batches
  • How does PHP determine whether a gif image is a dynamic image (animation)
  • php How to get the start timestamp and end timestamp of today, yesterday, last week, and this month
  • A phper The most authentic work and life experience, dedicated to the majority of PHPER enthusiasts

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/982749.htmlTechArticleHow to implement php to remember the password and automatically log in next time. 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. ...
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