Do the site often encounter to realize the password, the next automatic login, a week without landing, one months to avoid this demand. This functionality is generally done through cookies. This article will briefly describe how to use PHP to implement this requirement. Of course there are many n ways to implement this requirement.
The whole process is the user in the landing, if you choose to remember the password or a week without landing, such as this option, then after the user successfully logged the operation completed, store a implementation of the automatic login cookie data to the database user table, as the next automatic login verification. Verify that you are logged in automatically, or you will need to enter a username and password to log in. The saved cookie value can take a random code.
The specific example code is as follows:
Copy Code code as follows:
$username =trim ($_post[' username '));
$password =md5 (Trim ($_post[' password '));
$ref _url=$_get[' Req_url '];
$remember =$_post[' Remember '];//automatically sign in
$err _msg= ';
if ($username = = "' | | $password = = ") {
$err _msg= "User name and password cannot be null";
}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 place the username and encryption 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 you visit each page of the site, you must first check the following function.
Copy Code code as follows:
Check if the user is logged on
function Checklogin () {
if (Empty ($_session[' user_info ')) {//check if 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 logon form
Header ("location:login.php?req_url=". $_server[' Request_uri ');/go to the login page, record the requested URL, log in and jump past, the user experience 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 access information, go to the login page
Header ("location:login.php?req_url=". $_server[' Request_uri '));
}else{
$_session[' User_info ']= $user;//username and password right, put the user's profile in the session
}
}
}
}