How to set the expiration time for a PHP session?

In PHP, you can set the expiration time of a session using the session_set_cookie_params() function.

// 设置 session 过期时间为 1 小时
session_set_cookie_params(3600);

// 启动 session
session_start();

In the above code, the session_set_cookie_params() function sets the expiration time of the session to 1 hour (3600 seconds). Then, the session is started using the session_start() function.

The expiration time will be applied to the session ID passed through cookies. If the user does not interact with the server within the set time, the session will expire and reauthentication will be required.

bannerAds