<?php
/*
Copyright (c) 2005 Sam J. Clarke
All rights reserved.

Script Name: Get Headers
Author Name: Sam J. Clarke
Author Email: admin@free-php.org.uk
Author URI: http://www.free-php.org.uk/
Description: This script will get the headers of any web page.
Instructions: To use it copy all the code a put it
in a file called headerveiw.php on your webserver.
Then just run it, If you need any help just email
me.


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html
*/
if(isset($_GET['url'])) {
  function 
getheaders($url) {
    
// parse url ready to use
    
$info parse_url($url);
    
// open socket
    
$fp = @fsockopen($info[host], 80$errno$errstr10);
    
// if false socket display error
    
if (!$fp) {
       print 
"Error: The url you entered seems not to exist.\n";
    } else {
      
// check the paths not empty if it is replace with
      
if(empty($info[path])) {
         
$info[path] = "/";
      }
      
// HTTP heders to send to get the web page
      
$out  "GET ".$info[path]."".$info[query]." HTTP/1.0\r\n";
      
$out .= "Host: ".$info[host]."\r\n";
      
$out .= "Connection: close \r\n";
      
$out .= "User-Agent: free-phpdotorgdotuk header veiwer/1.0\r\n\r\n";
      
// write the headers out
      
fwrite($fp$out);
      
// create var to store html
      
$html '';
      while (!
feof($fp)) {
        
// read HTML
        
$html .= fread($fp8192);
      }
    }
    
// close socket
    
fclose($fp);
    
// split the headers and HTML
    
$pieces explode("\r\n\r\n"$html,2);
    
// save headers
    
$headerinfo =  $pieces[0];
    
// get rid of html
    
unset($pieces);
    
// return headers
    
return $headerinfo;
  }
  
// call get header function
  
$headers getheaders($url);
  
// get it ready to be displayed
  
$headers str_replace("\r\n","<br>\r\n",$headers);
  print 
"Below are th headers returned:<br><br>\n";
  
// Display headers
  
print "".$headers."";
} else {
  print 
"<form action=\"headerveiw.php\" method=\"get\">\n";
  print 
"Enter the page url you want to see the headers of in the box below:<br>\n";
  print 
"<input type=\"text\" name=\"url\" value=\"http://\" size=\"40\">\n";
  print 
"<input type=\"submit\" value=\"Check links\">\n";
  print 
"</form>\n";
}
?>