apache_get_modules() is only available when the PHP is installed as a module and not as a CGI == It doesn't work with php-fpm.
(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)
apache_get_modules — Obtiene una lista de los módulos cargados en el servidor Apache
Obtiene una lista de los módulos cargados en el servidor Apache
Devuelve un array con la lista de los módulos cargados.
Ejemplo #1 Ejemplo de apache_get_modules()
<?php
print_r(apache_get_modules());
?>
El resultado del ejemplo sería algo similar a:
Array ( [0] => core [1] => http_core [2] => mod_so [3] => sapi_apache2 [4] => mod_mime [5] => mod_rewrite )
apache_get_modules() is only available when the PHP is installed as a module and not as a CGI == It doesn't work with php-fpm.
<?php
function apache_module_exists($module)
{
return in_array($module, apache_get_modules());
}
?>
this function can be used on older php versions using something like "/etc/httpd/httpd.conf" as $fname
<?php
function get_modules ($fname){
if (is_readable($fname)){
$fcont = file($fname);
if (is_array($fcont)){
foreach ($fcont as $line){
if (preg_match ("/^LoadModule\s*(\S*)\s*(\S*)/i",$line,$match)){
$return[$match[2]] = $match[1];
}
}
}
}
return $return;
}
?>
/**
* Check if a Apache module is loaded (even if php run as fcgi or cgi )
*
* @param string $module
* @return bool
*/
public static function apache_check_module(string $module): bool
{
$module = ($module ? strval(value: $module) : '');
if (function_exists('apache_get_modules') && !empty($module)) {
if (in_array(needle: $module, haystack: apache_get_modules())) {
return TRUE;
}
} else if (!empty(shell_exec(command: 'apache2ctl -M | grep \'' . $module . '\''))) {
return TRUE;
} else {
return FALSE;
}
}
function apache_module_exists($module_name)
{
$modules = apache_get_modules();
return ( in_array($module_name, $modules) ? true : false );
}
var_dump(apache_module_exists('mod_headers'));
<?php
function apache_module_exists($module_name)
{
$modules = apache_get_modules();
foreach ($modules as $module)
{
if ($module == $module_name)
return true;
}
return false;
}
var_dump(apache_module_exists('mod_headers'));