Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Passing variables between apache PHP includes

Apache, SSI, fancyindexing, php, indexes, HeaderName, ReadmeName

8:25 pm on Nov 28, 2008 (gmt 0)

10+ Year Member

I'm building a custom indexing setup and wondering if there is a way to pass variables between php files included by apache for headers/footers of indexes:

for this Directory, I have apache2 configured to use
HeaderName /site_includes/head.shtml
ReadmeName /site_includes/foot.shtml
-- which then each pull in php via
<!--#include virtual="/site_includes/head.php" -->
<!--#include virtual="/site_includes/head.php" -->
which do some stuff and display around the standard apache directory indexes.
(i had some reasons which i may be open to revisiting for NOT reconfiguring .php to output as text/whatever, which would enable me to include these directly)

however, i am finding (not surprisingly) that foot.php is unable to access any variables/functions defined/included by head.php

so, any ideas on how to get around this, to send them across the gap?

it doesn't kill me to re-include the functions but some of the path parsing and template selection stuff seems like a waste to repeat... session variables? something like that?

please advise, danke

8:44 pm on Nov 28, 2008 (gmt 0)

5+ Year Member

(i had some reasons which i may be open to revisiting for NOT reconfiguring .php to output as text/whatever, which would enable me to include these directly)

I assume that's referring to your using SSI instead of the PHP include(); function, which would enable you to do exactly as you want. If that's the case, I recommend rethinking why you're using the SSI. I can't think that's the case, though, as you shouldn't need to worry about the output MIME type when it comes to includes. You may want to clarify that.

Regardless, remember your global directives (though my suspicion is that that's not the problem). I might recommend storing variables in the $_SESSION array, but I can't remember if those would update immediately. Regardless, it doesn't help you with functions. If that doesn't work, you could output relevant variables to a temporary file and read them back in the footer; again, fails on functions.

In short, regardless of your reasons for not doing so, find a way to use include()

EDIT: Ah, I see why you're using the SSI now. I didn't see the bit about doing this for apache indexes. If apache can use PHP files for the header and readme documents, I'd recommend that approach. Otherwise, I've no clue.

8:57 pm on Nov 28, 2008 (gmt 0)

10+ Year Member

It's referring to Apache's NEED to have the files to which HeaderName and ReadmeName point to return a major content type of "text/something" -- see doc here:

-- on the server in question, there is a lot of php-based img processing and some proxied download instances, so that route will potentially break established things, and i am not interested in it except as a last resort.

setting the major ctype of php returns to text/* would in this context enable me to use
HeaderName /site_includes/head.php
ReadmeName /site_includes/foot.php
-- which i *suspect* would have the same problem anyway.

I had considered temp files, and decided that was slower than i'd like things to be, (file io overhead will probbly test to be larger than reprocessing overhead of things as they are presently) again, reserving that until I see no other way.

A test with
// headerfile
$_SESSION["whatever"] = "something";
// end headerfile


// footerfile
// end footerfile

shows that $_SESSION is lost between the two files.

[edited by: TeofenGL at 8:59 pm (utc) on Nov. 28, 2008]

9:28 pm on Nov 28, 2008 (gmt 0)

5+ Year Member

My suspicion of what's happening is that you're losing the instance of PHP running; IE, when Apache makes the request to the ReadmeName document, it initiates a new PHP "request." My guess is that since it's apache including the file instead of PHP, it runs the PHP parser twice, once over each file.

Since $_SESSION is associated with a series of HTTP requests, and these file includes are technically internal system requests, a new session ID would be issued (in fact, the old session should technically be inaccessible). If you're still interested in that route, I might suggest using session_name('arbitrary_name') before you begin the session in both files.

Beyond that, perhaps you could actually consolidate the header and footer into a single document? I'm not sure of the HTML structure of Apache indexes, but you may be able to absolutely position your header and footer, or else structure them in such a way with CSS that the contents appear out of order. Perphaps you could place the functionality into a sidebar? Of course, that's assuming it wouldn't mess with the aesthetic :)

10:42 pm on Nov 28, 2008 (gmt 0)

10+ Year Member

CSS seems to be a pretty viable solution ATM, for the simpler stuff, writing some of the work-intensive results into attributes and then into formatted :after pseudoelements etc

I'm blanking on how to reverse order of two divs, tho, so off to the frontend forums i guess.



Featured Threads

Hot Threads This Week

Hot Threads This Month