Getting Status Code and Server Type with PHP
What is Server Status Code?
A status code is a number and a label, like "404 Not Found" and "500 Internal Server Error."
What is Server Type?
The server type (the server operating system) is the software being used and, sometimes, a list of helper software.
Useful Software
That information can useful while testing software. Knowing the server type where a client's domain is on can be useful when recommending software.
Do you use .htaccess or other methods to redirect certain web pages? Use this software to verify things are working as they should. For example, if you enter "willmaster.com", you'll see both "301 Moved Permanently" and "Location: /". If you then enter "www.willmaster.com", you'll see a "200 OK" status.
How To Read the Web Page Header Information
The status code is on the first line of the information the server provides. Other information follows.
And, of course, it can also be good to see what others can see about our own servers, should they be inclined to snoop.
Determining Server Operating System
We use a version of the PHP server snooper software presented in this article to let customers test their own server to see if they meet operating system requirements. The checker is on the software description pages.
If you want to see how one works, a server type checker is on the Master Form V4 description page. Give it a spin.
Viewing Actual Redirect Status Code
Many hosting accounts are configured to redirect to a different web page when certain errors are found, like the "500." The PHP server snooper script with this article lets you see that real error code regardless which web page the hosting account is configured to display instead.
Independently obtaining a status code can be a real time saver when installing software and the server keeps spawning errors. You know what the real error code is, not just a redirect page.
Copy 'n Paste PHP Script
The PHP script that comes with this article is complete. There is nothing to add or remove or customize. Copy 'n paste.
Here is the PHP server snooper script:
<html> <head> <style type="text/css"> body { margin-left:200px; margin-top:50px; width:500px; font-family:verdana,sans-serif; } input, textarea { width:500px; font-family:monospace; } .nowrap { white-space:nowrap; } </style> <body> <!-- To paste this form into an existing web page, copy from here to just above the cancel </body> tag. The web page needs to be PHP, usually with .php file name extension. --> <form method="GET" action="<?php echo($_SERVER['PHP_SELF']) ?>"> <h3><a href="/library/">Possibilities Ezine</a> Server Snooper</h3> <?php $host = $uri = ''; if( isset($_GET['host']) ) { $host = $_GET['host']; $uri = ( isset($_GET['uri']) and strlen($_GET['uri']) > 0 ) ? $_GET['uri'] : '/'; $content = ''; $fp = fsockopen("$host", 80, $errno, $errstr, 30); if(!$fp) { echo "$errstr ($errno)<br />\n"; return; } else { fwrite($fp,"HEAD $uri HTTP/1.0\r\n"); fwrite($fp,"Host: $host\r\n"); fwrite($fp,"Connection: Close\r\n"); fwrite($fp,"\r\n"); while (!feof($fp)) { $content .= fgets($fp, 128); } fclose($fp); } echo "<p>Results for <a href="\"http://$host$uri\">http://$host$uri</a></p>";" echo '<textarea cols="55" rows="9" wrap="off">'; echo $content; echo '</textarea><br /><hr /><br />'; } ?> <p> What is the host name (the domain name without the "http://" part)?<br /> <input type="text" name="host" size="55" value="<?php echo($host) ?>"> </p> <p> What is the URI (the part of the URL after the domain name, or "/" if default page)?<br /> <input type="text" name="uri" size="55" value="<?php echo($uri) ?>"> </p> <p> <input type="submit" name="submit" value="Yes, let's snoop." style="text-align:left"> </p> </form> <!-- To paste this form into an existing web page, copy from just below the begin <body> tag to this point. The web page needs to be PHP, usually with .php file name extension. --> </body> </html>
The PHP server snooper source code is provided as a complete web page. Upload it to your server and you're good to go. To use the software, type its URL into your browser.
If you scan the code, you'll see it has the beginning and ending marked should you wish to transfer the software to one of your existing PHP web pages. Copy the code between those marks and paste it where it is to be used.
Will Bontrager