Print_r函数在第一页刷新时不起作用


Print_r function doesn't work on first page refresh

有一个简单的问题。
我有两个文件;index.phpfunctions.php
index.php:

<?php
require 'functions.php';
print_r($_COOKIE['blablabla'])
?>

Cookie在functions.php中初始化。
所以,我要去index.php,只在第二页刷新时看到print_r结果。所以,第一次刷新cookie集,我可以看到它在浏览器的隐私设置,但他们不打印。为什么会这样呢?

由于cookie是在浏览器上设置的,因此最初应该在第一页刷新时设置(来自functions.php的代码)。一旦在浏览器上设置了cookie,那么您就可以访问它了。

在使用setcookie();之后,您必须在同一进程中自己填充$_COOKIE才能访问这些值。

$key = 'variable';
$val = 'value123';
setcookie($key, $val, ...);
$_COOKIE[$key] = $val;

$_COOKIE通常有来自浏览器请求的值。请求在服务器响应之前发生。

Request (without cookie) ----> Response (with set cookie)
----> Second Request (with cookie) ----> Response