如何允许来自多个站点的http referer

时间:2014-10-07 06:17:47

标签: php http referrer

我想知道如何允许来自多个网站的http引用。

例如

<?php
$domain='example.net';
$referrer = $_SERVER['HTTP_REFERER'];
if (@preg_match("/example.net/",$referrer)) {
} else {
header('Location: http://www.example.net/404.php');
};
?>

如果我从example.net打开链接,此代码有效,但我想允许example1.net和example2.net访问链接。

我该怎么做?如果有人能帮助我,我们将非常感激。

1 个答案:

答案 0 :(得分:1)

使用正则表达式运算符或 - |(管道)

<?php
$domains = Array(
    'example.net',      
    'example2.net'
);
$referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); // Gets the domain name from referer
if (!preg_match("/" . implode('|', $domains) . "/", $referrer)) {
    header('Location: http://www.example.net/404.php');
    exit(0); //force exit script after header redirect to ensure no further code is executed.
};

// Normal code execution here...
?>