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

后端 未结 2 848
庸人自扰
庸人自扰 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条回答
  •  萌比男神i
    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).

提交回复
热议问题