2014-10-07 85 views
0

我想知道如何让http引用超过1个网站。如何让超过1个网站的http referer

例如

<?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以及访问的链接。

我该怎么做?如果有人可以帮助我,这将非常感激。

+0

如果你的目标是从除非在引用作为安全性的方法存在访问此页面震慑人心,但应注意的是,它很容易被欺骗。 – Ohgodwhy 2014-10-07 06:25:56

+0

只是一个音符 - 在上面的代码中,变量$ domain,不会在任何地方使用。 – 2014-10-07 06:27:28

回答

0

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

<?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. 
?> 
+0

非常感谢,非常感谢。 – user3522662 2014-10-07 09:21:15

+0

没问题。我已经编辑了答案,以添加一个稍微好一些的方式来使用代码作为基础来完成此操作。 – 2014-10-08 17:12:49