Setting the http referer with PHP CURLPosted in PHP -
The PHP CURL functions use the libcurl library to allow you to connect to various servers and different protocols. If the http referer string is not explicitly defined then nothing will be sent to the web server, but there may be times when you need to pass this along with your request.
There may be times you need to do this if you need to scrape a web page which is expecting some sort of referring URL, or in order to test some code in your own project which requires a refering url to be sent. There are other blackhat reasons but I won't go into those here (and nor do I use them myself).
The PHP code
The following PHP example requests the page at http://www.example.com/2 and sets the referring page as http://www.example.com/1. The latter URL will then show up in the weblogs as the page the visitor came from. The output from http://www.example.com/2 is then saved to the $html variable.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/2'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/1'); $html = curl_exec($ch);
The CURLOPT_REFERER line is where the referer string is set. Simply set whatever URL you require as the string passed to it.
The "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);" line makes it so the call to curl_exec returns the HTML from the web page as a variable instead of echoing it out to standard output.