referer...

Discussion in 'PHP' started by deemainer, Nov 14, 2008.

  1. #1
    hi all

    noob to php and ive been trying to work this out for a bit.Hoping someone might be kind enough to help.

    If i have a website that has a 404 set up to redirect to another site. Is there a way of displaying the referer url and also main domain that the person was bounced from ?

    Ive tried this:

    $referer = $_SERVER['HTTP_REFERER'];
    $refererhome = $_SERVER['HTTP_HOST'];


    and then echo results etc but it doesnt seem to work ! I just get nothing for referer and referhome gives main url of site that script is on not where bounced from.

    Any ideas???? thanks
     
    deemainer, Nov 14, 2008 IP
  2. penalty

    penalty Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    hi,
    most new browsers hide the referer, thats the problem. actually the php code itself should work.
     
    penalty, Nov 14, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Try making a simple file that contain a link to your php file that contain the HTTP_REFERER thing
     
    ads2help, Nov 14, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Where did you read this ?
     
    joebert, Nov 14, 2008 IP
  5. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #5
    ok...so made a simple link on other url as suggested by ads2elp :)

    The script i direct too now showed the referring link $referer = $_SERVER['HTTP_REFERER']; as correct address from where it came.

    However still showed host $refererhome = $_SERVER['HTTP_HOST']; as server that script was running on and not site redirected from.

    So it appears in terms of a 404 there is no referring url?? is that right ? And still doest explain why host doesnt display from redirected site.

    confused....and thanks for your help.
     
    deemainer, Nov 14, 2008 IP
  6. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    HTTP_HOST is suppose to show the host it is coming from.

    You need to probably regex the referer to get the main domain name
     
    atlantaazfinest, Nov 14, 2008 IP
  7. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #7
    aaah...so HTTP_HOST shows the host that the script is on not referring host?

    Any ideas how i do the regex then? thanks.:)
     
    deemainer, Nov 14, 2008 IP
  8. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    $referer = $_SERVER['HTTP_REFERER'];
    preg_match('@^(?:http://)?([^/]+)@i', $referer, $matches);
    $host = $matches[1];
    echo $host; // or do something with it
    
    Code (markup):
     
    atlantaazfinest, Nov 14, 2008 IP
    deemainer likes this.
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    Check
    $_SERVER['REMOTE_HOST']
    Code (markup):
    It's only there on some setups.
     
    joebert, Nov 14, 2008 IP
  10. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #10
    atlantaazfinest THANK YOU ! That worked for the link and getting host ! Your a star.Rep added

    So now all i need to do is to be able to get referer from a 404 redirect.Is that impossible?
     
    deemainer, Nov 14, 2008 IP
  11. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No problem if the 404 is directly redirecting to your script the header should have the referer in it. unless like stated before referer info is stripped at browser level
     
    atlantaazfinest, Nov 14, 2008 IP
  12. nepasolutions

    nepasolutions Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    it is really help full for me.
     
    nepasolutions, Nov 14, 2008 IP
  13. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #13
    404 is directing straight to script.Do yo happen to know what browsers strip? And is there a way around it? or is there another way of passing referer so its not stripped?
     
    deemainer, Nov 14, 2008 IP
  14. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No you can set browsers to strip or add add-ons ... so i dunno if there would be a list but you can in the 404 redirect add a GET variable to the redirect url this would make it alot easier on your script
     
    atlantaazfinest, Nov 14, 2008 IP
  15. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #15
    mmm.... ok.Thank you again ! I have no idea how to even start with that .
     
    deemainer, Nov 14, 2008 IP
  16. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #16
    if u have access to the 404 redirect probably something like

    header("Location: http://yourdomain.com/atyourscript.php?r=".$_SERVER['HTTP_HOST']);

    YOUR SCRIPT:

    $refererhost = $_GET['r'];
     
    atlantaazfinest, Nov 14, 2008 IP