TheDrunkenEpic

Hello, I'm Wilhelm, a PHP developer, and this is my little corner of the internet where I can discuss things that interest me and, hopefully, you too. :-)


Currently Reading:

28Jun 2007

Checking if PHP is loaded as a module in Apache

If you're like me, you always come across weird situations where you need to check for some obscure configuration item or other piece of information. Well, this was the case for me just recently.

I needed to create a function that would check how PHP is loaded into Apache; whether it's loaded as a module or CGI extension. Since I couldn't find a built-in PHP function that checks for this, I had to think a bit 'outside the box', so to speak. The only other place I knew of that actually displayed this type of information was when invoking the phpinfo(); function.

I then figured I could use output buffering to capture the information generated by phpinfo();and then just use pattern matching to get the item I wanted. Simple, yes, but a kind of out-of-the-way solution just to get to some information that should be easily accessible through less obscure means.

The following is the end result of my brainstorming:

function loadedAsModule()
{
    ob_start();

        phpinfo(INFO_GENERAL);

        $output = ob_get_contents();

    ob_end_clean();

    if(preg_match_all('/<tr><td class="e">(.*?)</td><td class="v">(.*?)</td></tr>/', $output, $parsed, PREG_SET_ORDER))
    {
        foreach($parsed as $entry)
        {
            if(trim($entry[1]) == 'Server API' && preg_match('/apache/i', $entry[2]))
            {
                return true;
            }
        }
    }

    return false;
}

There you go, just call the function and it should return either TRUE or FALSE.

Not the most elegant piece of code and I'm sure it could be trimmed down quite a bit, but I thought it would be nice to share it with others in hopes that they won't have to deal with the extra headache. It could also be easily modified to check for just about any configuration item that phpinfo(); spits out.

Enjoy.

tagged with Free Goodies and Code Monkey and has 6 comments


Your Commentary:

Interesting snippet. I wonder though, would 'apache_get_modules()' achieve the same thing? (http://au2.php.net/manual/en/function.apache-get-modules.php). I've never needed to check if PHP is loaded as a module, so I am unsure if it shows up in that list however.

Alistair Kearney spoke the truth on June 29th at 6:02 am #

Yeah, I tried 'apache_get_modules()' too, but it doesn't have any reference to PHP within the return list. I also couldn't find any instances on the web where someone had to check for this.

If someone else could confirm, that would be great. You would assume that the aforementioned function would list PHP if it was loaded as a module, wouldn't you?

Wilhelm Murdoch spoke the truth on June 29th at 8:27 am #

How odd. Potentially that means PHP is specifically removing itself from the list. It is, after all, a module like any other. How annoying!

Alistair Kearney spoke the truth on June 29th at 6:53 pm #

Yeah, I kicked it in the nuts.

Wilhelm Murdoch spoke the truth on June 30th at 5:07 am #

As late as this comment is, I'm sure this is something worth adding. http://nz.php.net/php_sapi_name

A nice little function, one which I believe is more useful than merely on an Apache install.

Detruire spoke the truth on July 26th at 1:48 am #

Well, I'll be damned. I wish I found this earlier. It sure would've saved me a ton of time.

Wilhelm Murdoch spoke the truth on July 26th at 9:59 am #

TrackBack: http://www.thedrunkenepic.com/home/articles/trackback/82/

Leave a Comment: