doupiao9318 2011-07-13 13:18
浏览 66

setcookie第二次没有工作

I am working on a script which allows for a switch between mobile and desktop view of my website. 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.

  • 写回答

3条回答 默认 最新

  • duanqia9034 2011-07-13 13:26
    关注

    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.

    评论

报告相同问题?