PHP Implementation remember the password automatic login function

Source: Internet
Author: User
Tags setcookie
    1. Check if the user is logged in
    2. function Checklogin () {
    3. if (Emptyempty ($_session[' User_info ')) {//check to see if the SESSION is empty
    4. if (Emptyempty ($_cookie[' username ')) | | emptyempty ($_cookie[' password ')) {//If the session is empty and the user does not choose to log the login form
    5. 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.
    6. }else{//user selected Remember login status
    7. $user = GetUserInfo ($_cookie[' username '],$_cookie[' password '); To fetch the user's personal data
    8. if (Emptyempty ($user)) {//username password not to fetch information, go to login page
    9. Header ("location:login.php?req_url=". $_server[' Request_uri ');
    10. }else{
    11. $_session[' user_info '] = $user; Username and password Right, put the user's personal data into the session
    12. }
    13. }
    14. }
    15. }
Copy Code

Note: When accessing each page in the background, you must first check the above

Second, the user submits the login information when the user fills in the user name and the password, submits here.

  1. /**

  2. Detection of user-submitted login information
  3. link:http://bbs.it-home.org
  4. */
  5. $username = Trim ($_post[' username ');
  6. $password = MD5 (Trim ($_post[' password '));
  7. $validatecode = $_post[' Validatecode ');
  8. $ref _url = $_get[' Req_url ');
  9. $remember = $_post[' remember ');

  10. $err _msg = ";

  11. if ($validatecode!=$_session[' checksum ') {
  12. $err _msg = "Incorrect verification code";
  13. }elseif ($username = = "| | $password = =") {
  14. $err _msg = "User name and password cannot be empty";
  15. }else{
  16. $row = GetUserInfo ($username, $password);

  17. if (Emptyempty ($row)) {

  18. $err _msg = "username and password are not correct";
  19. }else{
  20. $_session[' user_info '] = $row;
  21. if (!emptyempty ($remember)) {//If the user chooses, log the login status and put the username and secret password into the cookie
  22. Setcookie ("username", $username, Time () +3600*24*365);
  23. Setcookie ("Password", $password, Time () +3600*24*365);
  24. }
  25. if (Strpos ($ref _url, "login.php") = = = = False) {
  26. Header ("Location:". $ref _url);
  27. }else{
  28. Header ("location:main_user.php");
  29. }
  30. }
  31. }
  32. $username = Trim ($_post[' username ');
  33. $password = MD5 (Trim ($_post[' password '));
  34. $validatecode = $_post[' Validatecode ');
  35. $ref _url = $_get[' Req_url ');
  36. $remember = $_post[' remember ');

  37. $err _msg = ";

  38. if ($validatecode!=$_session[' checksum ') {
  39. $err _msg = "Incorrect verification code";
  40. }elseif ($username = = "| | $password = =") {
  41. $err _msg = "User name and password cannot be empty";
  42. }else{
  43. $row = GetUserInfo ($username, $password);

  44. if (empty ($row)) {

  45. $err _msg = "username and password are not correct";
  46. }else{
  47. $_session[' user_info '] = $row;
  48. if (!empty ($remember)) {//If the user chooses, log the login status and put the username and secret password into the cookie
  49. Setcookie ("username", $username, Time () +3600*24*365);
  50. Setcookie ("Password", $password, Time () +3600*24*365);
  51. }
  52. if (Strpos ($ref _url, "login.php") = = = = False) {
  53. Header ("Location:". $ref _url);
  54. }else{
  55. Header ("location:main_user.php");
  56. }
  57. }
  58. }

Copy Code

About $ref_url 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. The user experience is better because b.php is the page that user a wants to visit.

Third, when the user exits, clear record login status when the user points out, be sure to clear the current login status, so that malicious people use your login information to destroy OH.

    1. Sign Out
    2. function logout () {
    3. unset ($_session[' user_info ');
    4. if (!emptyempty ($_cookie[' username ')) | | emptyempty ($_cookie[' password ')) {
    5. Setcookie ("username", NULL, Time () -3600*24*365);
    6. Setcookie ("password", NULL, Time () -3600*24*365);
    7. }
    8. }
    9. ?>
Copy Code

OK, about using PHP to remember password, automatic login method introduced, I hope you have something to gain. Scripting Academy, focus on you.

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