In PHP implementation Remember password next automatic login example, remember password automatic login
Do the site often encounter to achieve remember password, the next automatic login, a week to avoid landing, one months to avoid the need to log in. This functionality is generally done through cookies. This article will briefly explain how to use PHP to implement this requirement. There are, of course, n multiple ways to implement this requirement.
The whole process is the user at the time of landing, if you choose to remember the password or a week to avoid the landing, such as this option, then after the user successfully logged in the operation is completed, store an automatic login to implement the cookie data into the database user table, as the next automatic login validation. Verify that it is logged on automatically, otherwise you will need to enter your username and password to log in. The saved cookie value can take a random code.
The sample code is as follows:
Copy the Code code as follows:
$username =trim ($_post[' username ');
$password =md5 (Trim ($_post[' password '));
$ref _url=$_get[' Req_url '];
$remember =$_post[' Remember '];//is automatically logged in
$err _msg= ';
if ($username = = "| | $password = = ") {
$err _msg= "User name and password cannot be empty";
}else{
$row =getuserinfo ($username, $password);
if (empty ($row)) {
$err _msg= "User name and password are not correct";
}else{
$_session[' User_info ']= $row;
if (!empty ($remember)) {//If the user chooses, log the login status and put the username and secret password into 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 you visit each page of the site, you must first check the following functions.
Copy the Code code as follows:
Check if the user is logged in
function Checklogin () {
if (Empty ($_session[' user_info ')) {//check to see if the SESSION is empty
if (Empty ($_cookie[' username ')) | | Empty ($_cookie[' password ')) {//If the session is empty and the user does not have the option to log the login form
Header ("location:login.php?req_url=". $_server[' Request_uri '));//go to the login page, record the URL of the request, log in to the past, and the user experience is good.
}else{//user has chosen to remember login status
$user =getuserinfo ($_cookie[' username '],$_cookie[' password ');//access to user's profile
if (empty ($user)) {//username password not to fetch information, go to login page
Header ("location:login.php?req_url=". $_server[' Request_uri ');
}else{
$_session[' User_info ']= $user;//username and password right, put the user's personal data into the SESSION
}
}
}
}
PHP Next Automatic Login
To determine the user login is generally based on a cookie to judge, so this implementation is generally based on the time to set a cookie, first determine whether to remain logged on, if you keep the cookie at the time of setting a longer. How long you want to be able to set, can be a day, one months, a year can.
Setcookie ("TestCookie", $value, Time () +3600);
where time () +3600 is the term, which means to remain logged on for one hours from now on.
How to remember user name and password in PHP
Learn technology session and Cookie is specifically addressed to the problem to be saved in the local cookie
http://www.bkjia.com/PHPjc/907280.html www.bkjia.com true http://www.bkjia.com/PHPjc/907280.html techarticle PHP Implementation Remember password next automatic login example, remember password automatically login to do the site often encounter to realize remember Password, the next automatic login, a week free landing, a ...