Home  >  Article  >  Backend Development  >  How to use session and cookie at the same time in php to save user login information_PHP tutorial

How to use session and cookie at the same time in php to save user login information_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:18948browse

Use session and cookie at the same time to save user login information
1. Database connection configuration page: connectvars.php

Copy code The code is as follows:

//The location of the database
define('DB_HOST', '127.0.0.1');
// 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 relevant information about connecting to the database
require_once 'connectvars.php';
//Start a session
session_start();
$ error_msg = "";
//If the user is not logged in, that is, when $_SESSION['user_id'] is not set, execute the following code
if(!isset($_SESSION['user_id'])){
if(isset($_POST['submit'])){//Execute the following code when the user submits the login form
$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)){
                                                                                                                                                                                                                                                                                       ; "."password = SHA('$user_password')";
                                                                                                                                                                                                                           ; Then set SESSION and COOKIE and perform page redirection at the same time
if(mysqli_num_rows($data)==1){
]=$ row['user_id'];
                      $_SESSION['username']=$row['username']; 60*24*30));
                                                                                                                                                 . ';
                    header('Location: '             ); 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
/css" href="style.css" />
                                                                                -Judge by $_SESSION['user_id']. If the user is not logged in, the login form will be displayed, allowing the user to enter the user name and password-->
                                                                                                                                                                                                                                           . 'user_id'])){
                                                                                                                                                                                                                                                                                           _Server ['PHP_SELF'] means when the user submits the form, calling its own pHP file-& gt;
& lt; form method = "post" action = "& lt;? Php echo $ _Server ['php_seld'];? & Gt; ">
" 🎜>                                                 value="" />
/>
                                                                      
//Use cookie to assign value to session
; }
}
//Use a session variable to check the login status

if(isset($_SESSION['username'])){ echo 'You are Logged as '.$ _SESSION['username'].'
';
echo ' Log Out('.$_SESSION['username'].')< /a>';}
/**In the logged in page, you can use the user's session such as $_SESSION['username'], * $_SESSION['user_id'] to query the database, and you can do many things.*/
?>


4. Logout session and cookie page: logOut.php (redirect to lonIn.php after logout)





Copy code

The code is as follows:

/**Log out the session and cookie pages at the same time*/
//Even when logging out, you must start a session first to access session variables
session_start();
//Use a session variable to check login status
if(isset($_SESSION['user_id'])){
//To clear the session variable, set the $_SESSION super global variable to an empty array
$_SESSION = array();
//If there is a session cookie, delete it by setting the expiration time to 1 hour before
if(isset($_COOKIE[session_name()])){
setcookie(session_name(),'',time()-3600);
}
//Use the built-in session_destroy() function to call to cancel the session
session_destroy();
}
//At the same time, set the expiration time of each cookie to a time in the past so that they will be deleted by the system. The time is in seconds
setcookie('user_id','',time()-3600);
setcookie('username','',time()-3600);
//The location header causes the browser to redirect to another page
$home_url = 'logIn.php';
header ('Location:'.$home_url);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328072.htmlTechArticleUse session and cookie at the same time to save user login information 1. Database connection configuration page: connectvars.php Copy the code as follows : ?php //Database location define('DB_HOST', '127...
Previous article:A brief analysis of the solution to Mysql data rollback error_PHP tutorialNext article:A brief analysis of the solution to Mysql data rollback error_PHP tutorial

Related articles

See more