Application of PHP cookie session and Analysis of User Automatic Login Function

  • 2021-12-12 04:00:27
  • OfStack

In this paper, the use of PHP, cookie and session and the realization method of user automatic login function are described with examples. Share it for your reference, as follows:

Use of cookie


// Generate cookie
// Note: setcookie()  Function must be located in the  <html>  Before the label. 
//setcookie(name, value, expire, path, domain);  // Name, value, expiration time, valid path, valid domain name 
//path , optional; If the path is set to  "/" , then  cookie  Will be valid throughout the domain name . If the path is set to  "/test/" , then  cookie  Will be in  test  Directory and all its subdirectories. The default path value is  cookie  The current directory in which. 
//domain , optional; In order to make  cookie  In  example.com  Valid in all subdomains of, you need to put  cookie  The domain name of is set to  ".example.com" . When you put  cookie  The domain name of is set to  www.example.com  When, cookie  Only in  www  Valid in subdomain name. Default current domain name. 
setcookie("user", "Alex Porter", time()+3600);
setcookie("userid", "1000569852", time()+3600);
// Get 
var_dump($_COOKIE);
// isset()  Function to verify that the  cookie : 
if (isset($_COOKIE["user"])){
 echo "Welcome " . $_COOKIE["user"] . "!<br />";
}
else{
 echo "Welcome guest!<br />";
}
// When deleting  cookie  You should change the expiration date to a past point in time. 
setcookie("user", "", time()-3600);

Use of session


//session Specific: It can be used by all pages in the application; Session information is temporary and will be deleted after the user leaves the website. 
//  Virtual host user.   Settings 1 Storage directory , Read and write permissions required 
$savePath = getcwd().'/session_save_dir/';
//echo $savePath;
session_save_path($savePath);//session_start  Before opening. 
session_id('phpjianlong'); // session_start()  Called before the function  session_id(); Naming method identical variable 
session_start();
// Storage and value taking 
$_SESSION['views']=123456789;
$_SESSION['name']='php Jianlong ';
$_SESSION['array']=array('a','b','c','d');
echo "Pageviews=". $_SESSION['views'];
var_dump($_SESSION);
// Judge whether it exists or not 
if(isset($_SESSION['views'])){
 $_SESSION['views']=$_SESSION['views']+1;
}
else{
 $_SESSION['views']=1;  // There is no assignment 
}
echo "Views=". $_SESSION['views'];
//session Deletion of 
unset($_SESSION['name']);
var_dump($_SESSION);
// Note: session_destroy()  Will reset  session You will lose all stored  session  Data. 
session_destroy();  // Without parameters 
session_start();  // Open again session Yes, reinitialize $_SESSION Array; 
var_dump($_SESSION);
// Get / Settings   Current session  ID . ; The browser automatically generates a PHPSESSID Adj. cookie
echo session_id();
session_id('phpjianlong'); // session_start()  Called before the function  session_id(); Naming method identical variable 

Differences between session and cookie:

1. session is in the server and cookie is stored in the browser
2. session can store arrays, cookie value can only be strings
3. session cannot set the validity period, while cookie can set the validity period
4. session judges user information based on cookie, disables cookie, and session is affected and cannot be used. You can also manually pass values through URL and hide form passes Session ID. Save Session ID in the form of files and databases,

URL is shaped like: http://www.openphp.cn/index.php? PHPSESSID = bba5b2a240a77e5b44cfa01d49cf9669

Realization of User Automatic Login


// Method 1 :  Cookie , in  Cookie  Save the user name and password in the  md5  Encrypted string), and validate it every time the page is requested. If the user name and password are stored in the database, execute it every time 1 Secondary database query, causing redundant burden to the database. Because the client  Cookie  The information in is possible to be viewed and modified by users. It is not safe to discard this method. 
// Method 2 : session , use cookie Long-term preservation session id ; 
// Find this in the system temporary folder  Session  Documents, 1 The file name is as follows: sess_4c83638b3b0dbf65583181c2f89168ec , followed by  32  Bit-encoded random string. Open it with an editor and see 1 Below its contents: 
// Variable name | Type : Length : Value ;
// Settings  Session  Survival time of: 
session_start();
//  Save 5 Days 
$lifeTime = 5 * 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");
// After that, the browser enters the corresponding website address here, and the server obtains it cookie Preserved sessionid ; According to sessionid The content of the judgment, the realization of automatic login. 

For more readers interested in PHP related content, please check the topics of this site: "Summary of cookie Usage in PHP", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills" and "Summary of php String (string) Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: