/    Sign up×
Community /Pin to ProfileBookmark

Sharing Session Variables

I’ll try to explain what I’m doing (briefly) and then present the problem….

I’m taking the html output from a separate page and stuffing it into a DIV within a parent page (kind of a psuedo frame). I’m using file_get_contents() to render the php file to html and then outputting the resulting html inside a DIV.

The problem I’m having is that these “sub pages” are dependent upon session variables and it seems that file_get_contents() does a great job of executing the file but it runs it in a seperate session from the “parent page”. Hence the values the “sub pages” need are not available to them and the messages they send back to the parent aren’t available to the “parent page”.

I’ve not tried to do anything fancy with session variables so I’m not sure how to do what I need. What I need is to be able to specify the session_id to each and then have the pages render within that environment. I’m assuming I somehow use COOKIES to do this but don’t know how to accomplish what I want.

Any pointers to documentation about this?

thanks,
tony

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chazzyNov 18.2005 — It sounds like you might want to consider include and function calls. What exactly is your script doing, maybe we could help you rewrite it instead.
Copy linkTweet thisAlerts:
@tbirnsethauthorNov 18.2005 — It is an application that has many pages. Is what you're telling me that I can't get 'file_get_contents()' to run within the same session as the calling parent?

tony
Copy linkTweet thisAlerts:
@SpectreReturnsNov 18.2005 — There is no reason to use file_get_contents() when you could just include the page you want to parse.
Copy linkTweet thisAlerts:
@tbirnsethauthorNov 18.2005 — I have a script called generatepage.php. It gets the name of a template file from the database. It does keyword substitution on the template for things like <title></title>, etc. It also imbeds the 'sub page' within a DIV to create a psuedo frame.

So the generatpage.php script captures the template, does some substitutions, then invokes the 'sub page' within a DIV. The template then contains the html result of both the template and the 'sub page' within it.

The solution turned out to be abandoning file_get_contents and doing some diversion of the output buffers. I ended up with something similar to the following which seems to behave correctly as well as requirs no modification to any of the 'sub pages' (which is highly desireable). There's no error handling in the example below. The goal is to show what I did versus "hardening" it for this example. Things like $companyID are set, etc..

[CODE]
session_start();

$template = get_template_from_db($companyID);
ob_start();
include($template);
$tmpl_str = ob_get_clean(); // gets the ouput into a buffer

$subpage = get_sub_page();
ob_start();
include($subpage); // Note: the resulting HTML is returned, PHP has already been processed.
$sub_page = og_get_clean();

foreach( $substitution_array as $key => $val) {
$tmpl_str = str_replace($key, $val, $tmpl_str);
// Note that substitution of $sub_page within $tmpl_str is part of this loop
// so $tmpl_str will contain the output of $sub_page within the DIV in $tmpl_str
}
echo $tmpl_str; // output the created page.
[/CODE]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 18.2005 — why don't you include those pages and subpages ?

EDIT: sorry, i don't know how, but i didnt see the last post with the code ? ?
Copy linkTweet thisAlerts:
@tbirnsethauthorNov 19.2005 — Uh... as the solution update indicates, that's what I did. I trapped the output and used include().

tony
Copy linkTweet thisAlerts:
@chazzyNov 19.2005 — It is an application that has many pages. Is what you're telling me that I can't get 'file_get_contents()' to run within the same session as the calling parent?

tony[/QUOTE]


yes, actually. but it looks like you got through that already, good show!
×

Success!

Help @tbirnseth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...