我正在尝试访问我刚刚在同一域的其他页面中设置的 cookie,但它不起作用。当我执行 echo $_COOKIE
时,数组在新页面上为空,但包含创建页面上的 cookie。
这是/PROC/LOGIN.PROC.PHP 中的代码
//Set the cookie for 1 year.
setcookie("username", $username, time()+365*24*60*60);
setcookie("password", $password, time()+365*24*60*60);
这是/INC/HEADER.INC.PHP 中的代码
if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
include("pages/user.header.pages.php");
但是当我尝试设置 cookie 或仅在 header.inc.php 中显示数组时,该数组是空的。
最佳答案
您需要将 cookie 的 path
值设置为您域的根目录,as per the docs :
setcookie("username", $username, time()+365*24*60*60, '/');
否则,它将被设置为当前工作目录,在您的示例中为 /PROC/
。因此,只有 /PROC/
中的脚本才能使用该 cookie。
关于PHP Cookie 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677921/