Search code examples
phpuser-agentreferer

How to get the real referer?


Supposing I want to restrict post requests in order to process only ones that are coming from my own domain, how can I check for the referer other way than using $_SERVER['HTTP_REFERER'], which I read ($_SERVER documentation) that can not be trusted?


Solution

  • This isn't possible unfortunately, the only way to check the referrer is if the browser has supplied this information, which obviously, could be changed to benefit the reader.

    However you could create a user session or cookie that saves a variable to show that the user has visited the site. You could then only allow users to see the posts if that session variable is defined.

    Another option would be to generate links based on the users IP address and only give them access if they have clicked on a specifically generated link. You could start by saving the users IP address and a unique key in the database and then all your internal links could use a GET variable with the unique ID. If the page is opened without a genuine ID then you could display an error message.