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.
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.