CodeIgniter Preflight Checker

In my post on the subject, I discussed do's and don'ts for making a PHP application portable. In this post, I will show you how to create a preflight checker in the CodeIgniter PHP framework to ensure that an environment will support your application.

Setting Up the Hook

The best way to setup a preflight checker is to create a hook. Make sure that you enable hooks in your main configuration file before getting started. We'll be setting up a pre-system hook, so add the following code to your application/config/hooks.php file:

/*Preflight Check Hook*/
$hook['pre_system'] = array(
    'class' => '',
    'function' => 'preflight_check',
    'filename' => 'preflight_check_hook.php',
    'filepath' => 'hooks'
);

This will tell CodeIgniter to run this hook every time the system starts. As you can infer from the above code, we'll now have to create a file in the application/hooks/ folder (if it doesn't exist, create it) named preflight_check_hook.php and add a function to that file named preflight_check():

/**
 * Preflight Check checks all of the prerequistes before the first
 * pageview for each session.
 *
 * If a problem is found, the program halts execution.
 */
function preflight_check()
{
  //all your checks go here
}

Now we're ready to add checks so we can test the server environment.

What to Check

Technically, you can check whatever you want here, but I tend to check the things that are likely to be different from one server environment to the next. Some of the more important ones are:

  • Whether the version of PHP or CodeIgniter is new enough to support your application. You can check this by using the phpversion() function
  • Necessary PHP Extensions installed? You can check this by using the function_exists() function.
  • PHP.INI variables set correctly? You can check this by using the ini_get() function.
  • Whether certain folders are really writeable on the server or no
  • Whether files exist or not.

Of course, you can check for most anything you want to in the preflight check, and even try to tweak the environment a little using ini_set(). What you can't do is access most of the functionality in CodeIgniter, since this hook will run before most of your libraries are loaded. There are a few useful CodeIgniter variables and functions that you will have access to, though:

  • show_error() -- For printing out errors (non CodeIgniter users should use the die() function)
  • is_really_writable -- For testing if a file is writable or not (non CodeIgniter users should use the is_readable() function)
  • $application_folder and $system_folder

Here's an example of one of a check in one of my applications:

function preflight_check()
{
    global $application_folder;
    global $system_folder;

    //Ensure proper version of PHP
    if (phpversion() < '5.1')
      show_error("This application requires at least PHP 5.1 to run properly!");

    //Ensure SQLITE extension is installed
    if ( ! function_exists('mysql_connect'))
      show_error("The sqlite PHP extension is required to run this application!  Refer to the PHP documentation for this extension for more information.");

    //Ensure GD2 is is installed
    if ( ! function_exists("gd_info"))
        show_error("The PHP GD2 image library is required to run this software.  Check with your server administrator or hosting provider, or manually install it.)");

    //Check memory limit and attempt to set it to what this application needs
    if (get_bytes(ini_get('php_value memory_limit')) < 33554432)
    {
        if( ! ini_set('php_value_memory_limit', '32m')
            show_error("The memory limit in your PHP.INI file must be set to at least 32 megabytes (32m).");
    }
}

What's the Advantage over just letting the program fail on its own?

Technically, nothing; your preflight checker will check things that would normally cause your application to fail at some point in the execution anyway. This is simply a way for you to alert your admins/developers/users that they need to fix their environment right off the bat. Plus it's more graceful and clean than PHP error messages.

Practically, however, this may save a lot of headaches, especially if you distribute the application or expect people to download a copy onto their own desktops for development. Oh, and if your users/developers/admins have PHP error reporting turned off on their system, then a carefully coded preflight checker will save them from getting a mysterious blank screen when the environment doesn't match the requirements.

Keeping this thing from running for every page view

You probably won't want this script to run every time a page is loaded on your site, especially if the script includes file operations, lest your server take a performance hit. Typically, you will want to run this preflight checker only once per session. By adding a few lines of code to the top and bottom of this function, you can make it run only onc per session (unless it fails).

function preflight_check()
{
    //See if this has already been run during the current session
    session_start();
    if (isset($_SESSION['preflight_check_pass']) && $_SESSION['preflight_check_pass'] == 'pass')
        return TRUE;

    global $application_folder;
    global $system_folder;

    //Ensure proper version of PHP
    if (phpversion() < '5.1')
      show_error("This application requires at least PHP 5.1 to run properly!");

    // All of the
    // rest of your checks
    // go in here...

    // ...

    //Woo hoo.. the check passed!
    $_SESSION['preflight_check_pass'] = 'pass';
}

You'll notice that this example uses PHP's built-in session system rather than CodeIgniter's session library. This is because CodeIgniter's will not have been loaded yet when this hook runs, and because we're not storing any particularly sensitive information in this session, so we don't have to take advantage of CI's advanced session security features.

Well, that's about it for now. If you have any comments or see any issues with the examples in today's post, feel free to share by leaving a comment.


This article was published on June 22, 2009 by Casey McLaughlin.

You can find more articles and other stuff on my website.