Does the include() command break sessions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Thresher
Forum Newbie
Posts: 13
Joined: Sun Apr 01, 2007 3:12 am

Does the include() command break sessions?

Post by Thresher »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am unable to get a session to persist through an include() command.  I have distilled the problem down to file a.php that includes file b.php.  a.php starts a session, sets a session variable and includes b.php.  b.php starts the session, changes the session variable and completes.  a.php prints out the session variable.  The change made by b.php to the session variable is not evident in the output of a.php.

File a.php:

Code: Select all

<?php
session_start();
?>
<html><head><title>a</title></head><body>
<?php
$_SESSION['MySession'] = 'initial_MySession';

include("http://localhost/b.php");

print '$_SESSION["MySession"] is: '.$_SESSION['MySession'];
?>
</body></html>
File b.php:

Code: Select all

<?php
session_start();
$_SESSION['MySession'] = 'new_MySession';
?>
The output is:

Code: Select all

$_SESSION["MySession"] is: initial_MySession

Is there a way to make this work?

Thanks,
Griff


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

include() does not break sessions. Including a file that's remote (http://) will.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Weird. I don't think I've ever had a problem like that. Output session_id() in each file and see if it's the same.

Edit: Nevermind.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

you only need to include session_start() ONCE.

if its in a included file, it should throw an error. (its in the beginning of file b.php, and shouldnt be)

turn your error reporting on and set it to E_ALL.
Thresher
Forum Newbie
Posts: 13
Joined: Sun Apr 01, 2007 3:12 am

Post by Thresher »

feyd wrote:include() does not break sessions. Including a file that's remote (http://) will.
Thanks feyd. The problem was fixed by using:

Code: Select all

include("b.php");
instead of:

Code: Select all

include("http://localhost/b.php");
Last edited by Thresher on Sun Jun 03, 2007 5:44 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

maliskoleather wrote:you only need to include session_start() ONCE.

if its in a included file, it should throw an error. (its in the beginning of file b.php, and shouldnt be)

turn your error reporting on and set it to E_ALL.
But the reason it didn't throw an error is because it interpreted it as another website due to the http://, so the separate website had it's own session. Sort of like using cURL. Though, I was unaware you could include remotely.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

superdezign wrote:
maliskoleather wrote:you only need to include session_start() ONCE.

if its in a included file, it should throw an error. (its in the beginning of file b.php, and shouldnt be)

turn your error reporting on and set it to E_ALL.
But the reason it didn't throw an error is because it interpreted it as another website due to the http://, so the separate website had it's own session. Sort of like using cURL. Though, I was unaware you could include remotely.
*cough* *cough* whoopsie *cough*
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

superdezign wrote:Though, I was unaware you could include remotely.
It's a php.ini setting called allow_url_fopen. It's often turned off so don't rely upon it.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

d11wtq wrote:
superdezign wrote:Though, I was unaware you could include remotely.
It's a php.ini setting called allow_url_fopen. It's often turned off so don't rely upon it.
Aww... And here I was about to steal bandwidth. :twisted:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not to mention it's dangerous to include a remote page.
Post Reply