Articles PHP Code

How to get loaded modules in the server using php code

Hi friends, In this post i would like to explain how to get list of modules which are installed in the server using php code. We have confuse that which installed or activated are available in the server, when we are trying to code then we got the error that particular module is not loaded in the server. Before complaint (request ) to the hosting provider (eg: godaddy, hostgator etc … ).

How to get loaded modules in the server using PHP by Anil Kumar Panigrahi
How to get loaded modules in the server using PHP by Anil Kumar Panigrahi
demo link for how to get loaded modules in the server using php code  

Once run the below coded file in your server. It will display the all loaded modules in the server.

PHP Class:

class Loaded_modules {

  public $Modules; // public variable

  //function Loaded_modules()
  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
  // 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();


Creating object to that class and call the methods of that class.

$modules = new Loaded_modules(); // Start the moduleCheck class
echo '
print_r($modules->listModules()); // List all installed modules
echo '< /pre>';

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.