我有这个脚本从用户收集数据,我想使用HTML Purifier检查他们的数据是否有恶意代码(例如XSS和SQL注入),但是如何将它添加到我的PHP表单提交脚本中?
这是我的HTML Purifier代码:
require_once '../../htmlpurifier/library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding
$config->set('HTML.Doctype', 'XHTML 1.0 Strict'); // replace with your doctype
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
这是我的PHP表单提交代码:
if (isset($_POST['submitted'])) { // Handle the form.
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, profile.*
FROM users
INNER JOIN contact_info ON contact_info.user_id = users.user_id
WHERE users.user_id=3");
$about_me = mysqli_real_escape_string($mysqli, $_POST['about_me']);
$interests = mysqli_real_escape_string($mysqli, $_POST['interests']);
if (mysqli_num_rows($dbc) == 0) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO profile (user_id, about_me, interests)
VALUES ('$user_id', '$about_me', '$interests')");
}
if ($dbc == TRUE) {
$dbc = mysqli_query($mysqli,"UPDATE profile
SET about_me = '$about_me', interests = '$interests'
WHERE user_id = '$user_id'");
echo '<p class="changes-saved">Your changes have been saved!</p>';
}
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
return;
}
}
答案 0 :(得分:1)
if ($dbc == TRUE) {
//add the stuff you want to clean here.
$about_me = $purifier->purify($about_me);
$interests = $purifier->purify($interests);
$dbc = mysqli_query($mysqli,"UPDATE profile
SET about_me = '".mysql_real_escape_string ($about_me)."', interests = '".mysql_real_escape_string ($interests)."'
WHERE user_id = '$user_id'");
echo '<p class="changes-saved">Your changes have been saved!</p>';
}
在数据库中输入数据之前,您还应该考虑转义数据
你也可以结合使用mysql_real_escape_string($ purifier-&gt; purifiy($ interests)),但我没有整合以使其更具可读性。
答案 1 :(得分:1)
我想使用HTML Purifier
检查他们的数据是否存在XSS和SQL注入等恶意代码
这不是HTML Purifier的意思。
HTML Purifier适用于您需要允许用户提交实际的[X] HTML以包含在页面中,但您不希望他们完全访问HTML的所有潜在危险功能。
不能替代在将HTML文档输入HTML页面时正确使用htmlspecialchars
。在绝大多数情况下,您希望将输入用作文本字符串而不是HTML标记,您希望将<
和&
符号转义为<
和&
,不要将它们视为标记并将它们破坏以试图使标记“干净”。
它对SQL注入也没有任何作用。您必须继续使用mysqli_real_escape_string
,或转到参数化查询。