sabato 30 aprile 2011

AVOID INJECTION

////////////////////////////////////////////////// 
//////////////////////////////////////////////////
MYSQL QUERY CLEAN
//////////////////////////////////////////////////
function cleanQuery($string)
{
  if(get_magic_quotes_gpc())  // prevents duplicate backslashes
  {
    $string = stripslashes($string);
  }
  if (phpversion() >= '4.3.0')
  {
    $string = mysql_real_escape_string($string);
  }
  else
  {
    $string = mysql_escape_string($string);
  }
  return $string;
}

// if you are using form data, use the function like this:
if (isset($_POST['itemID'])) $itemID = cleanQuery($_POST['itemID']);

// you can also filter the data as part of your query:
SELECT * FROM items WHERE itemID = '".  cleanQuery($itemID)."' "
 
 
 
Or, preferably, mysql_real_escape_string(). mysql_escape_string() is deprecated, and should no longer be used.
 
 
////////////////////////////////////////////////// 
//////////////////////////////////////////////////
 
EVITARE SITUAZIONI DI GET DOVE SI SCEGLIE UNA PAGINA INCLUDE
 
  $whitelist = array('home', 'page');

  if (in_array($_GET['page'], $whitelist)) {
        include($_GET['page'].'.php');
  } else {
        include('home.php');
  }
 
//////////////////////////////////////////////////
//////////////////////////////////////////////////
 

Nessun commento:

Posta un commento