How to Increase Session Timeout in PHP?

By Hardik Savani December 25, 2023 Category : PHP

Hi Dev,

In this guide, we are going to learn php session timeout increase. If you have a question about how to increase session timeout in php then I will give a simple example with a solution. We will look at an example of how to increase session timeout in php.ini. I’m going to show you about how to set session timeout in php.

What is PHP Session Timeout?

PHP Session Timeout is the duration during which a user's session on a website remains active. When a user accesses a website, a server-side session is initiated to store pertinent data like login details, shopping cart contents, or other information that should persist across various pages. The session remains viable until the user actively logs out or until a predetermined period elapses.

The concept of session timeout denotes the timeframe during which a session stays active before the server automatically terminates it. This mechanism is commonly implemented to safeguard sensitive user data by ending sessions if a user remains inactive for a specific period, thereby preventing unauthorized access.

In PHP, session timeout is controlled by the session.gc_maxlifetime directive in the php.ini file. This directive sets the maximum lifetime of a session in seconds. By default, this value is set to 1440 seconds (24 minutes).

If you want to increase the session timeout, you can do the following:

Method 1: Modify php.ini

1. Locate your php.ini file. The location can vary depending on your server setup.

2. Look for the session.gc_maxlifetime directive in the file.

3. Change the value to the desired session timeout duration in seconds. For example, to set the timeout to 1 hour, you can set it to 3600 seconds.

session.gc_maxlifetime = 3600

4. Save the changes.

5. Restart your web server to apply the changes.

Method 2: Set it in your PHP script

If you don't have access to modify the php.ini file, you can set the session timeout in your PHP script using the ini_set function. Place the following code at the beginning of your script:



/* Set session timeout to 1 hour (3600 seconds) */

ini_set('session.gc_maxlifetime', 3600);

/* Optionally, set session cookie lifetime */

ini_set('session.cookie_lifetime', 3600);

/* Start the session */


This will set the session timeout to 1 hour for that specific script. Note that this method needs to be executed on every page where you want the increased session timeout.

I hope it can help you...

Tags :