PHP验证输入无效

时间:2016-04-28 21:30:11

标签: php validation character-encoding

我已经在这里工作了好几个小时,似乎无处可去。

我使用XAMPP Apache 2.0PHP 5.5Chrome并使用Netbeans作为我的编辑。

我有一个输入文本框,在我用它来搜索MYSQL数据库之前我要验证它。我使用POST来获取输入。

我使用的输入是" x / 65 !!!"如何

 $searchtext = $_POST['searchbox'];
 echo "<br />" . htmlspecialchars($searchtext);
 echo "<br />htmlentitites: " . $searchtext;
 echo "<br />strip slashes : " . stripslashes($searchtext);
 echo "<br />internal encoding is: " . mb_internal_encoding();

我的结果是:

for htmlspecialchars -> "x/65!!!"how to<b>   //why isn't the <b> removed?

for htmlentities     ->    "x/65!!!"how to //shouldn't the forward slash be stripped out?

for stripslashes     ->  "x/65!!!"how to  //shouldn't the '/' be stripped out?

for mb_internal_encoding->  ISO-8859-1 //My php.ini has UTF-8 as the default, I have meta content-type charset="UTF=8" and I though php 5.5 defaulted to UTF8

我是从PHP手册中复制的,但我没有得到他们的结果。我怀疑它的字符集有关,但我不知道在哪里看。

****评论********

杰夫,

 I changed the spelling of entities and used this string: <b>"'This \!'": /I

这就是我用htmlspecialchars得到的:&#34;&#39;这个!&#39;&#34;:/我 是的,一切都很大胆。我的所有输出都是粗体,我没有得到任何

       the escape chars showing:  &quot;&quot;&lt;&gt

有趣的是,当我回应$ _POST [&#39; searchbox&#39;];尽管如此,它还是以粗体显示。

我的mb_internal_encoding()是ISO-8859-1,即使在页面的开头我有一个meta语句将其设置为UTF-8而对于PHP我通过删除分号来制作默认的UTF-8。

我开始认为我的PHP解释器坏了。我将不得不看看XAMPP,看看他们最近的版本是什么。

1 个答案:

答案 0 :(得分:0)

我认为你可能会混淆这些函数对源输出的作用与浏览器呈现源的输出之间的区别。确保您将浏览器屏幕上显示的内容与浏览器源中显示的内容进行比较。您可以按 ctrl + U here's a reference for how to do it in a few others在大多数热门浏览器中查看html源代码。

  

for htmlspecialchars - &gt; x/65!!!"how to<b> //为什么不是<b>   去掉?

htmlspecialchars无法移除任何内容,只是将某些HTML字符转换为html entity encoding format。因此,它已将<b>翻译成html实体&lt;b&gt;,该实体在页面上呈现为<b>。如果您查看源代码,它实际上是<br />&quot;x/65!!!&quot;how to&lt;b&gt;。否则,您将无法在屏幕上看到<b>,因为它会被浏览器解释为一个开头的粗体标记,使您的所有后续文字都变为粗体。这也是为什么它在你的问题中不可见,因为你没有为降价而逃避它。

  

for htmlentities - &gt; x/65!!!"how to<b> //不应该是前锋   斜线被剥离了?

这里有几个问题:

  • 没有实际意义,但我只想指出你 在您的htmlentitites != htmlentities拼写错误echo 言。

  • 你没有真正使用过 htmlentities 代码中的函数:

    echo "<br />htmlentitites: " . $searchtext;
    

    为了实际使用它,请包装变量:

    echo "<br />htmlentitites: " . htmlentities($searchtext);
    
  • 巧合的是,即使你曾经使用它,它也不可能 转义是因为常规的旧正斜杠/不是html character entity, 虽然它看起来非常类似于小数斜杠,但是 将被编码为&frasl;

  

for stripslashes - &gt; x/65!!!"how to<b> //不应该是/   被剥夺了?

stripslashes仅删除反斜杠\,而不是正斜杠/。我认为它应该被称为stripbackslashes,但他们没有问我:)

  

for mb_internal_encoding-&gt; ISO-8859-1 //我的php.ini有UTF-8作为   默认情况下,我有meta content-type charset =&#34; UTF = 8&#34;而我虽然PHP 5.5   默认为UTF8

我对这个不太熟悉,但我可以告诉你,默认值不是UTF-8,而是ISO-8859-1。你肯定 My php.ini has UTF-8 as the default?因为我的php.ini有一行如下所示:;mbstring.internal_encoding = UTF-8你应该注意开头的分号;意味着它被注释掉了。您必须删除该分号并重新启动Web服务器才能使其生效。

对于您的meta statement setting it to UTF-8,我认为您的意思是您添加了html meta tag,其内容类似于<meta charset="UTF-8">。这仅适用于html输出信号通知浏览器所期望的字符集,并且不会对您的内部服务器设置产生任何影响。