Checking requirements for PHP Applications

A great strength in PHP is that it’s so accessible, that almost anyone can get access to a PHP site and often will download “applications” for their site such as discussion boards, photo galleries and others. While it may easy to download such applications, it often happens that the people trying to get things to work, doesn’t read the requirements section of the install.txt
or readme.txt file, and thus never get the application running and doesn’t understand what the problem was and how to fix it. This is a brief guide with some tips on how to improve usability in this area.

First things first

Instead of relying on the user to read and check all steps in a text file, you should provide the install/setup file as a PHP-file, which is called through the browser. By doing this, you put the user in a familiar environment, and enables you to use all the power of PHP to guide the user through the requirements and install procedure. Assuming you think that’s a
bright idea, let see what you can do in such a setup-file.

Checking for the correct PHP version

First, if your Application requires a certain version of PHP, you should check if it’s available.

  • There’s a php function called phpversion() which will return a string with the current version number as a string.
  • PHP has a constant defined called PHP_VERSION. It contains the same string as the phpversion function returns.

If your application requires a certain version of PHP, there’s also a function called version_compare(),
which can help you check for a specific php-version or at least a certain version (ie. 4.4.0 or newer).

You use the function like this:

1
2
3
4
5
if (version_compare(php_version(), "4.4.0", ">=")) {
// you're on 4.4.0 or later
} else {
// you're not
}

Checking for available extensions (compiled into PHP)

There’s function called get_loaded_extensions. It returns all the extensions compiled into PHP,
and if you need a certain extension, you can use this, to see if it’s available.

1
2
3
4
5
6
7
$load_ext = get_loaded_extensions();
 
if (in_array("soap", $load_ext)) {
echo "SOAP is available";
} else {
echo "SOAP is NOT available";
}

The extensions also have different version numbers, and if you need a certain version number to get your application to work, we can use the phpversion function from above to do this. The function can take an extension name as parameter, and will return not the PHP version, but the version of the extension instead:

1
2
3
4
5
$load_ext = get_loaded_extensions();
 
if (in_array("soap", $load_ext)) {
echo "soap extension available in version: " . phpverion("soap") . "<br>n";
}

If you do need a specific (or “at least”) version of the extension, you should do the check as in the above example, where at least a certain PHP version was required.

Checking for Zend Platform

If you’re running with Zend Platform enabled, it adds some extra features, which might be neat to use (if available) such as caching or error reporting. Since most PHP environments doesn’t have Zend available, we should check for the availablility before using the functions. One way of doing this is by using the “function_exists” function in PHP. It returns a boolean (true/false) answer.

1
2
3
if (function_exists("monitor_custom_event")) {
monitor_custom_event(........)
}

(the monitor_custom_event()-function is specific to Zend Platform – if available, we can use it.)

Checking for include files availablity

Some libraries are not compiled into PHP but written as PHP scripts themselves. Examples include MagpieRSS, CakePHP, Zend Framework and others. Checking if they are avaible without causing PHP to throw an error or at least warning message is a little tricky.

Here’s one way to do it. Turn off all error reporting, try to include the file, turn on error reporting and then check the list of included files to see if it was included:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$fileToInclude = 'SomeFile.inc';$initial_setting = error_reporting(0);
include($fileToInclude);
error_reporting($initial_setting);
$files = get_included_files();
$available = false;
$pattern = "/" . $fileToInclude . "$/";
 
foreach ($files as $file) {
if (preg_match($pattern, $file)) { $available = true; };
echo "$file n";
}
 
echo "The file \"$fileToInclude\" is ";
if (!$available) { echo "not "; };
echo "available";

Do be careful before applying this technique. First you should always be very careful
before toying with the error reporting (you don’t fix errors by turning off error reporting),
and second, while this technique works, it’s not lightweight, so it should only be used in
setup and configuration scripts.

Wrapping it up

Using PHP itself, to check if the requirements posed by your application are satisfied,
should be quite easy. Often however PHP applications seem to rely too much on text files to provide important information. In PHP all the tools and handles you need to check the requirements are available, and with HTML-producing PHP, you can easily provide links and guidance t the user, to help them get any missing pieces, to successfully install you application; so do that please.