The comparison _php skill of session and Cookie in PHP

Source: Internet
Author: User

This article compares the session in PHP with the cookie difference in detail. Share to everyone for your reference. The specific analysis is as follows:

1, the location of storage

Cookies are saved on the client, session is saved on the server-side file system/database/memcache, and so on.

2. Safety

Session because Save server side, security is undoubtedly higher.

3, Network Transmission volume

Cookies are transmitted over the network between the client and the server, consuming some bandwidth, while sessions are saved on the server side and do not need to be transmitted.

4, Save time (life cycle), take 20 minutes as an example

The life cycle of a cookie is cumulative, calculated from the time it was created, and the life cycle ends after 20 minutes, that is, the cookie is invalid;

The life cycle of the session is interval. The timer starts at the time of creation, and if the session is not accessed within 20 minutes, it expires at 20 minutes. If the session is accessed at any time within 20 minutes, the session's lifecycle will be recalculated.

5, session and the effective path of the cookie

Cookies by default, cookies only take effect in the current file's directory, and you typically need to set the fourth parameter of Setcookie to the root directory, which will take effect on all pages of the site. The session is under the root directory by default (it can be known by looking at the PHPSESSID information of the cookie, or by the Session.cookie_path in the php.ini file).

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.