In-depth analysis of PHP cookie deletion tutorial-PHP source code

Source: Internet
Author: User
Cookie is a small text file stored locally by the user. We can access it through a browser. Let's take a look at some things about how PHP deletes cookies. Cookie is a small text file stored locally by the user. We can access it through a browser. Let's take a look at some things about how PHP deletes cookies.

Script ec (2); script


To delete a cookie, you must ensure that its expiration time is in the past to trigger the deletion mechanism of the browser.

In php, we can use the setcookie () function to set the Cookie information of the browser.

The common sample code for setting cookies is as follows:

$ Name = 'mycooker'; // cookie name
$ Value = 'codeplayer '; // cookie value
$ Expire = time () + 3600*24*7; // The expiration time is 7 days.
$ Path = '/'; // specifies the path to which the cookie can be used. '/' indicates the root directory of the site. This directory and all subdirectories can access this cookie.

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

?>

However, what should we do if we want to delete the Cookie information. Php does not provide another function specifically used to delete cookies, but directly uses the setcookie () function to delete Cookie information. We only need to change the expiration time to the time between the current time.

// If the cookie has expired, the browser can delete the cookie. This can be any value.
Setcookie ('mycooker', 'codeplayer', time ()-3600 ,'/');

// Or

// Set the expiration time to 0, indicating that the expiration time has expired, which can avoid the consumption of time () and mathematical operations.
Setcookie ('mycooker', 'codeplayer', 0 ,'/');
?>

In addition, we can set the Cookie value to a null string ("") or null, or delete the Cookie.

/* Delete cookie */

Setcookie ('mycooker ','');
// Or
Setcookie ('mycooker', null );
?>

Is that all done? No! If you delete a Cookie directly using an empty string or null, the corresponding cookie may not be deleted.

Of course, there is no error in deleting the Cookie. The error is that we did not specify the path (4th parameters) When deleting the Cookie ). If no path parameter is specified, the path is the directory where the current request URL is located by default. If the path when you set the Cookie is inconsistent with the path when you delete the Cookie, the Cookie cannot be deleted.

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

// Set cookie under path "/"
Setcookie ('mycooker', 'codeplayer', time () + 3600*24*7 ,'/');

// Note: the deletion is invalid because the default path is the current directory, that is, "/abc /"
// Setcookie ('mycooker ','');

// Delete the cookie named "mycookie" in the path "/". The time value can be set at will, even if it has not expired.
Setcookie ('mycooker', '', 0 ,'/');
?>

The following code can be found in the php5.20 linux source code package near ext/standard/head. c line 99th.

If (value & value_len = 0 ){
/*
* MSIE doesn' t delete a cookie when you set it to a null value
* So in order to force cookies to be deleted, even on MSIE, we
* Pick an expiry date 1 year 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 code clearly shows that if (value & value_len = 0), when value_len is 0
Hour
Sprintf (cookie, "Set-Cookie: % s = deleted; expires = % s", name, dt );

The http header used to delete the cookie is sent to the browser.

Finally, we can draw a conclusion that php uses

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

Will delete the cookie, of course, these manuals do not.

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.