Example of Session login verification in PHP

Source: Internet
Author: User
    1. Login
Copy Code

Second, the Login test page

  1. @mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
  2. Or Die ("Database server Connection Failed");
  3. @mysql_select_db ("test")//Select Database MyDB
  4. Or Die ("database does not exist or is not available");
  5. Get user input
  6. $username = $_post[' username ');
  7. $passcode = $_post[' passcode ');
  8. Execute SQL statement to get the value of session
  9. $query = @mysql_query ("Select Username, userflag from users"
  10. ." where username = ' $username ' and passcode = ' $passcode ' ")
  11. Or Die ("SQL statement execution failed");
  12. Determine if the user exists, the password is correct
  13. if ($row = mysql_fetch_array ($query))
  14. {
  15. Session_Start (); The beginning of the sign session
  16. Determines whether the user's permission information is valid, or if 1 or 0 indicates a valid
  17. if ($row [' userflag '] = = 1 or $row [' userflag '] = = 0)
  18. {
  19. $_session[' username '] = $row [' username '];
  20. $_session[' userflag '] = $row [' Userflag '];
  21. echo "Welcome to login, click here to enter the Welcome screen";
  22. }
  23. else//If permission information is invalid output error message
  24. {
  25. echo "User rights information is incorrect";
  26. }
  27. }
  28. else//If the user name and password are incorrect, the output error
  29. {
  30. echo "User name or password error";
  31. }
  32. ?>
Copy Code

Third, log off the login page

    1. unset ($_session[' username ');
    2. unset ($_session[' passcode ');
    3. unset ($_session[' Userflag ');
    4. echo "Logout success";
    5. ?>
Copy Code

Four, the successful login prompt page

  1. Session_Start ();
  2. if (isset ($_session[' username '))
  3. {
  4. @mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
  5. Or Die ("Database server Connection Failed");
  6. @mysql_select_db ("test")//Select Database MyDB
  7. Or Die ("database does not exist or is not available");
  8. Get session
  9. $username = $_session[' username ');
  10. Execute SQL statement to get Userflag value
  11. $query = @mysql_query ("Select Userflag from Users"
  12. ." where username = ' $username ' ")
  13. Or Die ("SQL statement execution failed");
  14. $row = Mysql_fetch_array ($query);
  15. The information in the current database is compared to the information in the session, and if it is different, the session is updated.
  16. if ($row [' userflag ']! = $_session[' Userflag '])
  17. {
  18. $_session[' userflag '] = $row [' Userflag '];
  19. }
  20. Output a different welcome message based on the value of the session
  21. if ($_session[' userflag ') = = 1)
  22. echo "Welcome administrator". $_session[' username '. " Login System ";
  23. if ($_session[' userflag ') = = 0)
  24. echo "Welcome user". $_session[' username ']. " Login System ";
  25. echo "Logout";
  26. }
  27. Else
  28. {
  29. echo "You do not have permission to access this page";
  30. }
  31. ?>
Copy Code
  • Related Article

    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.