views:

216

answers:

5

I've got a simple login system using PHP sessions, but just recently it seems that if you visit pages not in a certain directory (/login/) you will always be flagged as not logged in, even when you are. It seems that my session data is being lost when I change directories (say, to /login/user/).

I don't think I've touched the code myself since the problem appeared, is there something my web host could have done to my PHP installation that would delete the session data, and is there a workaround?

EDIT:
Inside each file that needs authorization, it loads a loginfunctions.php file which calls session_start() and checks the login. Files which work in /login and i copy and paste into /login/user stop working, even though i update all the relevant paths and links.

EDIT2: Okay, some code.

In the actual pages that are giving me the error, this is the auth. code:

require_once("../../../includes/loginFunctions.php");

$login = new login; 
$login->checkLogin(0);

Inside loginFunctions.php is this:

class login{

    function checkLogin($requiredAccess){

            session_start();

            if($_SESSION['accesslevel'] < $requiredAccess || $_SESSION['logged_in'] != TRUE){
                die("You don't have access to this area. If you should have access, please log in again. <a href='/login/'>Login</a>");
            }

            if (isset($_SESSION['HTTP_USER_AGENT'])){
                if ($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT'])){
                    session_destroy();
                    die("Bad session. Please log in again. <a href='/login/'>Login</a> ");
                }
            } else {
                $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
            }

            if (!isset($_SESSION['initiated'])){
                session_regenerate_id();
                $_SESSION['initiated'] = true;
            }

    }

}

The $requiredAccess variable is the access level that you need to access this page, so if you have an accesslevel of 3 in the database you can view level 0, 1, 2 and 3 pages. This is specified when the function is called in the main page and is compared to the access level of the current user which is defined in $_SESSIONS when they log in.

I'm getting the error 'You don't have access to this area etc." when i try to access these pages. If i try to print the $_SESSION variables, nothing shows; they appear to be empty. However, if I move the file to the /login/ folder (one level up) and update the links, they work perfectly and all the variables print out fine. This makes me think the code is not the part that's not working, but some setting in my PHP install that has been changed without my notice.

A: 

maybe you aren't calling session_start() at the begging of pages not in /login/ ..?

Silmaril89
Edited my first post to show how i'm calling that. Also, the same pages work if they're in the main /login/ directory.
Ineffable
A: 

It's possible that they changed the php.ini setting session.cookie_path.

You should call session-set-cookie-params before you call session_start and make sure you set the cookie path yourself. Set it to the highest level directory you want the session to be valid for. EG if you set it to /login it will be valid for /login and /login/user. If you want your session to be valid for the etire site set the path to be /

Josh
I tried setting session_set_cookie_params(3600, "/");to no avail. Also, I checked session.cookie_path in my php info and it's set to "/" anyway.
Ineffable
Hmmm. Not sure what the issue could be. Please edit your question and post some code.
Josh
Updated my post for you. Thanks for taking the time to reply.
Ineffable
When you log in do you se a cookie set? If so, is the domain and path correct? You can check for cookies in FireFox under "Tools >Page Info", on the "Security" tab
Josh
A: 

This is why you shouldn't use directory to make false friendly URLs...

Don't forget to call session_start() every time you need the session.

TiuTalk
What does "use directory to make false friendly URLs" have anything to do with this? I have "friendly URLs" in almost every system I build and have never had an issue. And hey, ever noticed StackOverflow.com does, too?
Josh
I think he means that most friendly URLs are made by rewriting rather than by actual directories on disk.
erisco
@erisco: Ah. Thanks for clarifying. That makes sense. But still, whether rewritten or not the issue would still occur -- the browser has no idea that rewriting is occurring and cookie paths (if that's the issue) must still be set correctly.
Josh
there's no rewriting going on with these URLs in question. see comment to original post for how i'm calling session_start().
Ineffable
also, how would you suggest I organise the files? Maintenance would be a nightmare if they were all piled in the same directory..?
Ineffable
A: 

I had a similar problem. Check you don't have a php.ini file. Removing this sorted the problem out. Still looking ito exactly why. The php.ini file could even be blank and it would stop session data from carrying over to more than one directory...

sessionddrivingmemad
for clarification, a php.ini in your current directory. obviously you'll have a php.ini somewhere...
sessionddrivingmemad
A: 

i had a similar issue. you may want to use: <? setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1); ?>

or something similar. i know cookie and session variables are a different desired solution, but this was able to clear up my issue.

See here for documentation

jason m