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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
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.

1
2
3
4
5
6
7
<?php
$modules = new Loaded_modules(); // Start the moduleCheck class
echo '
<pre>'
;
print_r($modules->listModules()); // List all installed modules
echo '< /pre>';
?>

0 Comments

Leave a Reply

Avatar placeholder

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