开发者

A set cookie is not found with php

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜