home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / httpheader.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  5.9 KB  |  152 lines

  1. Http headers 
  2.  
  3. This gets the http response headers for a given url and returns them in an assoc. array. i.e. 
  4.  
  5. <?php
  6. #       get_http_headers()
  7. #       v1.3 - 1999/06/08
  8. #
  9. #       Copyright (c) 1998,1999 easyDNS Technologies Inc.
  10. #       http://www.easyDNS.com
  11. #       info@easyDNS.com
  12. #       All rights reserved.
  13. #
  14. #       This code provided "As Is" with no warrantees express or implied.
  15. #       The author and contributors are not liable for anything good or
  16. #       bad that results from your use of this code.
  17. #
  18. #       You are free to distribute this for free provided this notice is
  19. #       included. Please forward fixes/enhancements to the author for
  20. #       inclusion in the next revision.
  21. #
  22. #       USAGE:
  23. #               array get_http_headers( str url, str [proto], int [timeout]);
  24. #
  25. #               url is in the form "http://www.somewhere.com" or "www.somewhere.com"
  26. #               proto is optional, default is "HTTP/1.0"
  27. #               timeout is optional, default is 10 (seconds)
  28. #
  29. #               array is an associative array with the keys set to the header name
  30. #               (lowercase) with the first line of headers (the result line) 
  31. #               split up into:
  32. #                       $array[protocol] = protocol server used to answer
  33. #                       $array[result] = i.e. 200 or 301 or 403 or 404, etc
  34. #                       $array[message] = i.e. "OK" or "FORBIDDEN" etc.
  35. #
  36. #               $array[time_used] will be the approximate number of seconds the 
  37. #               that the request took
  38. #
  39. #               If the request times out, $array[result] will be set to 502.
  40. #
  41. #               If the URL is invalid, false is returned
  42. #
  43. #
  44. #       HISTORY:
  45. #               
  46. #               v1.3
  47. #               1999/06/08
  48. #               from Mark Jeftovic, markjr@easyDNS.com
  49. #                       - reduced buffer size in polling loop to 80 from 128                    
  50. #                       - setsockblocking = 0 by default but 1 if proto
  51. #                         request is HTTP/1.1 (in which case our own 
  52. #                         timeout is ignored for some reason)
  53. #                       - only send Host header for HTTP/1.1 requests
  54. #                       - stripped extra \n before Host header
  55. #                       - moved parsing to parse_output() function so
  56. #                         we can still return partial headers on a timeout
  57. #
  58. #               v1.2
  59. #               1999/02/24
  60. #               from Colin Viebrock, cmv@easyDNS.com
  61. #                       Speed enhancements and rewrite:
  62. #                       - check that host exists before trying to open socket
  63. #                       - socket blocking for a timeout
  64. #                       - returns $array[time_used] ... just for fun.
  65. #
  66. #               v1.1
  67. #               1998/11/24
  68. #               from Gary E. Bickford, garyb@slb.com
  69. #                       Fix for when $path turns out to be null
  70. #
  71. #               v1.0
  72. #               from Mark Jeftovic, markjr@easyDNS.com
  73. #                       Original code
  74. #
  75. #
  76.  
  77. function get_http_headers($url, $proto="HTTP/1.0", $timeout=10) {
  78.         $return = false;
  79.         if (substr($url,0,7)=="http://") {
  80.                 $url = substr($url,7);
  81.         }
  82.  
  83.         $parts = parse_url("http://".$url);
  84.         
  85.         $ips = gethostbynamel($parts["host"]);
  86.  
  87.         if ($ips[0]) {
  88.                 $ip = $ips[0];
  89.                 $host = $parts["host"];
  90.                 $path = ($parts["path"]) ? $parts["path"] : "/";
  91.                 $port = ($parts["port"]) ? $parts["port"] : 80;
  92.  
  93.                 $start = time();
  94.                 $timeout = $timeout + $start;
  95.  
  96.                 if($sock = fsockopen($host, $port)) {
  97.                         set_socket_blocking($sock, 0);
  98.                         switch($proto) {
  99.                                 case "HTTP/1.1":
  100.                                         set_socket_blocking($sock, 1);
  101.                                         fputs($sock, sprintf("HEAD %s %s\n", $path, $proto));
  102.                                         fputs($sock, sprintf("Host: %s\n\n", $host));
  103.                                         break;
  104.                                 default:
  105.                                         fputs($sock, sprintf("HEAD %s %s\n\n", $path, $proto));
  106.                                 }
  107.  
  108.                         while(!feof($sock) && $t<$timeout) {
  109.                                 $line .= fgets($sock,1);
  110.                                 $t = time();
  111.                         }
  112.                         fclose($sock);
  113.                         $end = time();
  114.  
  115.                         if ($t>=$timeout) {
  116.                                 $http = parse_output($line);
  117.                                 $http["result"] = 502;
  118.                                 $http["message"] = "Timed Out";
  119.                                 $http["time_used"] = $end - $start;
  120.                                 $return = $http;
  121.                         } elseif($line) {
  122.                                 $http = parse_output($line);
  123.                                 $http["time_used"] = $end - $start;
  124.                                 $return = $http;
  125.                         }
  126.                 }
  127.         }
  128.         return $return;
  129. }
  130.  
  131. function parse_output($line) {
  132.         $lines = explode("\n", $line);
  133.         if(substr($lines[0],0,4)=="HTTP") {
  134.         list($http["protocol"], $http["result"], $http["message"]) = split("[[:space:]]+",$lines[0],3);
  135.         } else if(substr($lines[0],0,7)=="Server:") {
  136.                 $http["server"] = substr($lines[0],8);
  137.         }
  138.         for ($i=1; $i<count($lines); $i++) {
  139.                 list($key, $val) = split(":[[:space:]]*", $lines[$i],2);
  140.                 $key = strtolower(trim($key));
  141.                 if ($key) {
  142.                         $http[$key] = trim($val);
  143.                 } else {
  144.                         break;
  145.                 }
  146.         }
  147.         return($http);
  148. };
  149. ?>
  150.  
  151.