简体   繁体   中英

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.

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.

您应该在调用会话变量之前添加session_start(),以便$ _SESSION ['username']不会被取消定义或为空。

First you will need to start session with this

session_start();

and then until the session ends you can get those variables that are stored in session can be used in your all directories and subdirectories

Type

 Session_start();

at beggining of the page... And echo the variable..

echo $<variable_name>;

You can see the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM