Search code examples
phpsessionsession-cookiesexpired-sessions

PHP Because of my new hosting firm my sessions started to end in half an hour idle time. How to change it with code (without php.ini or htaccess)


enter image description herechecked this link: How to change the session timeout in PHP?
but couldn't solve the issue...

> > $twenty_days = 60 \* 24 \* 20; // 20 days in minutes

session_cache_expire($twenty_days);

session_start();

AND TRIED

ini_set('session.cookie_lifetime', xxx);

ini_set('session.gc_maxlifetime', xxx);

EDIT: Solved with this:

session_set_cookie_params('604800', '/', null, true, true);

// Start the session
session_start(); }

Solution

  • session_set_cookie_params can be used to set session cookie expiration (and other settings) if your host is setting a session.cookie_lifetime value you don't like.

    Call it with your desired settings before session_start().

    session_set_cookie_params(0, '/', null, true, true); // expire session when browser closes
    session_set_cookie_params(86400, '/', null, true, true); // one day lifetime
    session_start();