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.



15 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

Muhammad Nabeel said...

i will test it after my busy schedule. Its looking awesome and helpful.
home if you need more help you can contact.

Anonymous said...

Thanks for the useful list. However I have been using couple of apps from the list one of them is Pinterest which is awesome.

https://www.trademarkattorneys.com/

GladysHardeman said...

you have altered it; you could use Traveler app on your Android fildo | mp3 downloader, android application, listen or download ... we will certainly update the info here.you wish to hear your preferred songs.

Akshi Sharma said...


good offline setting: Tap the major menu button, after that toggle Fildo Music App chance that you currently recognize how to download nice.
download Fildo

Akshi Sharma said...


Click here to get
aptoide for android

Akshi Sharma said...

NIce post
Wimbledon stream

Akshi Sharma said...

IMPORTANT NOTIFICATION FOR SSC..
SSC JE Admit Card

Huong Nguyen said...

Shop hoa TPHCM chuyên các sản phẩm hoa tươi đặc biệt hoa tặng sinh nhật đẹp với đủ thể loại hoa như hoa tang lễ, hoa khai trương, hoa sinh nhật tặng sếp, hoa đẹp sinh nhật tặng người yêu, hoa tặng sinh nhật bạn bè, ông bà, cha mẹ…Dịch vụ điện hoa tươi chuyên nghiệp sẵn sàng mang đến khách hàng những bó, lẵng, giỏ hoa đẹp sinh nhật độc đáo nhất khắc các quận 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, tân bình, tân phú, phú nhuận, bình thạnh, gò vấp, bình tân, huyện bình chánh, hóc môn, nhà bè, củ chi…

https://www.usacheckcashingstore.com/san-diego said...

It appears borrowers incline toward this sort of here and now credit, instead of longer term advances which are around for any longer, and can cost the same amount of cash, if not more.

www.usapaydayloanstore.com/chicago said...

Comfort: One of the most essential factors that goes for payday advances is the accommodation with which you can acquire these credits. Ordinarily when you obtain a customary advance, you have to set yourself up for an unpredictable procedure of utilization, supporting archives, credit check, plan of guarantee et cetera.