This article brings you a detailed introduction (pictures and texts) about cookies and sessions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Cookie technology
Cookie is a browser-side technology that can save data on the browser! Cookie refers to the data saved on the browser!
PHP supports cookie technology! PHP can issue instructions to the browser to save data to the browser!
The browser is responsible for saving data, and php is responsible for controlling what data the browser saves! (php uses cookie technology on the browser)
The cookie data saved on the browser can carry the data and make a request to the server every time the browser requests the server. At this time The script on the server can get this data!
Set cookie variables, add, change, delete
Use internal functions setcookie Complete
Form: setcookie(name, value)
Get cookie variable, read
Use predefined array variable:$_COOKIE
This variable stores all cookie data carried when requesting from the browser!
Each element is a cookie variable data! The subscript is the name, the value is the value!
Using firebug’s network tab:
What did you do when setting up?
In response, the server sends an instruction to add a cookie variable to the browser!
After receiving the instruction, the browser added a cookie data to the browser!
When obtained, it did What?
The browser will carry all cookies of the current site to the server upon request!
php will automatically obtain the cookie carried by the browser and form a $_COOKIE array, which can be used by user scripts!
1. Cookie data can only be string data!
2.setcookie function can add, modify and delete!
If it does not exist, add it; if it exists, modify it!
Delete, you can use the form of leaving the value blank!
3. The expiration period of cookie variables
There is a concept of validity period for cookie data:
Default, temporary cookies. It will be saved until the browser is closed!
At the same time, it is supported to add the third parameter of setcookie to modify the validity period of cookie variables. The validity period is expressed as a timestamp, indicating at which point in time it will expire!
php can obtain the current timestamp through the time() function, and extend the cookie time by increasing time() in increments!
At this time, the server will issue the following instructions to the browser:
Time is represented on the network using Greenwich Mean Time! That time without the concept of time zones! GMT
The browser knows the validity period of the cookie variable!
1 .Cookie has the concept of a valid path
The cookie variable will only take effect in the current directory and its descendant directories!
Set under test/, you can access under test/sub/
Inversely:
That’s because of the cookie:
You can change the effective path of cookie data:
Do it through the fourth parameter of setcookie Modification:
/ indicates that the site root directory is valid! Valid for the entire site!
5. The concept of cookie sub-domain name
Cookies strictly distinguish domain names.
Supports sharing between subdomain names:
Use the fifth parameter to set
Validity period, valid path, valid Subdomain!
6, $_COOKIE cannot capture the cookie variable set by the current script!
#$_COOKIE is all the cookies carried by the browser when requesting!
The current setting will only be used when requesting next time!
session technology, session technology
Scenario:
Cookie problem
Because the data itself is on the browser side:
Data security issues!
Data must always be carried when requesting!
How to solve it? Pay attention to sharing data between multiple requests of the browser!
Put the data on the server side, and at the same time distinguish the data from the browser, and share the data between multiple requests of the browser!
On the server, add a data space for each browser that visits, and then assign different unique identifiers to these data spaces! Assign a unique identifier to each browser. The identifier of the server-side database space should correspond one-to-one to the
requirement. The browser carries the identifier every time it makes a request. At this time, the server can obtain the identifier and use the identifier. The data space is determined, but all requested data processing is completed within the current determined space!
The unique identifier assigned by the server to the browser is stored in the browser's cookie, which ensures that the browser carries it every time it comes!
The server determines the identification for each new browser access (browser without identification), and generates a unique data space on the server!
Directly operate the $_SESSION array to complete the storage and retrieval of session data!
Each session data corresponds to an element in $_SESSION! Operating on elements means operating on session data!
However, session technology, including generating session identifiers, opening up session data space, allocating session identifiers to browsers, etc., all require the support of PHP's session mechanism!
Therefore, you need to enable session support first before you can operate the $_SESSION variable to operate session data!
Open:
session_start();
Operation:
$_SESSION;
Open first and then operate:
Update and delete:
SessionID saved in browser-side cookie:
When the current browser makes a request to the server for the first time, the server cannot determine the identity of the browser
A unique identifier will be regenerated and saved to the browser in the form of a cookie!
The default cookie variable name is: PHPSESSID.
This cookie scalar is also called sessionID!
When the browser has the sessionid cookie variable, subsequent requests will carry the ID and make requests:
The server side is the session data space
By default, PHP will generate a file in the space where the session data is saved! Distinguish which ID it belongs to by the name of the file!
The default is saved in the temporary directory of the server operating system:
Approximate process:
Test:
Use session to complete login ID verification:
The above is the detailed content of Detailed introduction to cookies and sessions (pictures and text). For more information, please follow other related articles on the PHP Chinese website!