How can the PHP session duration be set to last for a specific period, such as 4 hours or more?

To set the PHP session duration to last for a specific period, such as 4 hours or more, you can adjust the session cookie lifetime and the session garbage collection settings. By default, PHP sessions last until the browser is closed, but you can extend this duration by changing the session cookie lifetime and the session garbage collection settings in your PHP configuration.

// Set session cookie lifetime to 4 hours (in seconds)
ini_set('session.cookie_lifetime', 14400);

// Set session garbage collection probability to 0 to prevent automatic session expiration
ini_set('session.gc_probability', 0);
ini_set('session.gc_divisor', 1);