简体   繁体   中英

Only allow specific referrer PHP

I have this code in my website

<?php
// This is to check if the request is coming from a specific domain
$ref = $_SERVER['payskip.org'];
$refData = parse_url($ref);

if($refData['host'] !== 'payskip.org') {
  // Output string and stop execution
  die("Hotlinking not permitted");
}

echo "Executing code here";
?>

and it does what it should do, if you visit the site without the given referrer it will show "Hotlinking not permitted", but if you visit it from payskip.org it will execute the given code.

So far so good, but if I refresh my page with f5 or the reload button it executes the code again! I want it to execute only once and if you refresh it should go back with the hotlink is not permitted.

Well, what your server needs is a way to remember the visitor. You could do this via many ways but let me state the two most common:
1. Sessions
2. Database.

If you are only interested in stopping the 'refresh browser' hack, using sessions should solve your problem but what happens when the same user visits your site some other time after the session must have ended?... In such a scenario, you might want to also remember such a user...and that's where database comes to the rescue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM