1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

code to show referer in page and output results

Discussion in 'PHP' started by mattjack, Apr 4, 2020.

  1. #1
    Hello

    Could someone help me out with some simple php code to display the http referer in a page.

    I know how to detect it, but want to output the results to the page.

    <?php echo $_SERVER['HTTP_REFERER']; ?>

    Show the referer, and if referer is blank show message : referer is blank

    I'll chuck $5 to who ever can help me out :)

    Thanks
     
    mattjack, Apr 4, 2020 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    <?php
    if( isset( $_SERVER['HTTP_REFERER'] ) && $_SERVER['HTTP_REFERER'] != "" ){
    echo "<p> Referer: ". $_SERVER['HTTP_REFERER']. "</p>";
    }else{
    echo "<p> No Referer </p>";
    }
    ?>
     
    JEET, Apr 4, 2020 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Even simpler:

    echo empty($_SERVER['HTTP_REFERER']) ? 'Referrer is blank' : $_SERVER['HTTP_REFERER'];
    Code (markup):
    Ternary operators are your friend.

    Also keep in mind that "referer" is misspelt. Whilst PHP / HTTP uses HTTP_REFERER the actual word you should use in your output is referrer. There's no such word as "referer" in English.
     
    deathshadow, Apr 5, 2020 IP
    JEET likes this.
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    further hint php.net is amazing
    var_export variables so you can see what's in them, what the right spelling is.

    Not getting anything from $_SERVER['HTTP_REFERER'], then try $_SERVER

    Do not give links back on your page to your referrers, you'll be spammed to death to get you to link to dodgy sites.
     
    sarahk, Apr 5, 2020 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    deathshadow, Apr 5, 2020 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    @deathshadow - which bit?
    My debugging technique?
    My getting alarmed at people using Referer?
     
    sarahk, Apr 5, 2020 IP
    JEET likes this.
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    That part. I mean so long as you set the anchor to rel="nofollow noindex" you should be fine.

    Though I'd assume this is related to some sort of buffered logging. I often set buffers up so I can echo out certain logs during my processing stage, then switch them off (or set to a gzhandler) for my output stage. Separation of concerns and all that. Also easier during testing to route echo to output instead of the logs by just changing one line. Same code, handles both functions.
     
    deathshadow, Apr 5, 2020 IP