Adding HTTP Referrer in header() function

Discussion in 'PHP' started by krishmk, Nov 25, 2010.

  1. #1
    Just curious if it is possible to add HTTP REFERRER using PHP's header() function.
    Appreciate your response...
     
    krishmk, Nov 25, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $_SERVER['HTTP_REFERER'], empty if not set

    MAKE SURE YOU SANITIZE IT, IT CAN BE ANYTHING, NOT JUST A URL
     
    krsix, Nov 25, 2010 IP
  3. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #3
    No not trying to find the referrer for my site..
    I want to include a fake referrer from my site to a third party site.

    For instance if I redirect a visitor to third party site.. They shouldnt recognize my site has "http_referrer".. they should see whatever url I assign from my site.
    Is this possible?
     
    krishmk, Nov 25, 2010 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Based on RFC 2616, the syntax is:

    
    header("Referer: http://google.com");
    header("Location: http://yahoo.com.php");
    
    PHP:
    However, this never seems to work for me. I always use fsockopen or curl instead when I need to fake the headers.
     
    ThePHPMaster, Nov 25, 2010 IP
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Referrer is defined on the clientside, you can't just "fake a referrer"
     
    krsix, Nov 25, 2010 IP
    krishmk likes this.
  6. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #6
    As krsix already said, referrer header is sent by the browser. You can only fake it if you try to make a HTTP request.
    And the short answer is no, you can't make a client's browser send fake headers like that.
     
    Gray Fox, Nov 25, 2010 IP
  7. HalvinCarris

    HalvinCarris Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you are using PHP, you can spoof the referer with cURL:

    $ch = curl_init("http://google.com");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Referer: http://digitalpoint.com'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch); 
    curl_close($ch);
    
    PHP:
    Checkout the cURL manual at php.net for more info on cURL, it's very useful :)
     
    HalvinCarris, Nov 26, 2010 IP
  8. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #8
    Thanks for replying.. However cURL will not work for me as I dont intend to serve the content of third party website into my site.
    I just wanted my visitors to be redirected...
    After googling I found a double meta refresh would help to achieve my intention...
     
    krishmk, Nov 26, 2010 IP