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
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!

Link to comment
Share on other sites

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'>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.