Simple php function for session timeout, example and tutorial for session timeout, make session timeout in php easily.

How To Make Session Timeout

Using $_SESSION variable and time() function, we will make session timeout easily.

Important

Before using our functions, add this at top page:

ini_set('session.gc_maxlifetime', 3600); // change 3600 to your timeout, 3600 seconds = 1 hour
session_start();

Destroy Session If User Closed The Page

This is function to destroy session if user closed the page:

 /* Function By Qassim Hassan, wp-time.com */
function session_timeout_after_close_page($time, $name){
    if( isset($_SESSION["$name"]) and time() > $_SESSION["$name"] ){
        session_unset();
        session_destroy();
        session_start();
    }

    $_SESSION["$name"] = time() + $time;
}

Now just call function in your php file, for example in index.php file:

session_timeout_after_close_page(3600, 'my_session_name');

Now if user closed the page, all sessions in index.php will be destroyed after 1 hour, change “3600” to your timeout, 3600 seconds = 1 hour, change ‘my_session_name’ to your session name.

Destroy Session After Time

This is function to destroy session after time, if user has closed the page or has not closed, in all cases, session will be destroyed:

 /* Function By Qassim Hassan, wp-time.com */
function session_timeout($time, $name){
    if( !isset($_SESSION["$name"]) ){
        $_SESSION["$name"] = time() + $time;
    }

    if( time() > $_SESSION["$name"] ){
        session_unset();
        session_destroy();
        session_start();
    }
}

Now just call function in your php file, for example in index.php file:

session_timeout(3600, 'my_session_name');

Now all sessions in index.php will be destroyed after 1 hour, change “3600” to your timeout, 3600 seconds = 1 hour, change ‘my_session_name’ to your session name.

Example

This session timeout example for login page, now we have 3 files, index.php and login.php and functions.php, in index.php file:

<?php

ini_set('session.gc_maxlifetime', 3600);
session_start();

if( isset($_SESSION['login']) ){
    include 'functions.php';
    session_timeout(3600, "login");
    echo "Hello user!";
}else{
    echo '<a href="login.php">Please log in</a>';
}

?>

And in login.php file:

<?php
ini_set('session.gc_maxlifetime', 3600);
session_start();

include 'functions.php';
session_timeout(3600, "login");

header("location: index.php");

?>

And in functions.php file, same our functions:

<?php

function session_timeout_after_close_page($time, $name){
    if( time() > $_SESSION["$name"] ){
        session_unset();
        session_destroy();
        session_start();
    }

    $_SESSION["$name"] = time() + $time;
}

function session_timeout($time, $name){
    if( !isset($_SESSION["$name"]) ){
        $_SESSION["$name"] = time() + $time;
    }

    if( time() > $_SESSION["$name"] ){
        session_unset();
        session_destroy();
        session_start();
    }
}

?>

Now open http://localhost/session_time/index.php link, you must log in! Good, now log in, after logged in and after 1 hour (3600 seconds) session login will be destroyed, download example files.

Note

If our functions is not working, contact hosting provider.