cookies and sessions

cookie:

1. What is a cookie?

A cookie is a text file that a web server temporarily stores on a user's hard drive and is subsequently read by a web browser.

2. What is the function of cookies?

Cookies are commonly used in three ways:

①Record some information of the visitor, such as the number of times the user visits the web page, or record the information that the visitor has entered, and can also automatically record the user name of the visitor who logged in the last time.

② Pass variables between pages.

③ Store the viewed Internet pages in the temporary cookie folder, which can improve the speed of future browsing.

3. Create cookies

 

Note: If the cookie does not set an expiration time, the cookie will be valid forever unless it is manually deleted. Set the expiration time of the cookie cookie.setMaxAge();

4. Read and delete cookies

①Use setcookie() to delete cookies, setcookie("name"," ",time()-1), set the second parameter to be empty, and the third parameter to be less than the current system time.

②Delete cookies manually, and delete them directly in the browser's temporary cookies folder.

Remember: if the cookie does not set a time, it means that its life cycle is the browser session time. As long as the browser is closed, the cookie will disappear. This cookie is called a session cookie, which is not saved on the hard disk, but is saved in in memory.

Although cookies can be stored in the client browser for a long time, the browser can permanently store up to 300 cookie files, and each file is up to 4KB, and each domain name supports up to 20 cookies. If the limit is reached, the browser will Cookies are automatically and randomly deleted.

session

Used to store information about user sessions, or to change settings for user sessions. The information held by Session variables is single-user and available to all pages in the application.

Session works by creating a unique id (UID) for each visitor and storing variables based on this UID. The UID is stored in a cookie or transmitted through the URL.

 

Session can be understood as a cookie on the server, which will not disappear when the user clears general data. There are generally three cases of session failure: 
the first is the end of the session,
and the second is that the server sets the expiration time of the session (the default is 24 minutes, 1440 seconds)
The third type clears the characteristic data for the user, such as the sessionID stored in the cookie, which causes the server to think that this is a new user and assigns a new session, thus causing the original session to fail
. In a word, the cookie is stored in the user. Local, session is stored on the server, so cookies can save server resources, but the security is not as good as session, session is more secure, but it will occupy server resources

 

 

Guess you like

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