Using PHP to Display Version Info
Posted by Nessa | Posted in apache,code,linux,perl,php,ruby,scripts | Posted on October 15, 2007
3
I’ve been working on this application for work that does some simple server reporting, part of which involves displaying the versions of major software running on the machines. The importance of this to me personally is that since we have over 30 shared servers hosting multiple customers, we are continually moving websites between servers. Some of our older generation servers are still running MySQL 4.0 and PHP 4.3, so I need to be aware of this to make sure that customers are being moved to servers with compatible versions. It’s also good in tracking and planning upgrades.
I find it best to use the exec() function since it’s not blocked by most hosts. However, if you are on a shared host it’s very likely that certain PHP functions are disallowed in the php.ini. In that case you may be able to subsitute exec with system, passthru, escapeshellcmd, or shell_exec…unless those are blocked too. Then I guess you’re out of luck.
Start by creating some variables to store ordinary shell commands. If you wanted to find the php version from command line, you would usually type:
php -v
This will give a huge chunk of crap that you really don’t need if you’re making a simple version display script. In this case, you can use grep, awk, sed, and cut to trim down the output into a one-liner:
php -v |grep built |awk {‘print $2′}
The awk command prints out columns, so in the above example I’m printing out the second column of the line that contains the word ‘built’. Once the desired output is figured out, you simply assign it to a variable passed through exec() or a similar function:
$phpver = exec("php -v |grep built |awk {'print $2'}");
Then you can call the variable $phpver anywhere in your script:
echo "PHP Version: $phpver";
You can probably go through and figure out the commands to show other software versions on your server. In my script I’m showing the perl, php, mysql, apache, python, cpanel, and ruby versions. Here are the commands I used:
<?php
$perlver = exec("perl -v |grep linux |awk {'print $4'}|sed -e 's/v//'");
$phpver = exec("php -v |grep built |awk {'print $2'}");
$mysqlver = exec("mysql -V |awk {'print $5'} | sed -e 's/,//'");
$apachever = exec("apachectl -v |grep version |awk {'print $3 $4'}|sed -e 's/Apache\///'");
$pythonver = exec("python -V 2>&1 | sed -e 's/Python //'");
$cpanelver = exec("cat /usr/local/cpanel/version");
$rubyver = exec("ruby -v |awk {'print $2'}"); ?>
Then I just echoed out all the variables to display my version numbers:
See here .
Related posts:











[...] has two posts that talk about how to use PHP to display the versions of software running on the local [...]
[...] it’s so easy that I don’t even know why I’m posting it, but it kinda supplements this and [...]
This is an old post, so I hesitated to comment on it, but hopefully this will at least help anyone else who stumbles upon it.
apachectl doesn’t play nice with exec() and friends anymore. You can see this even in the example linked at the end of this post (http://v-nessa.net/wp-content/scripts/versions.php). I was unable to find any information about what’s going on, but I suspect that PHP is explicitly blocking it (for security reasons I’d assume).
What’s even more bizarre is that using the full path (so on my system: exec(“/usr/sbin/apachectl -v [...]“)) works!