$ _SERVER [' HTTP_REFERER']返回正确的值但不传递条件

时间:2017-11-15 19:44:43

标签: php http-referer

我有联系表格和产品表格。我想检查以确认产品表格上只有产品价值,但我可以通过条件通过。

破码

// If visitor filled out the form on the "Contact Us" page (/contact/index.php) then no 'product' field is required.
if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || "http://url.com/contact/index.php") {
    if(strlen($product) < 2) {
        $errors[] = "<font color='red'>Please enter the product requesting.</font>";
    }
}

疑难解答/调试代码

$serverValue = $_SERVER['HTTP_REFERER'];
print "The value of SERVER is ". $serverValue;
echo "<br />";
print $_SERVER['DOCUMENT_ROOT']."/contact/index.php";               
echo "<br />";

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || "http://url.com/contact/index.php" || "/home/url/public_html/contact/index.php") {
//if ($_SERVER['HTTP_REFERER'] != $_SERVER['DOCUMENT_ROOT']."/contact/index.php") { 
print "This is NOT the Contact page";
} else {
print "This IS the Contact page";
}

排除故障/调试输出

  

SERVER的值为http://url.com/contact/index.php   /home/url/public_html/contact/index.php   这不是联系页面

您可以通过输出看到正在传递正确的HTTP_REFERER,但它只是无法正确评估。那里有一条注释掉的线,我正在尝试其他的东西。请放轻松,我是PHP的新手。

好吧,我理解我做错了什么并试了一下但没有成功

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" || $_SERVER['HTTP_REFERER'] != "http://url.com/contact/index.php") { 
if(strlen($product) < 2) {
$errors[] = "<font color='red'>Please enter the product requesting.</font>";
}
}

还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

它确实正确评估,因为非空字符串的计算结果为true

所以:

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" 
   || "http://url.com/contact/index.php" 
   || "/home/url/public_html/contact/index.php") {

第一个条件是什么并不重要,因为第二个和第三个条件总是评估为true

if ($_SERVER['HTTP_REFERER'] != "http://www.url.com/contact/index.php" 
   || true 
   || true) {

结果:

if (true) {

如果您不希望引荐来源成为这3个字符串中的任何一个,您可以使用类似:

的内容
if (!in_array($_SERVER['HTTP_REFERER'] , [
    'http://www.url.com/contact/index.php', 
    'http://url.com/contact/index.php', 
    '/home/url/public_html/contact/index.php'
]) {
相关问题