Use cookies in php to save user login information. code _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Php uses cookies to save user login information. Use cookies to save the login information on the page 1. database connection configuration page: connectvars. php copy code :? The location of the php database is define (DB_HOST, localhost). the user name uses cookies to save logon information on the page.

1. database connection configuration page: connectvars. php

The code is as follows:


// Database location
Define ('Db _ host', 'localhost ');
// 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 ';

$ Error_msg = "";
// Determine whether the user has set a cookie. if $ _ COOKIE ['User _ id'] is not set, run the following code:
If (! Isset ($ _ COOKIE ['User _ id']) {
If (isset ($ _ POST ['submit ']) {// determines whether the user has submitted the logon form. if yes, run the following code:
$ 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 ')";
// Query by user name and password
$ Data = mysqli_query ($ dbc, $ query );
// If one record is found, set the COOKIE and redirect the page.
If (mysqli_num_rows ($ data) = 1 ){
$ Row = mysqli_fetch_array ($ data );
Setcookie ('User _ id', $ row ['User _ id']);
Setcookie ('username', $ row ['username']);
$ 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 (empty ($ _ COOKIE ['User _ id']) {
Echo'

'. $ Error_msg .'

';
?>


}
?>



:

3. logon page: loged. php

The code is as follows:


// The logon user name is displayed on the logon page.
If (isset ($ _ COOKIE ['username']) {
Echo 'you are Logged as '. $ _ COOKIE ['username'].'
';
// Click "Log Out" and go to the logOut. php page to Log Out the cookie.
Echo 'log Out ('. $ _ COOKIE ['username'].') ';
}
/** On the logon page, you can use your cookies, such as $ _ cookie ['username'],
* $ _ COOKIE ['User _ id'] queries the database, so you can do a lot of things */
?>


:

4. log out cookie page: logOut. php (redirection to lonIn. php after logging out)

The code is as follows:


/** Cancel cookies page */
If (isset ($ _ COOKIE ['User _ id']) {
// 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 );
?>

Protocol 1. database connection configuration page: connectvars. php code :? Php // database location define ('Db _ host', 'localhost'); // username...

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.