gpt4 book ai didi

php - 如何集成 HTML Purifier 来过滤用户提交的数据?

转载 作者:行者123 更新时间:2023-11-29 07:14:14 25 4
gpt4 key购买 nike

我有这个从用户收集数据的脚本,我想使用 HTML Purifier 检查他们的数据是否存在恶意代码(例如 XSS 和 SQL 注入(inject)) , 但如何将它添加到我的 PHP 表单提交脚本中呢?

这是我的 HTML 净化器代码:

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

}

最佳答案

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() 将数据输入数据库之前转义数据。

你也可以组合 mysql_real_escape_string($purifier->purifiy($interests)),但我没有合并以使其更具可读性。

关于php - 如何集成 HTML Purifier 来过滤用户提交的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2545634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com