How to delete cookies in PHP

Source: Internet
Author: User
Delete Cookies

When a cookie is created, its cookie file is automatically deleted when the browser is closed if it is not set to a valid time. If you want to delete the cookie file saved in the client before closing the browser, there are two ways to do this, and the same method as setting a cookie is to invoke the Setcookie () function to implement the Delete cookie action: First way, omit Setcookie () All parameter columns of the function, using only the first parameter cookie to identify the name parameter, to delete the cookie data of the specified name; the second way, the target cookie is set to the "Expired" state using the Setcookie () function.

1. Use the Setcookie () function to set the target cookie to the "expired" state to delete the cookie

The basic type of delete cookie and the way the cookie is created, and the delete cookie also uses the Setcookie () function. To delete a cookie simply set the second parameter in the Setcookie () function to a null value and set the expiration time of the third parameter Cookie to less than the current time of the system.

Here's the code to set the cookie's expiration time to the current time minus 1 seconds.

Setcookie ("Cookie_name", "", Time ()-1);

In the above code, the time () function returns the current timestamp in seconds, and the current time minus 1 seconds will get past the time, thereby removing the Cookie.

2. Using the Setcookie () function to set the lifetime of a cookie to null by default, the lifetime is the same as the browser, and the cookie is deleted when the browser is closed. Specify only one parameter for the cookie recognition name, which is to delete the cookie data for the specified name in the client.

Setcookie ("Cookie_name");

Note: Setting the expiration time to 0 also allows you to delete the Cookie directly.

The life cycle of a Cookie

If the cookie does not set the expiration time, it means that its lifetime is the duration of the browser session, and the cookie disappears automatically as soon as the browser is closed. This cookie, known as a session cookie, is generally not saved on the hard disk, but in memory.

If the expiration time is set, then the browser will save the cookie to the hard disk, and the browser will remain in effect once again, to guide its expiry date.

Although cookies can be kept in the client browser for a long time, they are not immutable. Because the browser allows a maximum of 300 cookie files, and each cookie file supports a maximum capacity of 4KB, each domain name supports up to 20 cookies, and if the limit is reached, the browser automatically deletes the cookie file randomly.

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.