PHP Cookie不工作 [英] PHP Cookie doesn't work

查看:127
本文介绍了PHP Cookie不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试访问我刚在同一域中的其他网页中设置的Cookie,但无法正常工作。当我执行 echo $ _COOKIE 时,数组在新页面上为空,但包含创建页面上的cookie。

I'm trying to access a cookie I just set in an other page on the same domain, but it doesn't work. When I'm doing echo $_COOKIE, the array is empty on the new page, but contains the cookie on the creation page.

以下是/PROC/LOGIN.PROC.PHP中的代码

Here is the code in /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中的代码

Here's the code in /INC/HEADER.INC.PHP

if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) {
include("pages/user.header.pages.php");

但是当我试图isset cookie或只显示数组在header.inc.php ,数组为空。

But when I'm trying to isset the cookie or only display the array in header.inc.php, the array is empty.

推荐答案

您需要设置 path Cookie的值存储到您的域的根目录,按照文档

You need to set the path value of the cookie to the root of your domain, as per the docs:

setcookie("username", $username, time()+365*24*60*60, '/');

否则,它将被设置为当前工作目录,即 PROC / 。因此,只有 / PROC / 中的脚本才能使用该cookie。

Otherwise, it will be set to the current working directory, which is /PROC/ for your example. So, only scripts in /PROC/ would be able to use that cookie.

这篇关于PHP Cookie不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆