Setcookie因不同的URL而异

Awa*_*rni 1 php setcookie

嗨,我有设置cookie的问题.我有这样的网址

http://www.myweb.com/series/Setayesh/Part-1
在这个网址,我检查cookie是否被设置

if(isset($_COOKIE['cookiename']))
{
 //Perform some operations
}
else
{
setcookie('cookiename',$value,time()+36000)
}
Run Code Online (Sandbox Code Playgroud)

它适用于所有网址

http://www.myweb.com/series/Setayesh/Part-1
http://www.myweb.com/series/Setayesh/Part-1
http://www.myweb.com/series/Setayesh/Part-1
and so on

但是当网址成为时

http://www.myweb.com/series/Pezeshkan/Part-1
if条件不执行它总是在同一浏览器中的其他条件.这是什么问题?当我设置cookie时,我没有设置任何url然后为什么它对这种情况表现不同.

Jan*_*omä 5

问题是,cookie被设置为您请求的路径.在您的情况下,您将发布路径的cookie

/series/Setayesh/Part-1
Run Code Online (Sandbox Code Playgroud)

因此,如果该路径更改为

/series/Pezeshkan/Part-1
Run Code Online (Sandbox Code Playgroud)

你不能再看到cookie,因为它是为另一条路径发出的.setcookie函数具有第四个参数"path",它允许明确地指定路径.如果将其设置为"/",则cookie将对整个域有效:

setcookie('cookiename',$value,time()+36000, '/')
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题.