How to store user IDs and passwords to the mysql database

Source: Internet
Author: User
How to store user IDs and passwords to the mysql database

  1. Create table tbl_auth_user (

  2. User_id VARCHAR (10) not null,
  3. User_password CHAR (32) 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 '));

We will use the same html code to create the login form created in the above example. We only need to modify the logon process. 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 combination 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 match,
  18. // Set the session
  19. $ _ SESSION ['DB _ is_logged_in '] = true;

  20. // Go to the homepage after logging on

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

  26. Include 'Library/closedb. php ';

  27. }
  28. ?>

//... The same html login form is the same as the previous example.

Instead of checking the hard-coded information of the user ID and password, we will query the database. if the two exist in the database, we will use the SELECT query. If we find a match, we set the session variables and move them to the home page. Note that the session name is prefixed with "db" so that it is different from the previous example.

In the next two scripts (main. Php and logout. Php) the code is similar to the previous one. The only difference is the session name. This is for the two codes

  1. Session_start ();

  2. // Is it a login to access this page?

  3. If (! Isset ($ _ SESSION ['DB _ is_logged_in '])
  4. | $ _ SESSION ['DB _ is_logged_in ']! = True ){

  5. // No logon. return to the logon page.

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

  9. ?>

//... Some html code here

  1. Session_start ();

  2. // If the user has logged on, set the session

  3. If (isset ($ _ SESSION ['DB _ is_logged_in ']) {
  4. Unset ($ _ SESSION ['DB _ is_logged_in ']);
  5. }

  6. // Now, the user logs on,

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

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.