2015-06-05 142 views
-4

這裏我有一段PHP代碼來拒絕訪問一個頁面,除非你來自兩個頁面(第1頁和第2頁)。但它不工作,因爲它根本不運行代碼。這裏有什麼問題?爲什麼PHP或它不工作?

if($_SERVER['HTTP_REFERER'] != 'http://www.example.com/access.html' or $_SERVER['HTTP_REFERER'] != 'http://example.com/php/upload.php'){ 
    header ('Location: http://example.com/php/retry.php'); 
    exit; 
} 

非常感謝。

+2

提醒的是'$ _ SERVER [ 'HTTP_REFERER']'[真的不能被信任(http://php.net/manual/en/reserved.variables.server.php)! – someOne

回答

2

您的病情始終是true,請使用in_array&&

if ($_SERVER['HTTP_REFERER'] != 'http://www.example.com/access.html' && $_SERVER['HTTP_REFERER'] != 'http://example.com/php/upload.php') { 
    header(...); 
} 

if (!in_array($_SERVER['HTTP_REFERER'], array('http://www.example.com/access.html', 'http://example.com/php/upload.php')) { 
    header(...); 
} 
0

你還應該增加一個檢查,如果HTTP_REFERERempty與否 -

if (!empty($_SERVER['HTTP_REFERER']) && !in_array($_SERVER['HTTP_REFERER'], array('http://www.example.com/access.html', 'http://example.com/php/upload.php')) { 
    header(...); 
}