Check dynamically loaded PHP extensions from command line
Asked Answered
O

3

27

I was checking the PHP manual to understand the different types of PHP extensions (PHP modules). There are Zend modules (mainly for PHP gurus), built-in modules and external modules.

Is there a way to tell from command line whether a PHP module has been loaded dynamically or whether it has built-in into the PHP binary?

I mean: with php -m I get all the loaded modules, but I would like to know which ones are built-in and which ones are external.

Osteoclast answered 23/1, 2013 at 11:44 Comment(0)
I
3

I'm not sure this is possible from regular PHP code, there may be some internal Zend calls you can make from an extension of your own. However, there might be a cheeky way of guessing, by checking if a loaded extension has a likely looking dynamic library available...

$extdir=ini_get('extension_dir');

$modules=get_loaded_extensions();
foreach($modules as $m){
    $lib=$extdir.'/'.$m.'.so';
    if (file_exists($lib)) {
        print "$m: dynamically loaded\n";
    } else {
        print "$m: statically loaded\n";
    }
}

That's not foolproof, but might be enough for you!

Intoxicated answered 23/1, 2013 at 11:54 Comment(1)
But the script would need to check also if the .so library is included in the php.ini (if a line with "extension=$m.so" has ben added) ? Also, I was wondering if there's any information about this in phpinfo() output, but I could not see itOsteoclast
S
75

Just run this command on the command line:

php -m

or this for more information:

php -i

Hope this helps.

Smoot answered 2/4, 2015 at 12:26 Comment(2)
How does this help distinguish between dynamically-loaded extensions and those built into the binary? As far as I can tell both kinds show up identically. Unless I'm missing something, this is totally irrelevant to the question asked - especially since the asker mentioned php -m in the question and explained why it doesn't give him the information he needs.Elan
People read the subject, not the whole question, and find the answer that they are looking in the second answer, not the first one. To correct that, question's subject should be more specific that only dynamically loaded extensions are what he is interested in: "Check dynamically loaded PHP extensions from command line"…Florri
G
8

1) Run

php -i

from the output locate the following parameters:

Loaded Configuration File - this will specify the location of the php.ini file being used by your php.

Scan this dir for additional .ini files - if this is not empty, some of the .ini files in this directory will dynamically load php extensions.

Additional .ini files parsed - .ini files loaded from the directory specified in the previous parameter.

If you are using Linux you could:

php -i | grep -e "Loaded Configuration File" -e "Scan this dir for additional .ini files" -e "Additional .ini files parsed"

2) Rename your php.ini file and rename the folder with additional .ini files.

3) Repeat step #1 and verify that Loaded Configuration File and Additional .ini files parsed both have a value of (none)

4) Run

php -m

You will now see a list containing only the extensions that are built-in to the php binary.

Guilbert answered 3/11, 2015 at 9:35 Comment(1)
The command line php --ini may be an easier way of finding out step (1) in this list.Catenoid
I
3

I'm not sure this is possible from regular PHP code, there may be some internal Zend calls you can make from an extension of your own. However, there might be a cheeky way of guessing, by checking if a loaded extension has a likely looking dynamic library available...

$extdir=ini_get('extension_dir');

$modules=get_loaded_extensions();
foreach($modules as $m){
    $lib=$extdir.'/'.$m.'.so';
    if (file_exists($lib)) {
        print "$m: dynamically loaded\n";
    } else {
        print "$m: statically loaded\n";
    }
}

That's not foolproof, but might be enough for you!

Intoxicated answered 23/1, 2013 at 11:54 Comment(1)
But the script would need to check also if the .so library is included in the php.ini (if a line with "extension=$m.so" has ben added) ? Also, I was wondering if there's any information about this in phpinfo() output, but I could not see itOsteoclast

© 2022 - 2024 — McMap. All rights reserved.