Search code examples
phphtmlhttp-refererindex-error

How to use HTTP_REFERER in PHP


I know this may be a duplicate of another link on here but I am not able to find a fix for my code.

I am trying to make a puzzle for my friends and family can try to do and I need a way to track what website they just came from so I know whether to send them to the next puzzle/win screen, or to send them to a page effectively saying they are cheaters. I am trying to use $_SERVER[HTTP_REFERER] to make this easier for me, but I want to echo out the address that the user is coming from to test my code, and I keep getting an index error.

MY CODE:

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

I keep getting an index error on this line $_SERVERADDR=$_SERVER['HTTP_REFERER'];

Can anyone help out?


Solution

  • https://stackoverflow.com/a/60288899/7044855

    Referrer isn't always set. You can set HTTP headers manually if you're testing using curl/postman/etc, but not if you were testing by simply hitting your script in a browser.

    set referrer in postman

    Using this key/value combination you should see the following output

    <?php
    
    $referer = $_SERVER['HTTP_REFERER'];
    
    echo $referer;
    // https://google.com