2011-10-07 2 views
-1
<?php 
//create_cat.php 
include 'connect.php'; 
include 'header.php'; 
include 'parser.php'; 

$sql = "SELECT 
      topic_id, 
      topic_subject 
     FROM 
      topics 
     WHERE 
      topics.topic_id = " . mysql_real_escape_string($_GET['id']); 

$result = mysql_query($sql); 

if(!$result) 
{ 
    echo 'The topic could not be displayed, please try again later.'; 
} 
else 
//check for sign in status 
    if(!$_SESSION['signed_in']) 
    { 
     echo 'You must be signed in!'; 
     header('Location:signin.php') ; 
    } 

else 
{ 
    if(mysql_num_rows($result) == 0) 
    { 
     echo 'This topic doesn&prime;t exist.'; 
    } 
    else 
    { 
     while($row = mysql_fetch_assoc($result)) 
     { 
      //display post data 
      echo '<table class="topic" border="1"> 
        <tr> 
         <th colspan="2">' . $row['topic_subject'] . '</th> 
        </tr>'; 

      //fetch the posts from the database 
      $posts_sql = "SELECT 
         posts.post_topic, 
         posts.post_content, 
         posts.post_date, 
         posts.post_by, 
         users.user_id, 
         users.user_name 
        FROM 
         posts 
        LEFT JOIN users ON posts.post_by = users.user_id 

        LEFT JOIN topics ON topics.topic_by = users.user_name 

        WHERE 
         posts.post_topic = " . mysql_real_escape_string($_GET['id']); 

      $posts_result = mysql_query($posts_sql); 

      if(!$posts_result) 
      { 
       echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>'; 
      } 
      else 
      { 
      $parser = new parser; // start up Recruiting Parsers 


       while($posts_row = mysql_fetch_assoc($posts_result)) 

       { 

       // parsesBBCode 
       $parsed = $parser->p($posts_row['post_content']); 



        echo '<tr class="topic-post"> 
          <td class="user-post">' . $posts_row['user_name'] . '<br/>' . date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td> 
          <td class="post-content">' . $parsed. '</td> 
          </tr>'; 
       } 
      } 

      if(!$_SESSION['signed_in']) 
      { 
       echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.'; 
      } 
      else 
      { 
       //show reply box 
       echo '<tr><td colspan="2"><h2>Reply:</h2><br /> 
        <form method="post" action="reply.php?id=' . $row['topic_id'] . '"> 
         <textarea name="reply-content"></textarea><br /><br /> 
         <input type="submit" value="Submit reply" /> 
        </form></td></tr>'; 
      } 

      //finish the table 
      echo '</table>'; 
     } 
    } 
} 

include 'footer.php'; 
?> 

오류 기반 SQL 주입이 내 코드에서 작동하며 실수가 어디에 있는지는 알 수 없습니다. 모든 기능이 있지만 코드가 취약하다는 것을 의미합니다. 이 코드 내에서 보안을 유지하기 위해 사용할 수있는 몇 가지 유용한 방법이 무엇입니까? 이것은 mysql을 사용하여 PHP를 사용하기위한 간단한 포럼 스크립트입니다.PHP MYSQL 불안전 한 오류 기반 주입

+0

와우 SQL 주입을 많이 먹으 렴을. – rook

답변

2

음, 하나 mysql_real_escape_string에 대한 당신의 문자열을 탈출하지만,되지도 인용을한다. 당신은 (일반적인 경우)이처럼 사용한다 :

$sql = sprintf('...blah blah... WHERE topics.topic_id = \'%s\'', 
       mysql_real_escape_string($_GET['id'])); 

이 특정한 경우에, 당신은 또한 (IMHO 더 자세한 설명입니다) intval를 사용할 수 및/또는 %dsprintf 인수 지정을 전환합니다.

+0

그 혼자서 오류 기반 주입을 막은, 감사합니다! 나는 sprintf와 이스케이프 문자열에 대해 더 읽고있다. –