Increase session timeout in PHP

There are some best ways to increase session timeout in PHP, we will discuss the easy way to do it.

First, we will see about the session in PHP for better understanding.

What is Session In PHP?

The session stores temporary information about a user on the server for later use.
This can be useful if one wants to save information about a specific user such as their name, email, or other details without having it get lost if there is no form submission.

Session in PHP is essentially the act of asking a user if they would like to carry on browsing with the same open tabs or close them.
It’s an easy way to make sure that users don’t have to re-enter their login details
when coming back to your website after a certain period of time so it pays to be aware of more than one PHP session ID!

Starting a session

There is a function in PHP called session_start(). We use it to start a session in our PHP script after starting it we can proceed more in sessions. If the session is already started so it will give a notice error.

Creating a Session Variable

We create session variables to store our data in it and we can use them anywhere in the application till the session is destroyed.

Assigning value to session variable:

$_SESSION['username'] = "PEC"; //

Destroying a session

Whenever we log out or close the browser so two functions usually run for destroying the session

  1. session_unset();
  2. session_destroy();

Difference Between Session unset vs Session destroy

Session destroy is used for destroying all data of the current session while session unset is used to truncate the data but the session exists.

Increase Session Timeout In PHP

The default session timeout in PHP is 24 minutes (1440 seconds).

Now here is the easiest way of increasing session timeout.


// Now server will keep session data for 2 hour

ini_set('session.gc_maxlifetime', 7200);

// All clients will remember their session id for EXACTLY 2 hour


Put these line in the index.php file of your application or another file that run first in your application.

Basically, these lines are increasing PHP server timeout which is defined in php.ini(server configuration file). we managed it simply from our code.


In this article, we explained to you how to increase the session timeout in PHP. PHP sessions are the most important part of our application nowadays so it’s good to have knowledge about increasing timeout for controlling big and complex applications.

Leave a Comment