• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Setting the http referer with PHP CURL

Setting the http referer with PHP CURL

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.

Why?

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);

CURLOPT_REFERER

The CURLOPT_REFERER line is where the referer string is set. Simply set whatever URL you require as the string passed to it.

CURLOPT_RETURNTRANSFER

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.

Check Out These Related posts:

  1. PHP’s file_get_contents and CURL functions
  2. Submitting a form post with PHP and CURL
  3. Setting the user agent with PHP CURL
  4. Determine whether PHP is being run via HTTP or CLI

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap