A set cookie is not found with php

后端 未结 1 336
鱼传尺愫
鱼传尺愫 2021-01-23 02:40

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

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

//par         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 03:09

    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 讨论(0)
提交回复
热议问题