Use session and cookie to save user login information at the same time
1. Database Connection Configuration page: connectvars.php
Copy CodeThe code is as follows:
Location of the database
Define (' Db_host ', ' 127.0.0.1 ');
User name
Define (' Db_user ', ' root ');
Password
Define (' Db_password ', ' 19900101 ');
Database name
Define (' db_name ', ' test ');
?>
2. Login page: login.php
Copy CodeThe code is as follows:
Inserting information about a connected database
Require_once ' connectvars.php ';
Open a session
Session_Start ();
$error _msg = "";
If the user is not logged in, that is, $_session[' user_id ' is not set, execute the following code
if (!isset ($_session[' user_id ')) {
if (Isset ($_post[' submit ')) {//The following code is executed when the user submits the login form
$DBC = Mysqli_connect (db_host,db_user,db_password,db_name);
$user _username = mysqli_real_escape_string ($dbc, trim ($_post[' username '));
$user _password = mysqli_real_escape_string ($dbc, trim ($_post[' password '));
if (!empty ($user _username) &&!empty ($user _password)) {
The SHA () function in MySQL is used for one-way encryption of strings
$query = "Select user_id, username from mismatch_user WHERE username = ' $user _username ' and '." Password = SHA (' $user _password ') ";
$data = Mysqli_query ($dbc, $query);
Query with user name and password, if the record is exactly one, set session and cookie, and page redirect
if (Mysqli_num_rows ($data) ==1) {
$row = Mysqli_fetch_array ($data);
$_session[' user_id ']= $row [' user_id '];
$_session[' username ']= $row [' username '];
Setcookie (' user_id ', $row [' user_id '],time () + (60*60*24*30));
Setcookie (' username ', $row [' username '],time () + (60*60*24*30));
$home _url = ' loged.php ';
Header (' Location: '. $home _url);
}else{//If the record is incorrect, set the error message
$error _msg = ' Sorry, you must enter a valid username and password to log in. ';
}
}else{
$error _msg = ' Sorry, you must enter a valid username and password to log in. ';
}
}
}else{//If the user is already logged in, jump directly to the already logged in page
$home _url = ' loged.php ';
Header (' Location: '. $home _url);
}
?>
<title>Mismatch-log in</title>
Msimatch-log in
if (!isset ($_session[' user_id ')) {
Echo '
'. $error _msg. '
';
?>
}
?>
:
3. Login page: loged.php
Copy CodeThe code is as follows:
Session must be started before using variable values stored in the session
Session_Start ();
If the session is not set, see if a cookie is set
if (!isset ($_session[' user_id ')) {
if (Isset ($_cookie[' user_id ')) &&isset ($_cookie[' username ']) {
Assign a value to a session with a cookie
$_session[' user_id ']=$_cookie[' user_id '];
$_session[' username ']=$_cookie[' username '];
}
}
Check login status with a session variable
if (isset ($_session[' username ')) {
Echo ' You are logged as '. $_session[' username '. '
';
Echo ' Log out ('. $_session[' username ') ';
}
/** in the login page, you can take advantage of the user's SESSION such as $_session[' username '],
* $_session[' user_id '] query the database, you can do a lot of things * *
?>
:
4. Logout Session and Cookie page: logout.php (redirected to lonin.php after logout)
Copy CodeThe code is as follows:
/** simultaneous logoff of Session and Cookie page */
Even when you log off, you must start the session first to access the session variable
Session_Start ();
Check login status with a session variable
if (Isset ($_session[' user_id ')) {
To clear a session variable, set the $_session Super global variable to an empty array
$_session = Array ();
If there is a session cookie, delete it by setting the expiration time to 1 hours before
if (Isset ($_cookie[session_name ())) {
Setcookie (Session_name (), ", Time ()-3600);
}
Use the built-in Session_destroy () function to invoke the undo session
Session_destroy ();
}
At the same time, the expiry time of each cookie is set to a certain period in the past, so that they are deleted by the system and the time is in seconds.
Setcookie (' user_id ', ', Time ()-3600);
Setcookie (' username ', ', Time ()-3600);
Location header redirects the browser to another page
$home _url = ' login.php ';
Header (' Location: '. $home _url);
?>
http://www.bkjia.com/PHPjc/328072.html www.bkjia.com true http://www.bkjia.com/PHPjc/328072.html techarticle use session and cookie to save user login information 1, Database Connection Configuration page: connectvars.php copy code code as follows: PHP//Database location define (' Db_host ', ' 127 ...