0

well this is really weird so i got this code for timeout user so he automatically logs out if 500 second passed since last time being active i put this in the upper bar since it's on all my pages

if(!isset($_SESSION))
    session_start();

if(isset($_SESSION['time']))
    if ($_SESSION['time'] + 600 < time()) {
        session_destroy();
    } else{
        $_SESSION['time'] = time();
        //some code to access database that says welcome and shows some details about the     account and manage account and stuff
    }

<div class="signIn"><?php 
    if(isset($_SESSION['USER_State']) && ($_SESSION['USER_State'] === true)){ 
        echo "<a href='SignOut.php'><p>Sign out</p>"; 
    } else { 
        echo"<a href='Registeration.php'><p>Sign In</p>"; 
    }?></div>

well this works but there is a problem when 600 second passes it destroys session when user press on any link and the welcome and stuff does not show anymore but still it echos Sign out instead of sign in although when i checked the page source from browser i found this

<td>
    <div class="signIn"><a href='Registeration.php'><p>Sign In</p></div>
</td>
</tr>

i checked a zillion times always the same sign out in display but sign in in source how is that possible

4
  • Why don't you just change your session time to 10 minutes? Why implement this yourself? Commented Dec 24, 2013 at 18:40
  • sorry iam a noob so can you explain ? Commented Dec 24, 2013 at 18:54
  • you are seeing different information in the page and view source because your page load that shows sign out still has the $_SESSION information loaded [session_destroy does not unload the $_SESSION data, just runs the garbage cleanup on the session] . when you go to view source, most browsers grab a fresh copy of the page, which will show the updated information. Commented Dec 24, 2013 at 18:57
  • oh now i get it thanks Commented Dec 24, 2013 at 19:02

2 Answers 2

2

To clear the session, you can use unset also.

unset($_SESSION);

Not only for session, you can use for any variable or array.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot this works but can u explain why destroy didn't work but this did ?
please check the link, t has details about your problem. php.net/session_destroy
1

Calling session_destroy does not remove the contents of the $_SESSION[] superglobal.

Add $_SESSION = array() to clear the session data for the current pageload.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.