In-depth analysis of the PHP delete cookie tutorial

Source: Internet
Author: User
Tags current time setcookie sprintf


To remove a cookie, you need to ensure that its expiration period is in the past in order to trigger the browser's deletion mechanism

In PHP, we can use the Setcookie () function to set the browser's cookie information.

The common example code for setting cookies is as follows:

<?php

$name = ' MyCookie '; Cookie Name
$value = ' Codeplayer '; Cookie value
$expire = time () + 3600 * 24 * 7; Expiration Time 7 days
$path = '/'; Set the path that can use the cookie, '/' to represent the site root, which can be accessed in the directory and in all subdirectories.

Set a cookie
Setcookie ($name, $value, $expire, $path);

?>

But what if we want to delete cookie information? Instead of providing another function to delete cookies, PHP simply uses the Setcookie () function to remove cookie information, and we only need to change the expiration time to the time between the current time.

<?php
The browser can delete the cookie when it is set to expire. can be any value at this time.
Setcookie (' MyCookie ', ' Codeplayer ', Time ()-3600, '/');

Or

Set the expiration time directly to 0, which means that 1970-1-1 (has expired), you can avoid the times () and the consumption of mathematical operations
Setcookie (' MyCookie ', ' Codeplayer ', 0, '/');
?>

In addition, we can set the value of the cookie to an empty string ("") or null, and can also be used to delete cookies.

<?php
/* Delete Cookies */

Setcookie (' MyCookie ', ');
Or
Setcookie (' MyCookie ', null);
?>

Is it over? No! If you delete a cookie directly as if using an empty string or null, it may cause the corresponding cookie to not be deleted.

Of course, there is nothing wrong with this way of deleting cookies, and the wrong is that we don't specify the path (4th parameter) when we delete the cookie. If no path parameter is specified, the path defaults to the directory where the current request URL resides. If you set the path of a cookie that is inconsistent with the path when you delete the cookie, you cannot delete the cookie.

<?php
The current request is: "/abc/cookie.php"

Set cookies under Path "/"
Setcookie (' MyCookie ', ' Codeplayer ', time () + 3600 * 24 * 7, '/');

Note: This deletion is not valid because the default path is the current directory, which is: "/abc/"
Setcookie (' MyCookie ', ');

Delete the cookie named "MyCookie" set under Path "/", at which time value is optional, even if it is not expired.
Setcookie (' MyCookie ', ', ', 0, '/');
?>

The following code can be found near line 99th of the php5.20 Linux source Pack ext/standard/head.c.

if (value && value_len = 0) {
/*
* MSIE doesn ' t delete a cookie when your set it to a null value
* Force cookies to is deleted, even on MSIE, we
* Pick a expiry date 1 and 1 second in the past
*/
time_t t = time (NULL)-31536001;
DT = Php_format_date ("D, d-m-y h:i:s T", sizeof ("D, d-m-y h:i:s T")-1, T, 0 tsrmls_cc);
sprintf (Cookie, "Set-cookie:%s=deleted; expires=%s ", name, DT);
Efree (DT);
} else {
sprintf (Cookie, "Set-cookie:%s=%s", name, value? Encoded_value: "");
if (Expires > 0) {
strcat (Cookie, "; Expires= ");
DT = Php_format_date ("D, d-m-y h:i:s T", sizeof ("D, d-m-y h:i:s T")-1, expires, 0 TSRMLS_CC);
strcat (cookie, DT);
Efree (DT);
}
}

The source is clearly displayed, if (value && value_len = 0), when Value_len is 0
When
sprintf (Cookie, "Set-cookie:%s=deleted; expires=%s ", name, DT);

The HTTP header that deletes the cookie is sent to the browser.

Finally we can draw the conclusion that in PHP use

Setcookie ($cookiename, '); or Setcookie ($cookiename, NULL);

Will delete cookies, of course, not in these manuals.

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.