Use cookies to save page login information
1. Database connection configuration page: connectvars.php
Copy code The code is as follows:
//Database location
define('DB_HOST', 'localhost ');
//Username
define('DB_USER', 'root');
//Password
define('DB_PASSWORD', '19900101');
//Database name
define('DB_NAME','test ') ;
?>
Copy code The code is as follows:
//Insert related information about connecting to the database
require_once 'connectvars. php';
$error_msg = "";
//Determine whether the user has set a cookie. If $_COOKIE['user_id'] is not set, execute the following code
if(!isset($_COOKIE['user_id'])) {
if(isset($_POST['submit'])){//Determine whether the user has submitted the login form, if so, execute 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 perform one-way encryption of strings
$query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND "."password = SHA ('$user_password')";
//Query with user name and password
$data = mysqli_query($dbc,$query);
//If the found record is exactly one, set COOKIE and reset the page at the same time Orientation
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 found 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 logged in page
$home_url = 'loged.php';
header('Location: '.$home_url);
}
?>
'.$error_msg.'
';Copy the code The code is as follows:
//Logged in page, showing the login user name
if(isset($_COOKIE['username '])){
echo 'You are Logged as '.$_COOKIE['username'].'
';
//Click "Log Out" and go to the logOut.php page to set cookies Log Out
echo ' Log Out('.$_COOKIE['username'].')';
}
/**In the logged-in page, you can use the user's cookies such as $_COOKIE['username'],
* $_COOKIE['user_id'] to query the database, and you can do many things.*/
?>
Copy the code The code is as follows:
/**cookies logout page*/
if(isset($_COOKIE['user_id'])){
//Set the expiration time of each cookie to a time in the past so that they Deleted by the system, the time is in seconds
setcookie('user_id','',time()-3600);
setcookie('username','',time()-3600);
}
//location header Make the browser redirect to another page
$home_url = 'logIn.php';
header('Location:'.$home_url);
?>
The above introduces the implementation code of using cookies to save user login information in tracking cookie PHP, including the content of tracking cookies. I hope it will be helpful to friends who are interested in PHP tutorials.