Forum Moderators: coopster

Message Too Old, No Replies

sessions working, but not working on includes

same bit of code is ignored on files that are included

         

mylungsarempty

9:54 pm on Nov 1, 2010 (gmt 0)

10+ Year Member



What could I be doing wrong?


if (isset($_SESSION['username'])){
echo '<p align="right"><a href="http://www.littlerockmusicscene.com/logout.php" class="footer">log out</a></p>';
}

if (!isset($_SESSION['username'])){
echo '<p align="center" style="color: tan; letter-spacing: 1px; line-height: 2px;"><a href="http://www.littlerockmusicscene.com/register.php">sign up</a> | <a href="http://www.littlerockmusicscene.com/login.php">log in</a></p>';
}
?>


is working basically fine when used on the same page as where the session is set, but if i use the same bit of code on a php include file, it is totally ignored and displays text like the session variable isn't set. Any idea why it might act this way?

Thank you,

Kyle

Matthew1980

9:58 pm on Nov 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there mylungsarempty,

On these other pages, are you making sure that you have session_start() at the top/start of each file; if not, the script will not function as you expect.

Pop error_reporting(E_ALL); on too, as this will aid you in developing the script, always handy to know what's going wrong...

Also, if you don't want any other output happening, just use an exit; so that no extra html/duplicate content can come out...

Cheers,
MRb

mylungsarempty

10:30 pm on Nov 1, 2010 (gmt 0)

10+ Year Member



Well i have sessions at the top of the main page (say, index.php) but I don't have them on the includes, the php files i include inside index.php (for my template)

Wouldn't it reason that code inside the include files could access the $_SESSION variables as well?

mylungsarempty

5:08 am on Nov 2, 2010 (gmt 0)

10+ Year Member



I found the solution on another site. The problem is with using the full filepath in the include. Seems strange to me but if you just use the file name, not the full [,...] then it fixes the problem. I'm surprised it took me all evening to solves this!

enigma1

8:42 am on Nov 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the file you want to include is not an optional one, do a "require" instead of an "include". That should give you an error if the path is incorrect.

mylungsarempty

7:01 pm on Nov 2, 2010 (gmt 0)

10+ Year Member



The file is not optional. You're saying using "require" will do what exactly?

I'm can't believe how hard it was to track down this non-obvious answer... that calling the file by the full file path would keep it from accessing your session variables. I just do ../../filename.php and all works welll.

enigma1

7:37 pm on Nov 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"require" will generate an error if the file does not exist in the path you specify. "include" tries to include a file if it doesn't exist it does nothing.

mylungsarempty

9:35 pm on Nov 2, 2010 (gmt 0)

10+ Year Member



Thanks for the info. Good bit to know.