user1904273 user1904273 - 29 days ago 5
PHP Question

PHP: Session vaiables not accessible in sub-directory

I have encountered a strange problem. I have a password protected site with most of my files in the main directory. All the files are inaccessible before login but upon setting a session variable become accessible. I use other session variables as well, all of which are accessible to these files.

However, for convenience I dumped a bunch of files related to one topic into a sub-directory. However, I've discovered that files in this sub-directory do not seem to have access to the session variables.

php

login.php
$_SESSION['username'] = "Bob"; //sets session variable.

subdir/file.php
echo $_SESSION['username']; //echoes nothing.
$username = $_SESSION['username'];
if (!isset($username)) {
echo "var not set"; //echoes out
}


Has anyone encountered this issue before? What could it be?

Thanks for any suggestions.

Answer

Have you started the session in those pages : session_start();

You have to start the session in pages were you assign values to session as well as on the pages where you use seesion values.

Comments