0

我正在编写一个简单的类似 cms 的解决方案来跟踪我的愚蠢想法。一切都很顺利,但我现在在将 Xinha RTE 插件实施到我的应用程序中时遇到了一些困难。

我已经按照他们的现场教程进行操作,它似乎正在工作,但是......

在对文本、标题段落等进行格式化时。虽然标签已正确保存在 mysql 数据库中:

<h1>heading</h1>
<p>text example</p>

它们显示为:

<h1>heading</h1><p>text example</p>  (concatenated and NOT formatted , displaying tags in stead)

或者

&lt;p&gt;tesy&lt;/p&gt; &lt;h4&gt;fgfg&lt;br /&gt;&lt;/h4&gt; &lt;h2&gt; &lt;/h2&gt;

最后一个示例输出是因为我做了这个更改:

//$postCon = mysql_real_escape_string($postCon);
$postCon = htmlspecialchars($postCon);

那只是因为他们论坛上的某个人说逃避 html 特殊字符是“愚蠢的”——因为 html 标签是由它们组成的。

我很难指定实际问题。因此我的问题有点草率。我希望那里有一些我现在所处的位置,并且可以在正确的方向上提供一些解释或指导。

我现在会去喝咖啡思考这个问题,如果我有任何新的东西,我会带来更新。现在,我将只为您提供执行后期处理的实际脚本。

谢谢,

<?php  

include_once 'bin/configDb.php';
include_once 'bin/connectDb.php';  
include_once 'header.php';
//get stuff from post


$topicSub = $_POST['topic_subject'];
//$topicSub = mysql_real_escape_string($topicSub);
$topicSub = htmlspecialchars($topicSub);
$topicCat = $_POST['topicCat'];
//  $topicCat = mysql_real_escape_string($topicCat);

$sesId = $_GET['username'];

        //the form has been posted, so save it  
        //insert the topic into the topics table first, then we'll save the post into    the posts table

$postCon = $_POST['post_content'];
//$postCon = mysql_real_escape_string($postCon);
$postCon = htmlspecialchars($postCon);


$sql = "INSERT INTO
                    topics(topic_subject, topic_date, topic_cat, topic_by)
    VALUES('$topicSub', NOW(), '$topicCat', '$sesId' )";

        $result = mysql_query($sql);

        if(!$result) 
        { 
            //something went wrong, display the error 
            echo 'An error occured while inserting your data. Please try again later.'    . mysql_error(); 
            $sql = "ROLLBACK;"; 
            $result = mysql_query($sql); 
        } 
        else 
        { 
            //the first query worked, now start the second, posts query 
            //retrieve the id of the freshly created topic for usage in the posts query

            $topicId = mysql_insert_id();

            $sql = "INSERT INTO 
                        posts(post_content, 
                             post_date,
                              post_topic, 
                              post_by) 
                    VALUES 
                        ('$postCon', NOW(), '$topicId', '$sesId' )";
            $result = mysql_query($sql); 

            if(!$result) 
            { 
                //something went wrong, display the error 
                  echo 'An error occured while inserting your post. Please try again    later.' . mysql_error(); 
                $sql = "ROLLBACK;"; 
                $result = mysql_query($sql); 
            } 
            else 
            { 
                $sql = "COMMIT;"; 
                $result = mysql_query($sql); 

                //after a lot of work, the query succeeded! 
                echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
                header("location:admin.php");

            } 


        }
        include_once 'footer.php';
?>
4

2 回答 2

0

你错过了 mysql_real_escape_string 的目的。它可以使任意字符串数据安全用于 SQL 查询。是一种SQL注入攻击防范方法。htmlspecialchars 对防止 SQL 注入攻击毫无帮助。您正在使用螺丝刀钉入钉子。它可能在某些情况下有效,但不会涵盖所有情况。正是那些“未发现”的案例将允许某人通过前门华尔兹来攻击您的网站。

于 2011-09-21T14:36:41.820 回答
0

我发现问题出在代码的完全不同的区域。是在显示内容的代码中,我很傻。这是一个 htmlentities(stripslashes())

做有趣的事。

谢谢你让我把它放在那里。

Marc B,感谢您再次处理我的 sql 注入问题。随意以我的方式提出更多建议。我确实采纳了你最后的建议:) 个人感谢你

于 2011-09-22T14:20:39.590 回答