2011-09-21 210 views
0

我在寫一個簡單的類似於cms的解決方案來跟蹤我愚蠢的想法。 一切都很順利,但我現在遇到一些困難,將辛哈RTE插件應用到我的應用程序中。包含html標籤的帖子顯示無格式

我按照他們的現場教程,它似乎是工作,但...

當進行格式化的文本,標題段落等。雖然這些標籤正確在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'; 
?> 

回答

0

你已經錯過了mysql_real_escape_string的目的。它可以在任意SQL查詢中使用任意字符串數據SAFE。這是一種SQL注入攻擊預防方法。 htmlspecialchars根本無助於防止SQL注入攻擊。你正在用螺絲刀開釘。它在某些情況下可能有效,但不會涵蓋所有情況。而這些「未被發現」的案件將允許某人通過前門進入華爾街攻擊您的網站。

+0

我假設,因爲它是這方面成爲一個MySQL查詢,這是一個好主意,包括它的一部分的任意字符串數據。 正如我上面提到的,我只是作爲一個檢查來評論它是否對實際問題有任何影響。然後我包含了htmlspecialchars來查看是否修改了處理html標籤的方式。沒有解決任何問題 我在這裏的問題不是關於sql注入漏洞 - 抱歉,如果我誤導了你。這是我真正想要了解更多關於希望更好預防的主題。請放縱我:) –

0

我發現這個問題是在代碼中的一個完全不同的領域。顯示內容的代碼是愚蠢的。這是一個 ヶ輛(stripslashes()函數)

做着滑稽的生意。

謝謝你讓我把它放在那裏。

馬克·B,感謝再次與我的SQL注入的問題處理。隨意以我的方式提出更多建議。 我沒有拿你的最後忠告投入使用:)個人感謝您

相關問題