PHP script only allow traffic from a certain domain?
Is there a simple script that would on开发者_开发问答ly allow visitors if they originate from a website of my choice?
Checking the referrer is the most reliable way to accomplish this, but you should be aware that not all user agents (aka browsers) send a complete or correct referrer.
Something like this:
$target_site = 'http://www.google.com';
if (isset($_SERVER['HTTP_REFERER']) && preg_match("/$target_site/",$_SERVER['HTTP_REFERER'])) {
// do something with people from google.com
} else {
// do something else with everyone else
}
Read more about it: http://www.electrictoolbox.com/php-http-referer-variable/
PHP manual on $_SERVER
superglobal: http://php.net/manual/en/reserved.variables.server.php
You can use the $_SERVER['http_referer']
but that can be easily faked.
If you get their referrer information you could check it against a list of accepted website origins and redirect them back to the site they came from if you don't want them.
$_SERVER["HTTP_REFERER"]
精彩评论