Example of session logon verification in php

Source: Internet
Author: User
Example of session logon verification in php

  1. Login

II. logon detection page

  1. @ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a database.
  2. Or die ("database server connection failed ");
  3. @ Mysql_select_db ("test") // select the database mydb
  4. Or die ("The database does not exist or is unavailable ");
  5. // Obtain user input
  6. $ Username = $ _ POST ['username'];
  7. $ Passcode = $ _ POST ['passcode'];
  8. // Execute an SQL statement to obtain the Session value
  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 whether the user exists and the password is correct
  13. If ($ row = mysql_fetch_array ($ query ))
  14. {
  15. Session_start (); // indicates the start of the Session.
  16. // Determine whether the user's permission information is valid. if the value is 1 or 0, the permission information is 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 log on. Click here to enter the welcome page ";
  22. }
  23. Else // output error message if the permission information is invalid
  24. {
  25. Echo "incorrect user permission information ";
  26. }
  27. }
  28. Else // if the user name and password are incorrect, an error is returned.
  29. {
  30. Echo "incorrect user name or password ";
  31. }
  32. ?>

3. log out of the logon page

  1. Unset ($ _ SESSION ['username']);
  2. Unset ($ _ SESSION ['passcode']);
  3. Unset ($ _ SESSION ['userflag']);
  4. Echo "logout successful ";
  5. ?>

4. logon success prompt page

  1. Session_start ();
  2. If (isset ($ _ SESSION ['username'])
  3. {
  4. @ Mysql_connect ("localhost", "root", "1981427") // you must connect to the database server before selecting a database.
  5. Or die ("database server connection failed ");
  6. @ Mysql_select_db ("test") // select the database mydb
  7. Or die ("The database does not exist or is unavailable ");
  8. // Obtain the Session
  9. $ Username = $ _ SESSION ['username'];
  10. // Execute the SQL statement to obtain the 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. // Compare the permission information in the current database with that in the Session. if the permission information is different, update the Session information.
  16. If ($ row ['userflag']! = $ _ SESSION ['userflag'])
  17. {
  18. $ _ SESSION ['userflag'] = $ row ['userflag'];
  19. }
  20. // Output different welcome information based on the Session value
  21. If ($ _ SESSION ['userflag'] = 1)
  22. Echo "welcome administrator". $ _ SESSION ['username']. "log on to the system ";
  23. If ($ _ SESSION ['userflag'] = 0)
  24. Echo "welcome". $ _ SESSION ['username']. "log on to the system ";
  25. Echo "logout ";
  26. }
  27. Else
  28. {
  29. Echo "you are not authorized to access this page ";
  30. }
  31. ?>

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.