0

我拥有一个网站,用于发布带有数据库的问题和答案等论坛。我想强化我的代码以防止 XSS 和 SQl 注入。

对于 XSS,我使用了 folloinf 函数:Strip_tags htmlspecialchars htmlentities

我的问题是:它们之间有什么区别?另外,我遇​​到的问题是:1-我有一个文本区域和带有默认文本的文本框 2-使用上述功能时,我在文本区域中输入了任何测试..输出不会采用我写的内容textarea ..它将始终采用默认值...

这就是我所做的

$first_name = $_POST['first_name'];
$first_name = htmlspecialchars($first_name);
echo $first_name

另外,任何人都对防止SQL注入有任何想法,我对此一无所知..

4

2 回答 2

0

直接看说明书。 http://php.net/manual/en/mysqli.prepare.php

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Amersfoort";

/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $city);

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>

对于 XSS,strip_tags 就可以了。除非有人破解了您的 FTP 密码并且他们进入并编辑您的文件,或者如果您允许他们上传漏洞,请确保您在上传文件夹中禁用脚本执行。

于 2012-11-10T12:16:35.450 回答
0

为了防止 XSS,您应该根据您要输出到的区域使用不同的过滤规则。

hmtlentities(转换<&lt;)很好,但是将不安全的变量输出到<head>or <script>or<style>标签;

另一个棘手的领域是标签属性,<input onMouseDown=""因为人们可以破坏你的字符串并添加他们自己的属性(就像 SQL 注入一样)。

该主题在以下地方进行了深入介绍:

http://adamjonrichardson.com/2012/02/01/improving-xss-cross-site-scripting-prevention-in-four-simple-steps/ https://www.golemtechnologies.com/articles/prevent-xss

您还可以研究使用 PHP 的filter_var函数,非常有用。

于 2012-11-10T12:17:32.807 回答