2013-05-13 5 views
-1

Je veux sécuriser cette page, mais je ne sais pas par où commencer. Parce que je me suis injecté hier, j'ai trébuché mysql chaîne d'échappement, mais je n'ai pas beaucoup aidé. Et je ne sais rien sur PDO, pouvez-vous me brancher? Voici le code.Instructions paramétrées et sécurité

<?php 
//category.php 

require_once('startsession.php'); 
require_once('php/mysql_prisijungimas.php'); 
include 'connect.php'; 

//first select the category based on $_GET['cat_id'] 
$sql = "SELECT 
      cat_id, 
      cat_name, 
      cat_description 
     FROM 
      categories 
     WHERE 
      cat_id = " . mysql_real_escape_string($dbc, trim($_GET['id'])); 

$result = mysql_query($sql); 

if(!$result) 
{ 
    echo 'The category could not be displayed, please try again later.' . mysql_error(); 
} 
else 
{ 
    if(mysql_num_rows($result) == 0) 
    { 
     echo 'This category does not exist.'; 
    } 
    else 
    { 
     //display category data 
     while($row = mysql_fetch_assoc($result)) 
     { 
      echo '<h2>Topics in &prime;' . $row['cat_name'] . '&prime; category</h2><br />'; 
     } 

     //do a query for the topics 
     $sql = "SELECT 
        topic_id, 
        topic_subject, 
        topic_date, 
        topic_cat 
       FROM 
        topics 
       WHERE 
        topic_cat = " . mysql_real_escape_string($dbc, trim($_GET['id'])); 

     $result = mysql_query($sql); 

     if(!$result) 
     { 
      echo 'The topics could not be displayed, please try again later.'; 
     } 
     else 
     { 
      if(mysql_num_rows($result) == 0) 
      { 
       echo 'There are no topics in this category yet.'; 
      } 
      else 
      { 
       //prepare the table 
       echo '<table border="1"> 
         <tr> 
         <th>Topic</th> 
         <th>Created at</th> 
         </tr>'; 

       while($row = mysql_fetch_assoc($result)) 
       {    
        echo '<tr>'; 
         echo '<td class="leftpart">'; 
          echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h3>'; 
         echo '</td>'; 
         echo '<td class="rightpart">'; 
          echo date('d-m-Y', strtotime($row['topic_date'])); 
         echo '</td>'; 
        echo '</tr>'; 
       } 
      } 
     } 
    } 
} 
?> 
+0

Bien que cela ne fonctionnera pas avec l'extension MySQL. PDO prend en charge [déclarations préparées] (http://php.net/manual/en/pdo.prepared-statements.php) –

+0

[Le grand évasion (Ou: ce que vous devez savoir pour travailler avec du texte dans le texte)] (http : //kunststube.net/escapism/) – deceze

Répondre

1

Dans votre cas entourent la mysql_real_escape_string avec des guillemets comme ceci:

SELECT * FROM table WHERE ID = '".mysql_real_escape_String($_POST['ID'])."' 

Notez les guillemets simples supplémentaires. Cela le rendra plus sûr, mais il vaut mieux utiliser des déclarations préparées. En plus de préférer utiliser des instructions préparées sur mysql_query, à partir de php version 5.5.0, la fonction mysql_query() sera obsolète.

Il existe un autre sujet sur stackoverflow qui répond à la question 'Comment empêcher l'injection SQL dans PHP Pdo'. Vous pouvez trouver des exemples et des informations supplémentaires: How can I prevent SQL injection in PHP?

+0

Merci, pour votre aide. –