Possible Duplicate:
What are the best practices for avoiding xss attacks in a PHP site
我有一个用户可以填写个人信息的表格。用户提交表单,Web服务将处理这些信息并将信息存储在mysql数据库中。
但是如果用户在输入字段中输入 html 标签、php 代码或 javascript 该怎么办?我想阻止这种情况发生。我知道在 javascript 中有一个方法调用 escapehtml,在 php 中它是 strip_tags。
我只是想知道禁用从输入字段键入 html、php、脚本的功能的正确方法。我是否对收到的所有输入使用 strip_tags?如果我使用 strip_tags,如何禁用脚本?或者有办法在 mysql 中做到这一点吗?
谢谢
这是表格:
<div>
<label class='info-title whitetext' for="name">Full Name: </label>
<input type="text" name="name" id="name" size='25' maxlength="100" required />
</div>
<div>
<label class='info-title whitetext' for="phone">Phone: </label>
<input type='text' pattern='\d+' name='phone' id='phone' size='25' maxlength='12' />
</div>
<div>
<label class='info-title' for="email">Email: </label>
<input type="email" name="email" id="email" size='35' maxlength="60" required />
</div>
<div>
<label class='info-title' for="address">Address: </label>
<input type="text" name="address" id="address" size='45' maxlength="50" required />
</div>
最佳答案
尝试htmlspecialchars($string);
这会将所有 HTML 标签编码为字符代码( <div>
将变为 <div>
,它将在不被解析为 html 的情况下显示),这样,也无法创建脚本标签。
在将内容提供给数据库之前,请务必先清理内容,例如使用 mysqli_escape_string()
进行转义(其他人可能会建议您使用 prepare
语句)。
将 HTML 字符编码字符串放入数据库很可能不是最佳实践,因为它只会不必要地增加字符串大小。 (而且它本身并不提供针对 SQL 注入(inject)的保护)
关于php - 禁用输入字段中的 html 标签、php、脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12045634/