PHP determines whether cookies are supported

(*-*)浩
Release: 2023-02-27 12:30:01
Original
2948 people have browsed it

PHP transparently supports HTTP cookies. A cookie is a mechanism that stores data on a remote browser to track and identify users. Cookies can be set using the setcookie() or setrawcookie() functions.

PHP determines whether cookies are supported

Cookie is part of the HTTP header, so the setcookie() function must be called before other information is output to the browser, which is the same as the header() function The restrictions are similar. You can use the output buffer function to delay the output of the script until all cookies or other HTTP headers have been set as required. (Recommended learning: PHP Video Tutorial)

If "C" is included in variables_order, any cookie sent from the client will be automatically included in the $_COOKIE automatic global array. If you want to set multiple values ​​for a cookie variable, you need to add the [] symbol after the cookie name.

According to the setting of register_globals, ordinary PHP variables can be created from cookies. However, relying on this feature is not recommended as this option is usually turned off for security reasons.

Setting new cookie
=============================
<?php
setcookie("name","value",time()+$int);
/*name is your cookie&#39;s name
value is cookie&#39;s value
$int is time of cookie expires*/
?>

Getting Cookie
=============================
<?php
echo $_COOKIE["your cookie name"];
?>

Updating Cookie
=============================
<?php
setcookie("color","red");
echo $_COOKIE["color"];
/*color is red*/
/* your codes and functions*/
setcookie("color","blue");
echo $_COOKIE["color"];
/*new color is blue*/
?>

Deleting Cookie
==============================
<?php
unset($_COOKIE["yourcookie"]);
/*Or*/
setcookie("yourcookie","yourvalue",time()-1);
/*it expired so it&#39;s deleted*/
?>
Copy after login

The above is the detailed content of PHP determines whether cookies are supported. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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