PHP implementation Remember password automatic login code _php Tutorial

Source: Internet
Author: User
Tags setcookie
PHP implementation Remember password automatic login method more than one, there are two emptyempty, is actually a, that is because the code highlighted the bug. Hope to be of help to everyone.

a check that the user logged in
Copy CodeThe code is as follows:
Check if the user is logged in
function Checklogin () {
if (Emptyempty ($_session[' User_info ')) {//check to see if the SESSION is empty
if (Emptyempty ($_cookie[' username ')) | | emptyempty ($_cookie[' password ')) {//If the session is empty and the user does not choose 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 (Emptyempty ($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
}
}
}
}
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 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
}
}
}
}

When you access each page in the background, you must first check the above

Two, the user submits the login information
When the user fills in the user name and password, submits it here,
Copy CodeThe 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 = "Incorrect verification code";
}elseif ($username = = "| | $password = =") {
$err _msg = "User name and password cannot be empty";
}else{
$row = GetUserInfo ($username, $password);

if (Emptyempty ($row)) {
$err _msg = "username and password are not correct";
}else{
$_session[' user_info '] = $row;
if (!emptyempty ($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");
}
}
}
$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 = "Incorrect verification code";
}elseif ($username = = "| | $password = =") {
$err _msg = "User name and password cannot be empty";
}else{
$row = GetUserInfo ($username, $password);

if (empty ($row)) {
$err _msg = "username 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");
}
}
}

About $ref_url Simple explanation, if: User a access to b.php, but a user is not logged in, jump to the login page login.php, after the login page completed user and password, determined to jump to b.php this page, instead of jumping to a default page Main_ user.php. Because b.php is the page that user A wants to go to, the user experience is a little bit better.

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

Why do this, because if someone else uses your computer, they may be able to browse your privacy, so when the user deliberately point out, it is best to log in the status of the record to cancel out.
Copy CodeThe code is as follows:
Sign 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);
}
}

http://www.bkjia.com/PHPjc/322892.html www.bkjia.com true http://www.bkjia.com/PHPjc/322892.html techarticle PHP Implementation Remember password automatic login method more than one, there are two emptyempty, is actually a, that is because the code highlighted the bug. Hope to be of help to everyone. One, user login ...

  • 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.