这里出了什么问题?为什么PHP或它不能工作


What is wrong here? Why PHP OR it isn't working?

这里我有一小段PHP代码来拒绝访问一个页面,除非你来自两个页面(第1页和第2页)。但是它不工作,因为它根本不运行代码。

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;
}

您的条件总是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(...);
}

您还应该添加检查HTTP_REFERER是否为empty -

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