使用php找不到设置的Cookie [英] A set cookie is not found with php

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

问题描述

我已成功设置了一个cookie cad ,可以在firefox cookie-search中查看。

  if(isset($ _ COOKIE ['cad'])){
echo'YES';
} else {
echo'NO';
}

// parse NO

我的网站在托管服务器上,要从另一个更改,所以服务器是site.com.test.host.com。这是否会导致调用cookie的问题?



但是这样说是 print_r($ _ COOKIE);

解决方案

设置cookie时,可以设置各种选项。众所周知,Cookie只能通过同一域上的脚本访问,但您也可以影响Cookie所设置的路径。例如, /foo/bar.php 上设置的Cookie可能无法在 /foobar.php 上访问。 / p>

PHP默认将Cookie设置为当前路径。因此,对于上面的例子,cookie被设置为路径 / foo / ,并且不能在该路径之外访问。



因此,当您设置Cookie时,最好明确说明您希望他们在哪里可用。在PHP这是很容易;只需设置一个额外的参数指定路径。正如您在评论中指出的,您需要最自由的路径 / ,这意味着此域上的任何地方。

  setcookie('cad','somevalue',0,'/'); 

请参阅 PHP手册中的 setcookie 文档


I have successfully set a cookie cad and can see it in firefox cookie-search.

if(isset($_COOKIE['cad'])){
echo'YES';
}else{
echo'NO';
}

//parse NO

I am setting up my website on a hosted server, about to change from another so the server is site.com.test.host.com. Does that create problem for calling the cookie?

But talking against that is that print_r($_COOKIE); shows other Cookies on the same test-domain.

解决方案

When you set a cookie, you can set various options. Cookies, as everyone knows, can only be accessed by scripts on the same domain, but you can also affect what path the cookie is set on. For instance, a cookie set on /foo/bar.php may not be accessible on /foobar.php.

PHP by default sets the cookie to the current path. So, with the above example, the cookie is set to the path /foo/, and is not accessible outside that path.

When you set your cookies, therefore, it's best to be explicit about where you want them to be available. In PHP this is very easy; just set an extra parameter specifying the path. As you indicate in the comments, you need the most liberal path possible /, which means "anywhere on this domain".

setcookie('cad', 'somevalue', 0, '/');

See the setcookie documentation in the PHP manual.

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

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