0

I have a PHP Session that I am using to manage my administration panel of my website. This uses two variables to monitor the session, lastAction and user. Recently, on one particular page of my website, the session variable user will randomly unset entirely and send the user back to the login page, but I cannot seem to figure out why.

session_start();

$fn = "timeout.txt";
$fh = fopen($fn,'r');
$to = fread($fh,filesize($fn));

if(abs(time()-$_SESSION["lastAction"]) > $to)
{
    session_unset();
    die("Timeout");
}

var_dump($_SESSION);

//Set timeout back to 0
$_SESSION["lastAction"] = time();

if($_SESSION["user"] != "...")
{
    die("Invalid User");
    header("Location:/login");
}

I never get the Timeout error, only the Invalid User error.

Sometimes when I dump the session variable, I get the array of two values, lastAction and user, but then, simply by reloading the page only a few seconds later, user gets unset, but lastAction does not.

Also, in this instance, I am reloading the page through javascript: parent.location='';

Any ideas on what might be causing this?

2
  • Is it possible that the sesssion has exceeded the php session timeout setting? Commented Oct 18, 2013 at 20:11
  • I don't think so; this happens randomly, and if I go directly to this page after logging in (perhaps only a minute later), 1 in 5 refreshes will unset the variable. Commented Oct 18, 2013 at 20:13

1 Answer 1

3

This most likely has to do with your Header. The session variables are only good within the domain they are set in. For example, if you set the session vars in www.example.com, but then your header redirects to example.com, you will not have your session vars to use, same as if you redirected to mobile.example.com. Change that relative path I see in your code and see if that is what is causing it.

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

4 Comments

What relative path do you mean in particular, because I'm still getting logged out sometimes.
I don't know, I can't see all the pages or all the code that could be causing it. That is for you to look through. It could be a javascript redirect too, not just a header redirect. The main thing is to be consistent with your domains.
Is it possible that a 404 error on an image could be causing this?
Yep that was it! A 404 error was causing it. Thanks for your help!

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.