PHP extension/module check with extension info
By: Daniel

Have you ever needed to check to see if curl, ldap, or mysql was loaded in php for your script to work? With the code below, you can now check much more than that. With the heavily commented code below, you will be able to list all installed php extensions, check if an extension is loaded and view the settings of that extension.

*Please note that this is a PHP5 script

Save the code below as class.phpextensions.php:

<?php
class moduleCheck {

  public $Modules;
  
  //function parseModules() {
  function __construct() {
   ob_start(); // Stop output of the code and hold in buffer
   phpinfo(INFO_MODULES); // get loaded modules and their respective settings.
   $data = ob_get_contents(); // Get the buffer contents and store in $data variable
   ob_end_clean(); // Clear buffer
  
   $data = strip_tags($data,'<h2><th><td>'); // Keep only the items in the <h2>,<th> and <td> tags
   
   // Use regular expressions to filter out needed data
   // Replace everything in the <th> tags and put in <info> tags
   $data = preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$data);
   
   // Replace everything in <td> tags and put in <info> tags
   $data = preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$data);
   
   // Split the data into an array
   $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE);
   $vModules = array();
   $count = count($vTmp);
   for ($i=1;$i<$count; $i+=2) { // Loop through array and add 2 instead of 1
   
    if (preg_match('/<h2>([^<]+)<\/h2>/',$vTmp[$i],$vMat)) { // Check to make sure value is a module
    
     $moduleName = trim($vMat[1]); // Get the module name 
     $vTmp2 = explode("\n",$vTmp[$i+1]);
     foreach ($vTmp2 AS $vOne) {
       $vPat = '<info>([^<]+)<\/info>'; // Specify the pattern we created above
       $vPat3 = "/$vPat\s*$vPat\s*$vPat/"; // Pattern for 2 settings (Local and Master values)
       $vPat2 = "/$vPat\s*$vPat/"; // Pattern for 1 settings
       if (preg_match($vPat3,$vOne,$vMat)) { // This setting has a Local and Master value
         $vModules[$moduleName][trim($vMat[1])] = array(trim($vMat[2]),trim($vMat[3]));
       } elseif (preg_match($vPat2,$vOne,$vMat)) { // This setting only has a value
         $vModules[$moduleName][trim($vMat[1])] = trim($vMat[2]);
       }
     }
     
    }
   }
   $this->Modules = $vModules; // Store modules in Modules variable
  }
  
  // Quick check if module is loaded
  // Returns true if loaded, false if not
  public function isLoaded($moduleName) {
    if($this->Modules[$moduleName]) { 
      return true;
    }
    return false;
  } // End function isLoaded
  
  // Get a module setting
  // Can be a single setting by specifying $setting value or all settings by not specifying $setting value
  public function getModuleSetting($moduleName, $setting = '') {
    // check if module is loaded before continuing
    if($this->isLoaded($moduleName)==false) {
      return 'Module not loaded'; // Module not loaded so return error
    }
    
    if($this->Modules[$moduleName][$setting]) { // You requested an individual setting
      return $this->Modules[$moduleName][$setting];
    } elseif(empty($setting)) { // List all settings
      return $this->Modules[$moduleName];
    }
    // If setting specified and no value found return error
    return 'Setting not found';
  } // End function getModuleSetting
  
  // List all php modules installed with no settings
  public function listModules() {
    foreach($this->Modules as $moduleName=>$values) { // Loop through modules
      // $moduleName is the key of $this->Modules, which is also module name
      $onlyModules[] = $moduleName;
    }
    return $onlyModules; // Return array of all module names
  } // End function listModules();
}
?>

Now to list all of the installed extensions:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck(); // Start the moduleCheck class
echo '<pre>';
print_r($modules->listModules()); // List all installed modules
echo '</pre>';
?>

This would produce something like:

Array
(
    [0] => bcmath
    [1] => calendar
    [2] => com_dotnet
    [3] => ctype
    [4] => curl
    [5] => date
    [6] => dom
    [7] => filter
    [8] => ftp
    [9] => hash
    [10] => iconv
    [11] => ISAPI
    [12] => json
    [13] => ldap
    [14] => libxml
    [15] => mssql
    [16] => mysql
    [17] => odbc
    [18] => pcre
    [19] => Reflection
    [20] => session
    [21] => SimpleXML
    [22] => SPL
    [23] => standard
    [24] => tokenizer
    [25] => wddx
    [26] => xml
    [27] => xmlreader
    [28] => xmlwriter
    [29] => zlib
)

Now check if a single module (curl) is loaded, then get a setting:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck();
if($modules->isLoaded('curl')) { // Test if curl is loaded
  echo 'Curl Loaded<br />';
  echo $modules->getModuleSetting('curl', 'cURL Information'); // Get specific information about a setting in curl
} else {
  echo 'Curl not loaded';
}
?>

This would produce something like:

Curl Loaded
libcurl/7.14.0 OpenSSL/0.9.8d zlib/1.2.3

To list all settings for the curl extension:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck();
echo '<pre>';
print_r($modules->getModuleSetting('curl'));
echo '</pre>';
?>

This would look like:

Array
(
    [cURL support] => enabled
    [cURL Information] => libcurl/7.14.0 OpenSSL/0.9.8d zlib/1.2.3
)

You will use these settings as the second argument for getModuleSetting(‘extension’, ’setting’)

Now when you create an installer application for a script that you have just written, you can create a checklist showing what extensions are loaded and which still need to be loaded in order to continue.

Hope you find this useful.

1 Star2 Stars3 Stars4 Stars5 Stars (16 votes, average: 4.19 out of 5)
Loading ... Loading ...

11 Responses to “PHP extension/module check with extension info”

  1. marcellus Says:

    check the native functions get_loaded_extensions and extension_loaded ffs, saves you all the bs preg matching on the phpinfo() output

  2. Joel Says:

    Maybe you should check the get_loaded_extensions() function in php.
    Goodl Luck

  3. barbara Says:

    Hi,

    hmmmm, wouldn’t you agree that you should not take credits for the main portion of this code, as it’s not yours to begin with?
    Scrolling through the PHP website, this code is ripped from a post way back in 2005. Besides that, it seriously lacks PHP-native functions, as already noted in the other comments. Lastly, the use of XPath expressions, in many many many cases, is much faster then regular expressions.

  4. Ray Says:

    get_loaded_extensions() or extension_loaded() doesn’t grab the extension’s settings.

    Is there another way to get an extension’s setting other than output buffering phpinfo()?

  5. Tennille Manalang Says:

    Thanks so much for sharring this, the 1onica Team

  6. easy kids dessert recipes Says:

    Good post, useful blog, thank you for your work, keep on, guys!

  7. Glendora Brailford Says:

    this is usually a superb internet site. I have not been through this kind of top quality pleased. My business is fairly delighted. I am to consider your site regurly and we do hope you modernize extra. We will recommend my pals for this place. The knowledge will likely be helpful for option living. I suggest these kinds of spot to everyone. I like viewing that a good deal. Thanks to you during such as high quality content articles.

  8. Animation Institute Says:

    This really is an outstanding article, I was wondering whether I might use this write-up on my personal internet site, I will hyperlink it back to your website though. If this sounds like a problem please let me know and I will take it down at once

  9. dieta Says:

    Wow, superb weblog structure! How lengthy have you ever been blogging for? you make running a blog look easy. The whole glance of your web site is fantastic, let alone the content!

  10. Belstaff jackets Says:

    We have read a number of good stuff right here. Definitely well worth bookmarking regarding returning to. My partner and i big surprise just how much effort you place to produce this kind of superb educational web site.

  11. Psycholog Says:

    Awesome web-site. A lot of handy info in this article. I’m mailing it to a few buddies and also posting in delicious. And naturally, thank you for your efforts!

geovisitors