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

后端 未结 6 1207
死守一世寂寞
死守一世寂寞 2021-01-21 20:25

Note: This issue is already solved, finally I found that it\'s not cookies problem, the problem is on unserialize() function. The serialized cookie which

相关标签:
6条回答
  • 2021-01-21 20:38

    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
    );
    
    0 讨论(0)
  • 2021-01-21 20:52

    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.

    0 讨论(0)
  • 2021-01-21 20:54

    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.

    0 讨论(0)
  • 2021-01-21 21:00

    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!

    0 讨论(0)
  • 2021-01-21 21:01

    Only initialize the ob_start() method before setcookie(). most of the developer ob_start() method include in config file.

    0 讨论(0)
  • 2021-01-21 21:02

    Try this:

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

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

    0 讨论(0)
提交回复
热议问题