Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wowrudy's avatar

Redirect After Session Expired

How to redirect user to login page automaticly if their session expired ? i mean i don't want waiting user click some button or refresh manually

0 likes
5 replies
mstnorris's avatar

This is taken from the Inspinia Theme from {wrap}bootstrap. Click on Miscellaneous and then Idle Timer to see a preview.

This is a JavaScript implementation and it won't check the session directly but it will check for no user input. If it detects (or not detects I should say) any input from the user, then it will show a message. (You can do what you like at this point).

$(function() {
    // Set idle time
    $( document ).idleTimer( 5000 );
});

$(function() {
    $( document ).on( "idle.idleTimer", function(event, elem, obj){
        // function you want to fire when the user goes idle
    });

    $( document ).on( "active.idleTimer", function(event, elem, obj, triggerevent){
        // function you want to fire when the user becomes active again
    });

});
1 like
wowrudy's avatar

Thankyou @mstnorris for your answer, it can solve my problem right now..

i must set config/session.php

'lifetime' => 120,

and in javascript i must write exact value with my php configuration 120 minute * 60 second * 1000 milisecond = 7.200.000

$(function() {
    // Set idle time
    $( document ).idleTimer( 7200000 );
});

$(function() {
    $( document ).on( "idle.idleTimer", function(event, elem, obj){
        window.location.href = "example.com/login"
    });  
});

but any simple php solution like this ?

'lifetime' => 120,
'expired-session-redirect' => url('auth/login')

Please or to participate in this conversation.