Implementation code for using cookies to save user login information in tracking cookie PHP

WBOY
Release: 2016-07-29 08:48:16
Original
1500 people have browsed it

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 ') ;
?>


2. Login page: logIn.php

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);
}
?>


Mismatch - Log In



Msimatch - Log In



if(empty($_COOKIE['user_id'])){
echo '

'.$error_msg.'

';
?>
< ;!-- $_SERVER['PHP_SELF'] calls its own php file when submitting a form on behalf of the user-->


Log In


value="




< /fieldset>




}
?>



Rendering:

 php中使用cookie来保存用户登录信息的实现代码

3. Login page: logged.php

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.*/
?>


Rendering:

 php中使用cookie来保存用户登录信息的实现代码

4. Log out cookie page: logOut.php (redirect to lonIn.php after logging out)

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template