Include() a pre-processed php file

Hey Everyone,
As a php developer I should know this… but I’m stumped…

I used include() all the time obviously, but I want to include a php file but have it be processed prior to it being included.


<?php include('header.php'); ?>

    Some content/code goes 

<?php include('footer.php'); ?>

The problem is, I want it to include these files as if they were being loaded from an external location via the path or

I can use absolute paths in the include, but my host admin reported that causing the server to be overloaded with apache connections. (why it would be overloaded, I’m not sure, but figured I would try to find a better way to do this)

I want the files to be preprocessed because they include some wordpress php that can’t be easily processed outside the wordpress environment.

Any ideas?

By the sounds of it, remote include is your only option if they have to be parsed as stand-alone scripts.

function remote_include($url, $timeout = 3){
    $curl = curl_init($url);
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => (int)$timeout
    return curl_exec($curl);

If the output of those two scripts doesn’t change all that often, you could just save the output to a file. Then you can just read it from the filesystem.

<?php readfile('header.html'); ?>

    Some content/code goes 

<?php readfile('footer.html'); ?>


copy('', 'header.html');
copy('', 'footer.html');

The update script could be triggered manually by you as needed, or maybe run as a cron job, or integrated into the system that causes the output to change.

Sounds like you want some kind of caching there.

You can do it like this:
In header.php, at the top, check if a file header.cache exists, if it does, load it and echo it’s contents.

If not, do an ob_start, do your magic to print out the header, and then a ob_get_contents to get the output in a variable. Write that to disk to header.cache, and echo it.

Look at the PHP manual for ob_start.

I usually cache the stuff in APC or MemCached, but disks can work to, depending on your code.