Determining Referer in PHP

后端 未结 5 1371
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:01

What is the most reliable and secure way to determine what page either sent, or called (via AJAX), the current page. I don\'t want to use the $_SERVER[\'HTTP_REFERER

5条回答
  •  既然无缘
    2020-11-22 04:43

    What I have found best is a CSRF token and save it in the session for links where you need to verify the referrer.

    So if you are generating a FB callback then it would look something like this:

    $token = uniqid(mt_rand(), TRUE);
    $_SESSION['token'] = $token;
    $url = "http://example.com/index.php?token={$token}";
    

    Then the index.php will look like this:

    if(empty($_GET['token']) || $_GET['token'] !== $_SESSION['token'])
    {
        show_404();
    } 
    
    //Continue with the rest of code
    

    I do know of secure sites that do the equivalent of this for all their secure pages.

提交回复
热议问题