Re: [blogite] PingBack client idea: Send a referer header"

Date view Thread view Subject view Author view Attachment view

From: Simon Willison (simon@incutio.com)
Date: Thu Sep 12 2002 - 12:12:56 BST


At 13:06 12/09/2002 +0100, you wrote:
>This is a very cool idea, though I'm having trouble figuring out how to
>send HTTP headers to begin with :P
>I noticed your pingback server source included a file named safeget.php or
>something (I forgot to save the server code, and it seems you deleted it
>from Bath webspace). Any pointers to how safeget.php works ?

Oops... I should probably put that back up somewhere. Here's the code:

function safeGet($url, $sizelimit = 102400, $timelimit = 5) {
     $bits = parse_url($url);
     if (!isset($bits['host'])) {
         return false;
     }
     $host = $bits['host'];
     $path = isset($bits['path']) ? $bits['path'] : '';
     if (isset($bits['query'])) {
         $path .= '?'.$bits['query'];
     }
     if (!$path) {
         $path = '/';
     }
     $port = isset($bits['port']) ? $bits['port'] : 80;
     // Try to connect to the server at $host
     $fp = fsockopen($host, $port, $errno, $errstr, $timelimit);
     if (!$fp) {
         return false;
     }
     socket_set_timeout($fp, $timelimit, 0);
     // Send the GET request
     $request = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
     fputs($fp, $request);
     // Start receiving headers
     $contents = '';
     $gettingHeaders = true;
     while (!feof($fp)) {
         $line = fgets($fp, 4096);
         if (trim($line) == '') {
             $gettingHeaders = false;
         }
         if (!$gettingHeaders) {
             $contents .= trim($line)."\n";
         }
         if (strlen($contents) > $sizelimit) {
             break;
         }
     }
     return $contents;
}

If you want to send a referer header as well (indicating that $url was the
referer) change the '$request = ' line to this:

$request = "GET $path HTTP/1.0\r\nHost: $host\r\nReferer: $url\r\n\r\n";

Cheers,

Simon

-- 
Web Developer, www.incutio.com
Weblog: http://simon.incutio.com/ 
Message sent over the Blogite mailing list.
Archives:     http://www.aquarionics.com/misc/archives/blogite/
Instructions: http://www.aquarionics.com/misc/blogite/

Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.5 : Thu Sep 12 2002 - 13:05:00 BST