【SESSION】VS 【COOKIE】 --------PHP

【Cookie】

1. Define -> exist in the browser, even when the browser is closed or the connection is interrupted, the user can still maintain the data state;

2. Life cycle -> By default, during the browser session (data is stored in memory), the validity period is set, and the data is stored on the hard disk;

3. Valid path -> need to set valid path path, the default path is the current path, if all paths need to be set to '/';

4. Valid domain name -> set the domain name domain, indicating that the path under the domain name is valid, the default current path of the current domain name;

5.Cookie deletion -> directly use setcookie("name",time()-1);

6. When does the cookie take effect -> setcookie() function, it will send a cookie value to the client, if it does not refresh or browse the next page, the client will not return the cookie;

7. Number of cookies -> A browser supports up to 30 cookies, and the capacity of each cookie cannot exceed 4KB, and the number of cookies set by each website cannot exceed 20;


【Session】

1. How to use -> use session_start() to initialize , create or load a session_id;

2. Life cycle -> The user accesses the page until the connection with the website is disconnected . Each time the connection is made, a SESSION ID will be automatically generated to represent the current user and other users;

3. Pass data -> You can pass data between different pages when there is no disconnection

4. In the PHP environment, the session data storage place -> here is more complicated. I directly copy a paragraph. Generally speaking, it is to configure the storage path according to the configuration file. According to the id command file name, single file storage or hierarchical storage, the original words are as follows :

Where is the SESSION data stored?

  On the server side of course, but not in memory, but in a file or database. By default, the SESSION saving method set in PHP.ini is files (session.save_handler = files), that is, SESSION data is saved by reading and writing files, and the directory where the SESSION file is saved is specified by session.save_path, and the file name is sess_ prefix, followed by the SESSION ID, for example: sess_c72665af28a8b14c0fe11afe3b59b51b. The data in the file is the serialized SESSION data. If the amount of access is large, there may be more SESSION files. At this time, you can set the hierarchical directory to save the SESSION files, and the efficiency will be improved a lot. The setting method is: session.save_path="N;/save_path", N is the hierarchical The number of stages, save_path is the starting directory. When writing SESSION data, php will get the SESSION_ID of the client, and then find the corresponding SESSION file in the specified SESSION file storage directory according to the SESSION ID, create it if it does not exist, and finally write the data to the file after serialization 【3】. Reading SESSION data is also a similar operation process. The read data needs to be deserialized to generate corresponding SESSION variables.


Precautions:

 1. There cannot be any output before using the session_start() function;

2. The session will not be deleted when the browser is closed. But the default expiration time for cookies with session IDs is session level. That is, when the user closes the browser, the session ID stored on the client side will be lost, but the session data stored on the server side will not be deleted immediately. From the client's point of view, the browser, it seems that the session has been deleted (because we lost the session ID and cannot find the original session data).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325484555&siteId=291194637