PHP - Referer redirect script

user5880 picture user5880 · May 13, 2009 · Viewed 34.6k times · Source

Often, when searching for answers, I have found that certain websites will allow you to read the information they offer if the referer is, for example, google.com. Yet, if you link directly to the information, it will be unavailable.

What I am looking for is the smallest PHP script that will set a referer of my choice, and a destination, like so:

http://example.com/ref_red.php?referer=http://google.com/&end=http://example.net/

Notes:

  • ref_red.php is the name of the script on my example.
  • referer and end should accept http, https, ftp.
  • referer and end can contain an URI of any type, as simple as http://end.com or as complicated as: http://example.com/some/rr/print.pl?document=rr, for example.

NOTE: As recommended on a reply, I am adding this. The script isn't a full proxy per se. Only initial HTTP request would be proxied (not subsequent requests like images,etc) for the sole purpose of setting the referer.

Answer

bumperbox picture bumperbox · May 14, 2009

this function should give you a starting point it will fetch any http url with the specified referrer

handling the query parms should be pretty trivial, so i will leave that part for you to do

<?php

    echo geturl('http://some-url', 'http://referring-url');

    function geturl($url, $referer) { 

        $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg,text/html,application/xhtml+xml'; 
        $headers[] = 'Connection: Keep-Alive'; 
        $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
        $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 

        $process = curl_init($url); 
        curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($process, CURLOPT_HEADER, 0); 
        curl_setopt($process, CURLOPT_USERAGENT, $useragent);
        curl_setopt($process, CURLOPT_REFERER, $referer);
        curl_setopt($process, CURLOPT_TIMEOUT, 30); 
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 

        $return = curl_exec($process); 
        curl_close($process); 

        return $return; 
    } 

?>