Setcookie Different for Different URLs
Hi I have a problem with set cookie. I have a url like this
http://www.myweb.com/series/Setayesh/Part-1At this url I check if the cookie is set or not by this
if(isset($_COOKIE['cookiename']))
{
//Perform some operations
}
else
{
setcookie('cookiename',$value,time()+36000)
}
It works well for all urls like
http://www.myweb.com/series/Setayesh/P开发者_如何学Cart-1 http://www.myweb.com/series/Setayesh/Part-1 http://www.myweb.com/series/Setayesh/Part-1 and so on
But when url becomes
http://www.myweb.com/series/Pezeshkan/Part-1The if condition doesn't executes it always come in else condition in same browser. What is this problem? When I am setting cookie I am not setting any url then why it behaves different for such condition.
The problem is, that the cookie is set to the path that you are requesting. In your case you are issuing the cookie for the path
/series/Setayesh/Part-1
So if that path changes to
/series/Pezeshkan/Part-1
you cannot see the cookie anymore as it was issued for another path. The setcookie function has a fourth parameter "path" which allows to specify the path explicitely. If you set it to "/" the cookie will be valid for the whole domain:
setcookie('cookiename',$value,time()+36000, '/')
This should solve the issue.
精彩评论