2015-06-05 244 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(...); 
}