A cookie can be compared to a membership card that a merchant handles for a customer, and all information is stored on the chip of the card. If the customer forgets to bring the membership card next time, or the card is lost, then the customer cannot use the membership again. However, if the merchant handles the membership card for the customer, all the information is stored on the merchant’s server, and the user does not need to put the card on himself. However, due to the large number of membership cards, merchants need customers to provide membership card numbers or identification marks each time a customer comes to consume. Through the card number or identification mark, merchants can inquire about the relevant information and consumption records of the membership card.
Session will save a session identifier (equivalent to the card number of the membership card) created by the server for the user on the client side, called the Session ID, and save the value of the Session variable on the server side (in the file/database).
How to apply PHP Session in PHP development?
That is to say, Session is a server-side technology. Using this technology, the server can create a session file for each user’s browser. Since the session is exclusively for the user’s browser, the user is accessing the server’s web resources. When the user accesses other web resources in the server, the other web resources take out data from the user’s respective session to serve the user.
For PHP developers, it can be understood by the following example.
There are two files, aaa.php and bbb.php, where aaa.php is as follows:
<?php session_start(); // Initialize the session $_SESSION['name'] = "name"; // Stores the name to Session
bbb.php is as follows:
<?php session_start(); echo $_SESSION['name']; // Display Session names
First run aaa.php, then run bbb.php, it will output in the webpage: Zhang San
How to configure PHP Session in PHP environment?
Session setting reference item in php.ini:
Mode for handling session access
session.save_handler = files
Session file storage path
session.save_path = /tmp
The session uses the cookie function, start: 1
session.use_cookies = 1
session.name = PHPSESSID
Auto start switch, 0 off, 1 on
session.auto_start = 0
session uses cookie lifetime in seconds
session.cookie_lifetime = 0
cookie valid domain name
session.cookie_domain = “bkzzz.com”
It is not recommended to set session.gc_probability/session.gc_divisor too small, because the garbage collection of the session requires checking whether each file expires
session.gc_probability = 1
session.gc_divisor = 1000
The expiration time defaults to 24 minutes
session.gc_maxlifetime = 1440