开发者

setcookie does not work the second time around

I am working on a script which allows for a switch between mobile and desktop view of my webs开发者_StackOverflowite. The way I am getting it done is by placing a cookie on the system when the user click on 'Desktop' and deleting the cookie when the user clicks on 'Mobile'.

The system works the first time around with the cookie being placed and the redirect takes place.My script also read the cookie on index.php to serve the desktop view if the cookie is present. When the user clicks again on 'Mobile', I delete the cookie and the run the USER AGENT check.

The second time around, when I click on 'Desktop' link, the cookie is not set and the script fails to execute. What could be a possible error for this?

Thanks for your time

EDIT: I am creating the cookie by setcookie("mobile", "web",time()+31536000, "/");

Deleting the cookie by setcookie("mobile", "",time()-60, "/");

From the HTTP Headers I can see the Set-Cookie parameter being passed the first time the script executes, but after I delete the cookie and try again, the setcookie parameter is not passed.


1: Have you set the length of the cookie correctly? Ensure it is set into the future using

setcookie("Cookie", $value, time()+3600);

2: I would also recommend, instead of deleting the cookie why not make it true or false. This would probably resolve your error with relation to deletion.

Personally I have never had any problem with setCookie in a similar usage to yours (mobile sites), but I always just use mobileEnabled and then set that to true or false, if it doesn't exist the PHP defaults to whatever they are using, if it does exist it means that client has a preference and uses whatever they have it set to.


First off, you should add your code to your question so we can see how you set the cookie. So I am taking a wild guess and say you have not set the path for the cookie.

From the php.net documentation

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

The fourth parameter is $path and as the default is the current directory that the cookie is being set in. This means, and I have to guess again, if you are in a directory /desktop and you set the cookie it applies only to that directory. Once the redirect takes place and you are in lets guess /mobile your cookie, although set, does not apply. If you want a cookie to be visible across all paths, i.e. the domain, you have set the path as / explicitly. Except, of course, you set the cookie at the root but then you are just lucky.


From php.net:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction).

Have you made shure that you website doesn't generate any output when deleting and writing the new cookie? Check for some (for debugging) echo() statements. I've been running into this as well...

:: EDIT because of comment ::

<?PHP

// write cookie
setcookie("TestCookie", "some value", time()+3600);

// expire coockie (delete)
setcookie("TestCookie", "some value", time()-1);

// write cookie
setcookie("TestCookie", "some value", time()+3600);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜