Php method for storing user ID and password to MySQL database _php tutorial

Source: Internet
Author: User
To create a user information table:
Copy CodeThe code is 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 only need to modify the login process a bit.
Logon script:
Copy CodeThe code is as follows:
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 combinations 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 User ID and password matching,
Setting up Sessions
$_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 ';
}
?>

/ /... 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
Copy the Code code as follows:
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 are some of the HTML code
Copy the Code code as follows:
Session_Start ();
If the user is logged in, set the session
if (Isset ($_session[' db_is_logged_in ')) {
unset ($_session[' db_is_logged_in ');
}
Now, the user logs in,
Go to login page
Header (' Location:login.php ');
?>

http://www.bkjia.com/PHPjc/326440.html www.bkjia.com true http://www.bkjia.com/PHPjc/326440.html techarticle Create a User Information table: Copy the code as follows: Create TABLE Tbl_auth_user (user_id VARCHAR) not NULL, User_password CHAR (+) not NULL, Primar Y KEY (user_id)); INSERT into ...

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