This article compares the differences between session and cookie in PHP in more detail. Share it with everyone for your reference. The specific analysis is as follows:
1. Storage location
Cookies are saved on the client side, and sessions are saved on the server side in the file system/database/memcache, etc.
2. Security
Because the session is saved on the server side, the security is undoubtedly higher.
3. Network transmission volume
Cookies are transmitted between the client and the server through the network, which will occupy some bandwidth; while the session is saved on the server and does not need to be transmitted.
4. Storage time (life cycle), take 20 minutes as an example
The life cycle of a cookie is cumulative, starting from the time of creation. The life cycle ends after 20 minutes, that is, the cookie is invalid;
The life cycle of session is interval. The time starts from the time of creation. If the session is not accessed within 20 minutes, the session will expire in 20 minutes. If the session is accessed at any time within 20 minutes, the session life cycle will restart.
5. Effective paths of session and cookie
By default, cookies only take effect in the directory of the current file. Generally, you need to set the fourth parameter of setcookie to the root directory, so that the entire website page takes effect; by default, session takes effect in the root directory ( You can know it by looking at the PHPSESSID information of the cookie, or setting it through session.cookie_path in the php.ini file).
I hope this article will be helpful to everyone’s PHP programming design.