PHP Cookies works well on localhost, but it's not working on live server

点点圈 提交于 2019-12-02 00:47:14

Look at both path and domain parameters for the setcookie function. Reference: setcookie @ PHP docs http://php.net/manual/en/function.setcookie.php

Try this to set your cookie:

if ($on_localhost) { // change this
    $domain = '.localhost';
} else {
    $domain = '.webhoster.com'; // change this
}
setcookie(
    'settings',
    serialize($defaultSettings),
    time()+3600*24*30,
    '/',          // this is the path
    $domain       // this is the domain
);

Good luck!

Try this:

setcookie("settings", serialize($defaultSettings), time()+3600*24*30, '/'); // added path

Also, could it be that serialize($defaultSettings) result is too large?

Try exit() after the Location-header.

A Location-header does not prevent a PHP-script from executing further instructions, maybe there is something executed after the header that causes the misbehaviour.

Probably your server time is not correct therefore Cookeis are not working on server.

Try this:

setcookie("settings", serialize($defaultSettings), 0);

Setting expiration to zero will fix your issue in this case. or update your server time.

While applying solutions we get forgot the basic of Cookies.

Cookies are like headers. Like the headers, it should be sent before any output generates. then only it sets successfully. I have struggled a lot for this problem but when i went through the basics this problem got solved quickly.

this syntax will be enough to solve this problem...

setcookie(
'settings',
serialize($defaultSettings),
time()+3600*24*30,
'/'          // this is the path
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!