Using 'fsockopen' to connect to a remote web server and retrieve the HTTP server software response, similar to NetCraft.

Category: Software and Systems Engineering
Author: Sean O'Donnell
Thu, Feb. 20th, 2003 @ 2:20:22 (MST)

This simple function will return results similar to NetCraft regarding the Web Server Software of a Remote Domain.

<?php
/**
  * get_remote_response($str_domain,$int_port)
  * fetches the remote http server's software response (similar to netcraft)
 */
function get_remote_response($str_domain,$int_port) { 
    
/** Open Internet or Unix domain socket connection */
    
$obj_sock fsockopen($str_domain,$int_port,&$int_error,&$str_error); 
    if(!
$obj_sock) { 
        
/** error, create error msg for the string response */
        
$str_results "Error: "$str_error ." ("$int_error .")<br>\n"
    } else {
        
/** 
         * connection established
         * request HTTP header information
        */
        
fputs($obj_sock,"GET / HTTP/1.0\n\n"); 
        
/**
         * while the connection is reading a response...
        */
        
while(!feof($obj_sock)) {
            
/**
             * get the each line of information returned
            */ 
            
$str_results fgets($obj_sock,4096); 
            
            if (
preg_match("/Server:/i",$str_results)) { 
                
/**
                 * return the Server Information from within the Results
                */ 
                
$str_results trim(preg_replace("/Server:/","",$str_results)); 
                break; 
            } 
        } 
        
fclose($obj_sock); 
        if (!
$str_results) { $str_results 'No Results Returned'; }
    } 
    return(
$str_results); 

print 
get_remote_response('seanodonnell.com',80);
?>

The results from the example above, should like something similar to the following response:

Apache/1.3.31 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.8 FrontPage/5.0.2.2634a mod_ssl/2.8.18 OpenSSL/0.9.6b PHP-CGI/0.1b

Copyleft (<) 1998-2019 www.seanodonnell.com