Jump to content

redirect not working after session ends


webguync

Recommended Posts

anyone know what might be causing my redirect to not work in my session ending/logout code. The session ending part seems to work, but not the automatic redirect back to the login page. I know the session ending works, b/c when I refresh the page I go back to the login screen.

 

<?php
       session_start();

       if(!session_is_registered("session_count")) {
             $session_count = 0;
             $session_start = time();
             $_SESSION['session_count']=$session_count;
             $_SESSION['session_start']=$session_start;
       } else {
             $session_count++;
       }

       $session_timeout = 30; // expires after 30 seconds

       $session_duration = time() - $session_start;
       if ($session_duration > $session_timeout) {
           session_unset();
           session_destroy();
           $_SESSION = array();
               header("Location: StudentLogin.php");  // Redirect to Login Page
       } else {
           $session_start = time();
           $_SESSION['session_start']=$session_start;
       }

Link to comment
https://forums.phpfreaks.com/topic/195454-redirect-not-working-after-session-ends/
Share on other sites

Just making sure...

 

If you reload the page, and it sends you to the login page (after the session has expired) then PHP is doing it's job properly.

 

If you want to redirect the browser (without the user having to go to a new page, or refresh) then you will want to redirect via JS or a meta redirect.  That, or have an ajax request hit the server, check the session, and based on that redirect.

 

Either way, if javascript is turned off your options on automatically redirecting based on a timeout are limited.

 

If that's not what's happening, then I appologize and will be happy to help you investigate further!

yep you got it, so I guess the PHP code is working. Can I just add meta direct to the current code, if so would it just need to replace :

 

header("Location: StudentLogin.php");  // Redirect to Login Page

 

with...

<meta http-equiv='refresh' content='30'; url='StudentLogin.php'>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.