How to store the user ID and password to the MySQL database

Source: Internet
Author: User
    1. CREATE TABLE Tbl_auth_user (

    2. USER_ID VARCHAR (Ten) is not NULL,
    3. User_password CHAR (+) not NULL,

    4. PRIMARY KEY (user_id)

    5. );

    6. INSERT into Tbl_auth_user (user_id, User_password) VALUES (' Theadmin ', password (' Chumbawamba '));

    7. INSERT into Tbl_auth_user (user_id, User_password) VALUES (' Webmaster ', password (' webmistress '));

Copy Code

We will use the same HTML code to create the login form created in the example above. We only need to modify the login process a bit. Logon script:

  1. We must never forget to start the session

  2. Session_Start ();

  3. $errorMessage = ";

  4. if (Isset ($_post[' Txtuserid ')) && isset ($_post[' Txtpassword ']) {
  5. Include ' library/config.php ';
  6. Include ' library/opendb.php ';

  7. $userId = $_post[' Txtuserid ');

  8. $password = $_post[' Txtpassword ');

  9. Check that the user ID and password combinations exist in the database

  10. $sql = "Select user_id
  11. From Tbl_auth_user
  12. WHERE user_id = ' $userId '
  13. and User_password = password (' $password ') ";

  14. $result = mysql_query ($sql)

  15. Or Die (' Query failed. ' . Mysql_error ());

  16. if (mysql_num_rows ($result) = = 1) {

  17. Sessionthe set User ID and password matching,
  18. Setting up Sessions
  19. $_session[' db_is_logged_in ') = true;

  20. After logging in we go to the homepage

  21. Header (' Location:main.php ');
  22. Exit
  23. } else {
  24. $errorMessage = ' Sorry, wrong user Id/password ';
  25. }

  26. Include ' library/closedb.php ';

  27. }
  28. ?>

Copy Code

/ /... The same HTML login form as in the previous example

Instead of checking the user ID and password for hard-coded information, we query the database if both exist in the database using a select query. If we find a match we set the session variable and move to the home page. Note that the name of the session is prefixed with "db" to make it different from the previous example.

In the next two scripts (main. PHP and logoff. PHP) code is similar to the previous one. The only difference is the session name. This is the code for both of these

    1. Session_Start ();

    2. is a visit to this page login?

    3. if (!isset ($_session[' db_is_logged_in '))
    4. || $_session[' db_is_logged_in ']!== true) {

    5. No login, return to login page

    6. Header (' Location:login.php ');
    7. Exit
    8. }

    9. ?>

Copy Code

/ /... Here are some of the HTML code

    1. Session_Start ();

    2. If the user is logged in, set the session

    3. if (Isset ($_session[' db_is_logged_in ')) {
    4. unset ($_session[' db_is_logged_in ');
    5. }

    6. Now, the user logs in,

    7. Go to login page
    8. Header (' Location:login.php ');
    9. ?>

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