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 ');
?>