Home > Backend Development > PHP Tutorial > How to store user ID and password in mysql database in php_PHP tutorial

How to store user ID and password in mysql database in php_PHP tutorial

WBOY
Release: 2016-07-21 15:13:55
Original
1039 people have browsed it

Create user information table:

Copy code The code is as follows:

CREATE TABLE tbl_auth_user (
user_id VARCHAR( 10) NOT NULL,
user_password CHAR(32) 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 in Created in the above example. We just need to modify the login process a little bit.
Login script:
Copy code The code is as follows:

// We must always Don't 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 exists in the database ASSWORD('$password ')";
$result = mysql_query($sql)
or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {
// sessionthe sets the user id and password to match,
// Set the 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 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. This is the code for these two

Copy the code

The code is as follows: session_start() ;
//Is it a login to visit this page?
if (!isset($_SESSION['db_is_logged_in'])
|| $_SESSION['db_is_logged_in'] !== true) {
// Not logged in, return to the login page
header('Location: login.php');
exit;
}
?>


/ / …Here are some html codes


Copy code
The code is 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 is logged in,
// Go to the login page
header('Location: login.php');
?>




http://www.bkjia.com/PHPjc/326440.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/326440.htmlTechArticleCreate user information table: Copy the code as follows: CREATE TABLE tbl_auth_user ( user_id VARCHAR(10) NOT NULL, user_password CHAR (32) NOT NULL, PRIMARY KEY (user_id) ); INSERT INTO...
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