The method of storing user ID and password to MySQL database in PHP _php instance

Source: Internet
Author: User

To create a user information table:

Copy Code code as follows:

CREATE TABLE Tbl_auth_user (
USER_ID VARCHAR (Ten) is not NULL,
User_password CHAR (not NULL),
PRIMARY KEY (user_id)
);
INSERT into Tbl_auth_user (user_id, User_password) VALUES (' Theadmin ', password (' Chumbawamba '));
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 example above. We just need to modify the login process a bit.
Logon scripts:
Copy Code code as follows:

<?php
We must never forget to start the session
Session_Start ();
$errorMessage = ';
if (Isset ($_post[' Txtuserid ')) && isset ($_post[' Txtpassword ')) {
Include ' library/config.php ';
Include ' library/opendb.php ';
$userId = $_post[' Txtuserid '];
$password = $_post[' Txtpassword '];
Check that the user ID and password combination exist in the database
$sql = "Select user_id
From Tbl_auth_user
WHERE user_id = ' $userId '
and User_password = password (' $password ');
$result = mysql_query ($sql)
Or Die (' Query failed. ' . Mysql_error ());
if (mysql_num_rows ($result) = = 1) {
Sessionthe set the user ID and password match,
Setting up a session
$_session[' db_is_logged_in '] = true;
After logging in, we go to the homepage
Header (' Location:main.php ');
Exit
} else {
$errorMessage = ' Sorry, wrong user Id/password ';
}
Include ' library/closedb.php ';
}
?>

/ /... Same HTML login form as before an 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 variables 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 these two

Copy Code code as follows:

<?php
Session_Start ();
is a visit to this page login?
if (!isset ($_session[' db_is_logged_in '))
|| $_session[' db_is_logged_in ']!== true) {
No login, return to login page
Header (' Location:login.php ');
Exit
}
?>

/ /... Here's some HTML code
Copy Code code as follows:

<?php
Session_Start ();
Set session if user is logged in
if (Isset ($_session[' db_is_logged_in ')) {
unset ($_session[' db_is_logged_in '));
}
Now that the user is logged in,
Go to login page
Header (' Location:login.php ');
?>

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.