samoht Posted January 18, 2010 Share Posted January 18, 2010 Hello all, I tried to Google a answer for how to do a redirect on Session end with php but only asp came up (at least on the first 2 pages). So I thought I would ask here. I would like to send users back to the home page if their session expires. I think this should be simple - right?? Thanks for any help, Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/ Share on other sites More sharing options...
p2grace Posted January 18, 2010 Share Posted January 18, 2010 Add this to the top of your header include: if(!isset($_SESSION['somevar'])){ header("Location: path/to/login.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997482 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 Yep, Sessions can be called on pages required using session_start(); , Note this must be the first line of every page that uses sessions, or it may not work. As well, do not output anything (echo/whitespace/html etc.) before this line of code, outside the php tags. Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997497 Share on other sites More sharing options...
samoht Posted January 18, 2010 Author Share Posted January 18, 2010 For some reason I get a FORBIDDEN Access (flooding) message when I use that ?? Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997508 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 Are you pasting that code on the page that its redirecting to? If so you will be in an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997516 Share on other sites More sharing options...
samoht Posted January 18, 2010 Author Share Posted January 18, 2010 yep - that would be my problem. However, my site is run off of my index.php so don't I have to put this there? Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997521 Share on other sites More sharing options...
p2grace Posted January 18, 2010 Share Posted January 18, 2010 You'd put it anywhere you want php to check if the session is inactive and redirect. Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997523 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 I am assuming that your login page is access something like this "index.php?page=login" If I am correct, you can do this.. if ($_GET['page'] != 'login' && !isset($_SESSION['somevar'])) { // Do the redirect } Quote Link to comment https://forums.phpfreaks.com/topic/188889-redirect-on-session-end/#findComment-997524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.