How to use session and cookie to save user logon information in php at the same time _ php skills

Source: Internet
Author: User
This article provides a detailed analysis of the implementation code that saves user login information by using session and cookie in php at the same time, for more information, see session and cookie to save user logon information.
1. database connection configuration page: connectvars. php

The code is as follows:


// Database location
Define ('Db _ host', '2017. 0.0.1 ');
// User name
Define ('Db _ user', 'root ');
// Password
Define ('Db _ password', '123 ');
// Database name
Define ('Db _ name', 'test ');
?>


2. logon page: logIn. php

The code is as follows:


// Insert information related to the database connection
Require_once 'connectvars. php ';
// Start a session
Session_start ();
$ Error_msg = "";
// If the user is not logged on, that is, when $ _ SESSION ['User _ id'] is not set, run the following code:
If (! Isset ($ _ SESSION ['User _ id']) {
If (isset ($ _ POST ['submit ']) {// execute the following code when the user submits the logon 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 to encrypt strings in one way.
$ Query = "SELECT user_id, username FROM mismatch_user WHERE username = '$ user_username' AND". "password = SHA ('$ user_password ')";
$ Data = mysqli_query ($ dbc, $ query );
// Query with the user name and password. if the record is exactly one, set the SESSION and COOKIE, and redirect the page at the same time.
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 found is incorrect, the error message is set.
$ 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 has logged on, go directly to the logged on page
$ Home_url = 'loged. php ';
Header ('Location: '. $ home_url );
}
?>


Mismatch-Log In



Msimatch-Log In

If (! Isset ($ _ SESSION ['User _ id']) {
Echo'

'. $ Error_msg .'

';
?>


}
?>



:



3. logon page: loged. php

The code is as follows:


// You must enable the session before using the variable values stored in the session.
Session_start ();
// If the session is not set, check whether the cookie is set.
If (! Isset ($ _ SESSION ['User _ id']) {
If (isset ($ _ COOKIE ['User _ id']) & isset ($ _ COOKIE ['username']) {
// Use cookies to assign values to Sessions
$ _ SESSION ['User _ id'] = $ _ COOKIE ['User _ id'];
$ _ SESSION ['username'] = $ _ COOKIE ['username'];
}
}
// Use a session variable to check the logon status
If (isset ($ _ SESSION ['username']) {
Echo 'you are Logged as '. $ _ SESSION ['username'].'
';
Echo 'log Out ('. $ _ SESSION ['username'].') ';
}
/** On the logon page, you can use the user's session, for example, $ _ SESSION ['username'],
* $ _ SESSION ['User _ id'] queries the database, so you can do a lot of things */
?>


:

4. logOut session and cookie page: logOut. php (redirection to lonIn. php after logOut)

The code is as follows:


/** Cancel the session and cookie simultaneously */
// Even if you log out, you must start the session before accessing the session variable.
Session_start ();
// Use a session variable to check the logon status
If (isset ($ _ SESSION ['User _ id']) {
// To clear the SESSION variable, set the $ _ SESSION Super global variable to an empty array
$ _ SESSION = array ();
// If a session cookie exists, delete it by setting the expiration time to the previous one hour
If (isset ($ _ COOKIE [session_name ()]) {
Setcookie (session_name (), '', time ()-3600 );
}
// Use the built-in session_destroy () function to call and cancel a session
Session_destroy ();
}
// At the same time, set the expiration time of each cookie to a certain time in the past so that they can be deleted by the system, in seconds
Setcookie ('User _ id', '', time ()-3600 );
Setcookie ('username', '', time ()-3600 );
// The location header redirects the browser to another page
$ Home_url = 'login. php ';
Header ('Location: '. $ home_url );
?>

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.