2014-10-07 80 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