We will show you how to use cookies and sessions in PHP. There are two important functions: Cookies and Sessions. how are they used and what are the differences?
This article will show you how to use cookies and sessions.
Cookie introduction
Cookie is the data stored in the client browser. we track and store user data through cookies. Generally, the Cookie is returned from the server to the client through HTTP headers. Most web programs support Cookie operations,
Because the Cookie exists in the HTTP header, it must be set before other information output, similar to the use restrictions of the header function.
Cookie setting method
Setcookie ("name", 'hangsan ');
Setcookie ("name", 'hangsan', time () + 60); // Set the cookie validity period to 60 seconds.
// Setcookie ("visittime", date ("y-m-d H: I: s"), time () + 60); // Set the variable for saving the cookie expiration time
Cokie read method
$ Name = $ _ COOKIE ["name "};
Cookie deletion method
Setcookie ("name", "", time ()-1); // Set the cookie () time to the current time minus 1, time () the function returns the current timestamp in seconds. if the expiration time is reduced by 1 second, the previous time is obtained, and the cookie is deleted.
To delete cookiez, you only need to set the second parameter in the setcookie () function to a null value, and set the cookie expiration time of the third parameter to less than the current time of the system.
Finally, let's take a look at the biggest difference between session and cookie:
1. session stores session information on the server and transmits client information through a session ID. after the server receives the session ID, provide related sesion information resources based on this ID
2. cookies store all information on the client as text and are managed and maintained by the browser.
3. since session is stored on the server, all remote users cannot modify the content of the session file, and the cookie
For client-side storage, all sessions are much safer than cookies. of course, there are many advantages, such as easy control and custom storage (stored in the database )...