Use cookies to save logon information on the page
1. Database Connection configuration page: connectvars. php
Copy codeThe Code is as follows:
<? Php
// 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
Copy codeThe Code is as follows:
<? Php
// 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 );
}
?>
<Html>
<Head>
<Title> Mismatch-Log In </title>
<Link rel = "stylesheet" type = "text/css" href = "style.css"/>
</Head>
<Body>
<H3> Msimatch-Log In <! -- Judge by $ _ COOKIE ['user _ id']. If the user is not logged on, the logon form is displayed, asking the user to enter the user name and password -->
<? Php
If (empty ($ _ COOKIE ['user _ id']) {
Echo '<p class = "error">'. $ error_msg. '</p> ';
?>
<! -- $ _ SERVER ['php _ SELF '] indicates that when a user submits a form, the user calls his/her own PHP file. -->
<Form method = "post" action = "<? Php echo $ _ SERVER ['php _ SELF '];?> ">
<Fieldset style = "width: 250px;">
<Legend> Log In </legend>
<Label for = "username"> Username: </label>
<! -- If the user name has been lost, the user name is displayed. -->
<Input type = "text" id = "username" name = "username"
Value = "<? Php if (! Empty ($ user_username) echo $ user_username;?> "/>
<Br/>
<Label for = "password"> Password: </label>
<Input type = "password" id = "password" name = "password"/>
</Fieldset>
<Br/>
<Input type = "submit" value = "Log In" name = "submit"/>
</Form>
<? Php
}
?>
</Body>
</Html>
:
3. logon page: loged. php
Copy codeThe Code is as follows:
<? Php
// The logon user name is displayed on the logon page.
If (isset ($ _ COOKIE ['username']) {
Echo 'you are Logged as '. $ _ COOKIE ['username'].' <br/> ';
// Click "Log Out" and go to the logOut. php page to Log Out the cookie.
Echo '<a href = "logOut. php"> Log Out ('. $ _ COOKIE ['username']. ') </a> ';
}
/** 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)
Copy codeThe Code is as follows:
<? Php
/** 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 );
?>