PHP code for reading and writing cookies

WBOY
Release: 2016-07-25 08:45:47
Original
961 people have browsed it

setcookie('mycookie','value');
//Function prototype: int setcookie(string name,string value,int expire,string path,string domain,int secure)
echo($mycookie);
echo($ HTTP_COOKIE_VARS['mycookie']);
echo($_COOKIE['mycookie']);

Delete Cookie
(1) Call setcookie() with only name parameter;
(2) Set the expiration time to time() or time-1;

setcookie('mycookie'); or setcookie('mycookie',''); or setcookie("mycookie",false);
//setcookie('mycookie','',time()-3600);
echo ($HTTP_COOKIE_VARS['mycookie']);
print_r($_COOKIE);

Recommended deletion method:
setcookie('mycookie','',time()-3600);

PHP provides a very useful function mktime().
You just need to pass to mktime() the hours, minutes, seconds, month, date, and year you want to represent in order,
mktime() will return the total number of seconds since January 1, 1970 for the date.
So if you need to simulate the Y2K problem:
$y2k = mktime(0,0,0,1,1,2000);
setcookie('name','value',$y2k);
setcookie('name', 'value', time+3600);
setcookie('name', 'value', $y2k, '~/myhome', '.domain.com');

How to get the COOKIE expiration time
$expire = time() + 86400; // Set the validity period of 24 hours
setcookie ("var_name", "var_value", $expire); // Set a cookie named var_name, and The validity period is set
setcookie ("var_name_expire", $expire, $expire); // Then set the expiration time into the cookie so that you can know the expiration time of var_name

Note:
When sending a cookie, the cookie value is automatically URL encoded. URL decoding occurs on reception.
If you don’t need this, you can use setrawcookie() instead.

php, cookie


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template