Set PHP Session Duration
You can establish the duration of a session in PHP by using the function session_set_cookie_params(). This function is used to set the cookie parameters of a session, including the duration, path, domain, and more.
Here is a sample code that sets the session expiration time to one hour.
// 设置Session的有效时间为1小时
session_set_cookie_params(3600);
session_start();
In the code above, session_set_cookie_params(3600) sets the session’s expiration time to 3600 seconds, which is 1 hour. You can adjust the parameter value as needed to set a different expiration time.
Additionally, you can set the expiration time of the session using the ini_set() function, as shown below:
// 设置Session的过期时间为1小时
ini_set('session.gc_maxlifetime', 3600);
session_start();
The expiration time set by the ini_set() function is used in the Session garbage collection mechanism, defining the storage time of Session data on the server side. You can adjust the parameter value as needed to set different expiration times.