PHP implementation Remember password automatic login _php tutorial

Source: Internet
Author: User
Tags setcookie
To realize the ability to remember password automatic login Most of us are using the client's cookies to achieve, we use PHP is no exception, the need for friends can refer to.

PHP Production Remember password automatic login to solve the idea, in fact, is the operation of Session,cookies
Check if the user is logged in

The code is as follows Copy Code
function Checklogin () {
if (Empty ($_session[' user_info ')) {//check to see if the SESSION is empty
if (Empty ($_cookie[' username ')) | | empty ($_cookie[' password ')) {//if 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, log the URL of the request, log in and jump past, and the user experience is good.
}else{//user selected Remember login status
$user = GetUserInfo ($_cookie[' username '],$_cookie[' password '); To fetch the user's personal data
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
}
}
}
}


Second, the user submits the login information

code as follows copy code
u Sername = 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 = "Incorrect Captcha";
}elseif ($username = = ' | | $password = = ') {
$err _msg = "The user name 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, log the login status and put the username and 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");
}
}
}


Third, when the user points out, clear record log in status


Sign Out

The code is as follows Copy Code
function logout () {
unset ($_session[' user_info ');
if (!empty ($_cookie[' username ')) | |!empty ($_cookie[' password ')) {
Setcookie ("username", NULL, Time () -3600*24*365);
Setcookie ("password", NULL, Time () -3600*24*365);
}
}

http://www.bkjia.com/PHPjc/444728.html www.bkjia.com true http://www.bkjia.com/PHPjc/444728.html techarticle to realize the ability to remember password automatic login Most of us are using the client's cookies to achieve, we use PHP is no exception, the need for friends can refer to. PHP production ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.