How can we retrieve the value of a cookie in PHP?
To access the value of a cookie, you can use the $_COOKIE global variable to access and manipulate cookies.
Here is an example of how to retrieve a cookie value:
// 获取单个cookie的值
$cookieValue = $_COOKIE['cookie_name'];
// 检查cookie是否存在
if(isset($_COOKIE['cookie_name'])) {
$cookieValue = $_COOKIE['cookie_name'];
} else {
// cookie不存在的处理逻辑
}
// 获取所有cookie的值
foreach($_COOKIE as $name => $value) {
// 处理每个cookie的逻辑
}
Caution: when using the $_COOKIE global variable, make sure to obtain the cookie value after setting the cookie. Otherwise, the $_COOKIE variable will retain the cookie value from the previous request, instead of the latest set cookie value.