【问题标题】:$_SERVER['HTTP_REFERER'] returning correct value but not passing conditional$_SERVER['HTTP_REFERER'] 返回正确的值但不通过条件
【发布时间】:2017-11-15 19:44:42
【问题描述】:

我有一个联系表和一个产品表。我想检查以验证仅在产品表单上是否存在产品价值,但我可以获得通过的条件。

破解密码

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

还有其他想法吗?

【问题讨论】:

  • 这不可靠使用

标签: php http-referer


【解决方案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'
]) {

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2016-07-14
    • 2011-05-24
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多