1. Database Connection Configuration page: connectvars.php
<?php
Location of the database
Define (' db_host ', ' localhost ');
User Name
Define (' Db_user ', ' root ');
Password
Define (' Db_password ', ' 19900101 ');
database name
Define (' db_name ', ' test ');
?>
2. Login page: login.php
<?php
//Inserting information about a connected database
Require_once ' connectvars.php ';
$error _msg = "";
//Determine if the user has set a COOKIE, and if $_cookie[' user_id ' is not set, execute the following code
if (!Isset$_cookie[' user_id ')) {
IfIsset$_post[' Submit ')) {//Determines whether the user submits the login form, and if so, executes 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 for one-way encryption of strings
$query = "Select user_id, username from mismatch_user WHERE username = '$user _username ' and '. " Password = SHA ('$user _password ') ";
//Querying with a user name and password
$data =Mysqli_query ($DBC,$query);
//If the record is exactly one, set the cookie and redirect the page
IfMysqli_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{//Set error message if no record is found
$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>
<link rel= "stylesheet" type= "Text/css" href= "Style.css"/>
<body>
Log in<!--through$_cookie[' user_id ') to determine if the user is not logged in, display the login form, let the user enter the user name and password--
<?php
IfEmpty$_cookie[' user_id ')) {
Echo ' <p class= ' Error > '.$error _msg. ' </p> ';
?>
<!--$_server[' php_self ') when submitting a form on behalf of a user, call itself 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 has lost a user name, echo the user name--
<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>
:
3. Login page: loged.php
<?php
login page, display login user name
if (isset ($_ cookie[' username ')) {
echo ' is logged as '. // Click "Log Out" to go to the logout.php page to log off the cookie
echo ' <a href= "logout.php" > Log out ('. }
/** on the logged in page, you can take advantage of the user's COOKIE such as $_cookie[' Username '],
* $_cookie[' user_id '] query the database, you can do a lot of things */
?
:
4. Logout Cookie page: logout.php (redirected to lonin.php after logout)
<?php
/**/
Span style= "color: #0000ff;" >if (isset ($_cookie[' user_id '])) {
// sets the expiry time of each cookie to a time in the past, causing them to be deleted by the system, in seconds
Span style= "color: #008080;" >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);
?
Use cookies to save page login information