jMar"s Blog DevSmash Developer Portal

Tuesday, April 22, 2008

Know Your Environment: PHP Server Module Reporter

This post was authored by David Walsh. To learn more about David, click here.

One of the unfortunate parts on my job is that I rarely get a say in the customer's hosting environment. We don't host our customers' sites so I'm usually at the mercy of whichever hosting provider the customer has contracted with. We usually try to steer our customer toward our preferred host, but most of the time the customer is reluctant to switch because they fear email issues and change in general.

Knowing the hosting environment's limitations before we begin to develop the website is a must. Imagine creating a great website and running into issues later on because the shared hosting server doesn't have a given extension installed. A nightmare!

I've created a basic PHP script that I simply FTP to the server and use to evaluate extensions on the server. I simply supply the extensions I require and the script does the rest of the work.

<?php
 /* set required extensions */
 $my_required_extensions = array(
  'gd',  //graphics library
  'xml',  //xml
  'mysql', //database
  'curl',  //networking
  'openssl', //site will need SSL
  'pecl'  //pear
 );
 natcasesort($my_required_extensions);
 
 //get loaded modules
 $loaded_extensions = get_loaded_extensions();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title><?php echo $source_article; ?> Example</title>
 <meta name="description" value="<?php echo htmlentities($meta_description); ?>" />
 <style type="text/css">
  body  { font-size:12px; }
  h2   { border-bottom:1px solid #ccc; font-weight:normal; font-size:18px; margin-bottom:5px; padding-bottom:2px; }
  p   { line-height:20px; margin-top:0; }
  .found  { background:lightgreen; padding:5px 10px; margin:0 0 10px 0; }
  .miss  { background:pink; padding:5px 10px; margin:0 0 10px 0; }
  .extra  { background:lightblue; padding:5px 10px; margin:0 0 10px 0; }
 </style>
</head>
<body>

<h1><?php echo $_SERVER['HTTP_HOST']; ?></h1>

<h2>General Information</h2>
<p>
 <strong>Server Software:</strong>  <?php echo $_SERVER['SERVER_SOFTWARE']; ?><br />
 <strong>Document Root:</strong> <?php echo $_SERVER['DOCUMENT_ROOT']; ?><br />
 <strong>PHP Version:</strong> <?php echo phpversion(); ?>
</p>

<h2>Extension Check</h2>
<?php 
 /* print out */
 //analyze results
 foreach($my_required_extensions as $ext)
 {
  if(in_array($ext,$loaded_extensions))
  {
   $matches[] = strtolower($ext);
  }
  else
  {
   $missings[] = strtolower($ext);
  }
  unset($loaded_extensions[$ext]);
 }
 //print out results
 natcasesort($matches); natcasesort($missings); natcasesort($loaded_extensions);
 foreach($matches as $match) { echo '<div class="found"><strong>',$match,'</strong> found!</div>'; }
 foreach($missings as $miss) { echo '<div class="miss"><strong>',$miss,'</strong> missing!</div>'; }
 foreach($loaded_extensions as $e) { if(!in_array($e,$matches) && !in_array($e,$missings)) { echo '<div class="extra"><strong>',$e,'</strong> is available.</div>'; } }
?><br />

</body>
</html>

The output looks as follows:

Extension Evaluator Screenshot
(click to enlarge)

If I run into the situation where a needed extension isn't there, I must decide whether to push for the customer to switch hosts or adjust the way I code the website. You'll also want to keep a copy of this on hand so that you have proof when the host changes the server and causes issues with the website. Happy coding!

David Walsh.David Walsh is a Senior Web Developer residing in Madison, WI. He spends most of his day getting a monitor tan coding PHP, CSS, MooTools, and XHTML. When not tinkering with the newest version of Moo, David uses soccer, Rock Band, and movies to dull his coding pains.



5 comments:

Jesus said...

What about your mysql version? Though I am not sure how to get this without phpinfo() or knowing how to connect to the database in advance.

Nice script though

David said...

Great suggestion Jesus! To get the MySQL version, you could add:

echo("MySQL server version: ". mysql_get_server_info());

CédricG said...

If there is zero missing extension I have a big error* about $missing does not exists.
But nice idea!

*yes, I have XDebug ;-)

David said...

@CédricG: How luck you are to not be missing any extensions! Use the prevent that error:

if(count($missings)) {
foreach($missings as $miss) { /* code here */ } }

terminals-blocks said...

I have a big error* about $missing does not exists