PHP Section and Cookie usage summary. Difference between SESSION and COOKIE: Session stores the information on the server. after receiving the unique SESSION_ID, the server obtains the relevant data based on the ID and then transmits the information to the client (browser
Difference between SESSION and COOKIE:
Session stores the information on the server. after receiving the unique SESSION_ID, the server obtains the relevant data based on the ID and then transmits the information to the client (browser ).
Cookie stores all information in local files as text and is managed and maintained by the client (browser.
Because Session data is stored on the server, remote clients cannot modify the data, while cookies are stored locally on the client, which is easy to obtain and tamper with. Therefore, Session security is high.
SESSION introduction:
After PHP5, the session is set as a global variable and can be obtained through $ _ SESSION [session_id. when the page starts a SESSION, a SESSION with a unique id is generated on the server, which is valid until the end of the lifecycle. when the web page is closed or the lifecycle ends, the session is automatically canceled on the server.
To create a session, follow these four steps:
A) start the session ..
There are two methods: bool session_star (); boolean session_register (string name );
B) register the session.
After the SESSION variable is started, it is saved in the $ _ SESSION array. it is easy to create a SESSION variable through the array $ _ SESSION. you only need to add a variable to the array.
C) use session:
General operation: Determine whether the SESSION corresponding to session_id exists. If no SESSION exists, use $ _ SESSION [session_id.
D) delete a session.
Unset ($ _ SESSION [session_id]); // deletes a single SESSION.
Unset ([$ _ SESSION]); // if you delete the entire SESSION, the entire SESSION function is disabled and cannot be recovered.
$ __Session = array (); // delete the entire object and restore it.
Session_destroy (); // ends the current session ..
SESSION lifecycle:
1. how to control the session lifecycle:
I. prerequisites: the client supports cookies!
Method:
A> it is completed through Session.
Void session_set_cookie_params (int lifetime [, string path] [, string domain] [, bool secure]);
Bool setcookie (string name [, string value] [, int expire] [, string path] [, sting domain] [, secure]);
B> use cookies.
2. prerequisites: the client does not support cookies!
Once the client does not support cookies, the SESSION cannot be transmitted between pages. solution:
A>. prompt the user to open the cookie before login.
B>. form POST/GET to pass SESSION_ID.
C>. set session. use_trans_sid = 1 in the php. ini file or enable-trans-sid during compilation;
D>. pass SESSION_ID through a file or database;
SESSION performance optimization on the server:
If all user sessions are saved to the temporary directory on the server, the security and efficiency of the server are reduced.
Solution: store the SESSION in a database or temporary file on the server.
Temporary files:
String session_save_path (string path );
Database:
When the session is stored in a temporary file, when the website views are large, the query Session efficiency will be very low, and the database storage format is recommended.
Bool session_set_save_handler (string open, string close, string read, string write, string destroy, string gc); // store session_id with functions in the database.
SESSION cache:
Cache stores some information on the page in the folder specified by the client, and you can set a certain validity period. (accessed and stored for the first time) during this validity period, when you access the page again, you can directly read the content from the cache to improve page browsing efficiency.
String session_cache_limiter (string cache_limiter); // cache function.
Int session_cache_expire ([int new cache_expire]); // valid time.
COOKIE introduction:
Cookie is a mechanism for remote clients to store data and track and identify users. it is a text file for local users to temporarily store data on the server. The text file format is: "User name @"
Common COOKIE functions:
A) record some information about the visitor. B) pass variables between pages. c) store the page content in cookies to increase the next access speed.
Use cookies with caution:
A) not all browsers support cookies.
B) data is stored locally in plain text, which is not suitable for sensitive and unencrypted information.
C) different types of browsers have a size limit on the number of cookie files. for example, a maximum of 300 cookie files can be stored, and each file size cannot exceed 4 kB. each domain name supports a maximum of 20 cookie files. if the display is exceeded, it will be deleted randomly.
Create COOKIE:
Bool setcookie (string name [, string value] [, int expire] [, string path] [, sting domain] [, secure]);
Read COOKIE:
$ _ COOKIE [];
Delete COOKIE:
A). setcookie ("id", "", time ()-1); // The value is null and the effective time is smaller than the current time. 0 indicates that the COOKIE is deleted directly.
B) manually delete the files that temporarily store cookies on the client.
After the setcookie () function is executed, the cookie can obtain data only after refreshing:
I personally understand that the session and cookie are transmitted between the client and the server. before the page is displayed, we must obtain the corresponding data. therefore, when obtaining http: // or https: //, session or cookie is required .. when setcookie () is executed, the cookie is stored locally but the address does not contain the cookie .. therefore, you need to refresh the server to send a cookie to the address protocol so that the value can be obtained ..
It is exactly the above session_star (). before setcookie (), the html page cannot have output... so no one dares to recognize the second in cookie or session recognition.
Reset Session stores the information on the server. after the server receives a unique SESSION_ID, it obtains the relevant data based on the ID and then transmits the information to the client (browser...