Sessions combined with includes

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
yoma
Forum Newbie
Posts: 3
Joined: Fri Dec 12, 2003 5:17 am

Sessions combined with includes

Post by yoma »

hi

A problem i cannot seem to solve:

page a: i start a session in which i register a user
page b: has secret content => checks the session variables

no problem so far BUT

page c: has to be included in page b, but also needs the session variables checked.

=> i tried passing the sessionid via include but i couldn't get this working

HOW CAN ONE PASS A SESSION_ID TO AN INCLUDED FILE?

cu later folks.
:twisted:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You shouldn't need to pass the id to the included file, the included file should automatically have access to all the variables available to the parent file.

Mac
yoma
Forum Newbie
Posts: 3
Joined: Fri Dec 12, 2003 5:17 am

hmmm

Post by yoma »

tnx a lot for the quick answer

but i'm affraid it's not correct.

-if you don't start a session on the included page you don't have access to
$_SESSION['myvariable']

-if you do a session_start() on the included file a new session is started
-if u try to pass the session id via

include("http://myhost/mypage.php?PHPSESSID=$sessid") you get scripttimeout

Maybe there is someone who knows a small workaround for the problem?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

the thing is, include files ARE NOT run as separate scripts, when you inlcude a file into another, the two are combined, then run. So long as you include your file after your session_start() you should be okay. If not, there maybe another problem with the way you have constructed your script.

Could you show us you scripts!?

Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: hmmm

Post by twigletmac »

yoma wrote:tnx a lot for the quick answer

but i'm affraid it's not correct.

-if you don't start a session on the included page you don't have access to
$_SESSION['myvariable']
sorry that isn't true, the included file has access to the session variables as long as session_start() has been called in the parent file.
yoma wrote:-if you do a session_start() on the included file a new session is started
-if u try to pass the session id via

include("http://myhost/mypage.php?PHPSESSID=$sessid") you get scripttimeout
You can't pass variables to included files like that, it just won't work. The included file has access to the same variables that the parent file does.
yoma wrote:Maybe there is someone who knows a small workaround for the problem?
As Mark's said, we'll probably need to see your scripts to help you fix this.

However, you are trying to include a URL, have you tried using the relative path to the page? E.g.:

Code: Select all

include 'mypage.php';
assuming mypage.php is in the same folder as the parent page.

Mac

Mac
yoma
Forum Newbie
Posts: 3
Joined: Fri Dec 12, 2003 5:17 am

hey folks

Post by yoma »

thanx a lot for your patience:

don't ask me how but the bug went away...

anyway:

"PHP FOR PRESIDENT!" :P
Post Reply