How to store user ID and password in mysql database

WBOY
Release: 2016-07-25 09:05:43
Original
1166 people have browsed it
  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'));

Copy code

We will use the same html code to create the login form created in the above example. We just need to modify the login process a little bit. Login 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. < ;p> $userId = $_POST['txtUserId'];
  8. $password = $_POST['txtPassword'];

  9. // Check that the user id and password combination exists 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 sets the user id and password to match,
  18. // sets the session
  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

/ /…Same html login form as the previous example

Instead of checking the user id and password against hardcoded information we query the database and use a SELECT query if these two exist in the database. If we find a match we set the session variable and move to the home page. Note that the session name is prefixed with "db" making it different from the previous example.

The code in the next two scripts (main.php and logout.php) is similar to the previous one. The only difference is the session name. Here is the code for both

  1. session_start();

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

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

  5. // No login, return to the login page

  6. header('Location: login.php') ;
  7. exit;
  8. }

  9. ?>

Copy code

/ /…some html code here

  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 is logged in,

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

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!