Alter HTTP_REFERER and/or User Agent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
QuackFuzed
Forum Newbie
Posts: 2
Joined: Thu Aug 30, 2007 2:00 pm

Alter HTTP_REFERER and/or User Agent

Post by QuackFuzed »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am a **long-time** CF guy who has been forced to do some quickie stuff in PHP.  Unfortunately, I don't know jack about PHP, beyond the basics.  I have a very simple script that I've written (see below) that performs an http get request on the provided URL, and then spits out the resulting page.  I am wondering if there is an easy way to enhance it by allowing for the alteration of the 'referrer' and/or the 'user agent' that is sent with the request?

Code: Select all

<?php
function fetchURL( $url ) {
   $url_parsed = parse_url($url);
   $host = $url_parsed["host"];
   $port = $url_parsed["port"];
   if ($port==0)
       $port = 80;
   $path = $url_parsed["path"];
   if ($url_parsed["query"] != "")
       $path .= "?".$url_parsed["query"];

   $out = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";

   $fp = fsockopen($host, $port, $errno, $errstr, 30);

   fwrite($fp, $out);
   $body = false;
   while (!feof($fp)) {
       $s = fgets($fp, 1024);
       if ( $body )
           $in .= $s;
       if ( $s == "\r\n" )
           $body = true;
   }
  
   fclose($fp);
  
   return $in;
}

function isValidURL($url) {
	return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

if (isset($_GET['lnk']) && isValidURL($_GET['lnk'])) {
	echo fetchURL($_GET['lnk']);
} else {
	echo "An invalid URL was provided.";
}
?>
Thank you in advance for your assistance in bringing me over "from the dark side" ;)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Take a look at this user comment for fsockopen()

http://www.php.net/manual/en/function.f ... .php#37517
QuackFuzed
Forum Newbie
Posts: 2
Joined: Thu Aug 30, 2007 2:00 pm

Post by QuackFuzed »

Jcart,

You, my friend, are DA MAN! :)

Thank you!
Post Reply