PHP Sessions expiry time - keeping session alive for a specific number of minutes/hours/days

后端 未结 2 839
庸人自扰
庸人自扰 2020-12-19 07:24

I have a site that creates a Session for shopping carts.

$_SESSION[\'cart\']=array();

It seems as if the server automatically kills the ses

相关标签:
2条回答
  • 2020-12-19 07:48

    set a cookie and change in you config that the session use cookies

    session_start();
    set_cookie("PHPSESSID", session_id(), time() + 3600 * 2);
    

    this keep alive your session for 2 hours

    0 讨论(0)
  • 2020-12-19 07:49

    Call session_set_cookie_params() before you call session_start() in your scripts:

    $session_lifetime = 3600 * 24 * 2; // 2 days
    session_set_cookie_params ($session_lifetime);
    session_start();
    // ...
    

    From the documentation:

    session_set_cookie_params()
    Set cookie parameters defined in the php.ini file. The effect of this function only lasts for the duration of the script. Thus, you need to call session_set_cookie_params() for every request and before session_start() is called.

    Alternatively, you could update your php.ini file's session.cookie_lifetime directive to 2 days (in seconds).

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