php Cookie 在不同页面上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2038760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:45:34  来源:igfitidea点击:

Cookies not working on different pages

phpcookies

提问by David

Ok I have a cookie set, and I can clearly see it if I go to private data in Firefox... ok so when I echo it on one page in a certain directory it works, (www.example.com/dir), but on the index page of the site (www.example.com), it wont echo, it says the cookie is not set. Yes I have cookies enabled, yes I tried clearing cache and all that. Any ideas? PHP btw

好的,我有一个 cookie 集,如果我在 Firefox 中访问私人数据,我可以清楚地看到它......好吧,当我在某个目录的一页上回显它时,它可以工作,(www.example.com/dir),但是在网站(www.example.com)的索引页面上,它不会回显,它说没有设置cookie。是的,我启用了 cookie,是的,我尝试清除缓存等等。有任何想法吗?PHP 顺便说一句

回答by Pekka

Which directory are you in when the cookie gets set?

设置 cookie 时您在哪个目录中?

From the PHP manual on setcookie(), emphasis mine:

关于 setcookie()PHP 手册中,强调我的:

Path

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.

小路

cookie 可用的服务器上的路径。如果设置为“/”,cookie 将在整个域中可用。如果设置为 '/foo/',则 cookie 将仅在 /foo/ 目录和域的所有子目录(例如 /foo/bar/ )中可用。默认值是设置 cookie 的当前目录。

回答by Doug Neiner

Cookies can be bound to a specific domain, subdomain, path, and protocol (http/https). You need to specify the path when setting the cookie in PHP:

Cookie 可以绑定到特定的域、子域、路径和协议 (http/https)。PHP中设置cookie时需要指定路径:

setcookie("TestCookie", "Value", time()+3600 , '/' );

The fourth parameter binds it to the root of the site and it will be available in any subdirectory of the main site.

第四个参数将它绑定到站点的根目录,它将在主站点的任何子目录中可用。

If you want it available on the main domain and any subdomain, supply the fifth parameter like this:

如果您希望它在主域和任何子域上可用,请提供第五个参数,如下所示:

setcookie("TestCookie", "Value", time()+3600 , '/', '.example.com' );

Now it will be readable at:

现在它将在以下位置可读:

www.example.com
example.com/newdir
awesome.example.com/newdir

www.example.com
example.com/newdir
awesome.example.com/newdir

回答by Jacob Relkin

You need to check the path that the cookie is being set.

您需要检查设置 cookie 的路径。

If it's not '/', there's your answer!

如果不是'/',那就是你的答案!

回答by Anirudh Sood

Yes try this, I was also facing this problem but resolved by below code.

是的,试试这个,我也遇到了这个问题,但通过下面的代码解决了。

setcookie("TestCookie", "Value", time()+3600 , '/' );

回答by Erik

Set your path option; the default value is the current directory that the cookie is being set in. Because you're setting the cookie in the directory /dir , its only available within that directory or below it.

设置您的路径选项;默认值是设置 cookie 的当前目录。因为您在目录 /dir 中设置 cookie,所以它只能在该目录中或在它下面。

You get around this by explicitly setting the path, ie.

您可以通过显式设置路径来解决此问题,即。

setcookie(name,value,expire,path,domain,secure) 

Set the path to "/".

将路径设置为“/”。

回答by Robert Ostrowicki

Cookies Must Be Set Before Page Output !!! Since cookies are sent by the script to the browser in the HTTP headers, before your page is sent, they must be set before you even send a single line of HTML or any other page output. The moment you send any sort of output, you are signalling the end of the HTTP headers. When that happens, you can no longer set any cookie. If you try, the setcookie() function will return FALSE, and the cookie will not be sent.

必须在页面输出之前设置 Cookies !!! 由于 cookie 是由脚本在 HTTP 标头中发送到浏览器的,因此在发送您的页面之前,必须在您甚至发送一行 HTML 或任何其他页面输出之前设置它们。当您发送任何类型的输出时,您都在发出 HTTP 标头结束的信号。发生这种情况时,您将无法再设置任何 cookie。如果您尝试,setcookie() 函数将返回 FALSE,并且不会发送 cookie。

回答by JWC May

setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/"); // 86400 = 1 day,  '/' denotes cookie available in entire directory.

and in another page:

并在另一个页面中:

$username = $_COOKIE['cookie_username'];

also make sure that browser is not blocking cookies.

还要确保浏览器没有阻止 cookie。

If you want to use cookies in sub domain also:

如果您还想在子域中使用 cookie:

setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/", ".subdomain.com"); // 86400 = 1 day,  '/' denotes cookie available in entire directory.

回答by Mayank Kumar

setcookie("Cookie_name", "Cookie_Value", time()+3600 , '/' );

fourth parameter ('/') will make your cookies accessible to pages in parent directories.

第四个参数 ( '/') 将使您的 cookie 可被父目录中的页面访问。

回答by Joel L

You need to set the $path to / in setcookie(), if you want to access it in all directories

setcookie()如果要在所有目录中访问它,则需要将 $path 设置为 /in