2011-03-15 82 views
1
$alerter2="false"; 
for ($counter = 0; $counter <= count($filter); $counter++) { 
    $questionsubmitted=strtolower($_POST[question]); 
    $currentcheck =$filter[$counter]; 
    $foundvalue=stripos((string)$questionsubmitted,(string)$currentcheck); 
    echo $foundvalue; 
    if ($foundvalue==0) { 
     $alerter2="true"; 
    } else { } 
} 

if (!($alerter2=="true")) { 
    $sql="INSERT INTO Persons (Name, Email, Question) 
     VALUES 
     ('$_POST[name]','$_POST[email]','$_POST[question]')"; 
} else { 
    echo "Please only post appropriate questions"; 
} 

由于某些原因,每当我运行这个,每次迭代stripos都会返回0。它应该是一个过滤器,并使用回声,我发现每当它出现时,stripos都是0。然而,当我在if中使用0时,即使那些没有单词的人也会返回true。奇怪的布尔反应

我应该在哪里使用mysql_real_escape_string?查询后?请注意,我将这段代码放在了我希望将用户输入保存到数据库的地方。

+2

使用'mysql_escape_real_string();'您'$ sql'内'$ _POST'变量将是另一个事 – kjy112 2011-03-15 14:05:19

+3

SQL注射攻击! – Arafangion 2011-03-15 14:05:54

+2

使用字符串表示“true”和“false”。真?? – Spudley 2011-03-15 14:08:51

回答

0

您需要更改

if ($foundvalue==0) 

if ($foundvalue===0) // three equals signs 

或等价的东西,这取决于你的逻辑(我不明白这是怎么回事)。

但正如大家所说,此代码是开放的SQL注入攻击(以及其他问题)。

2

stripos如果找不到值,则返回false;如果是第一个字符,则返回0。问题是,PHP自动将布尔值转换为0整数或将0整数转换为false。所以我认为演员阵容正在发生,因此这种状况不会达到你想要的。

您可以使用===也检查变量的类型:

if ($foundvalue === 0) { 
    $alerter2="true"; 
} 

有关于stripos链接的文档中这个问题的更多细节。

您还应该删除空的else子句以获得更干净的代码,并使用mysql_real_escape_string在将值放入数据库之前对其进行消毒。

0

此外,

$questionsubmitted=strtolower($_POST[question]); 

也许应该是:

$questionsubmitted=strtolower($_POST['question']); 
+0

是的,这确实有效,但是您每次以这种方式执行时都会在您的PHP通知中记录文件。 'question'(without apostrophs)被视为常量表达式(即以前由define('question','somevalue')定义,尽管常量应该用大写字母表示),在你的情况下不是。 – 2011-03-15 14:19:38