Lordblacksuca

Solución a los ataques SQL-Injection y de etiquetas html, php etc.

by on Dec.11, 2010, under General

Este pequeño script debe ir arriba de TODOS los archivos php que requieran interacción del usuario por medio de los metodos POST, GET y REQUEST.

Esto, eliminar las etiquetas html  que puedan ser ingresadas en los campos de datos y tambien evita las sql-injection.

foreach( $_POST as $variable ){
$_POST [ $variable ] = mysql_real_escape_string($variable);
$_POST [ $variable ] = str_replace ( array("< ",">","[","]","*","^"), "" , $_POST[ $variable ]);
//$variable=$_POST [ $variable ];
//echo "POST:$variable";
}
foreach( $_REQUEST as $variable){
$_REQUEST [ $variable ] = mysql_real_escape_string($variable);
$_REQUEST [ $variable ] = str_replace ( array("< ",">","[","]","*","^"), "" , $_REQUEST[ $variable ]);
//$variable=$_REQUEST [ $variable ];
//echo "REQUEST:$variable";
}
foreach( $_GET as $variable){
$_GET [ $variable ] = mysql_real_escape_string($variable);
$_GET [ $variable ] = str_replace ( array("< ",">","[","]","*","^"), "" , $_GET[ $variable ]);
//$variable=$_GET [ $variable ];
//echo "GET:$variable";
}

Básicamente lo que el script hace es leer variable por variable, de cualquiera de los 3 métodos y añadir un caracter de escape, “\” a cualquier caracter peligroso para mysql, y eliminar etiquetas html y demás.

Por ejemplo:
El texto: ‘OR=’ ‘ es convertido a \’OR=\’ \’ por lo que pierde efecto.
El texto <h1>HOLA</h1> es convertido a h1HOLAh1 por lo que también pierde efecto.

Con este script nos evitamos tener que proteger todos los campos manualmente uno por uno.

Recuerda que si te sirvió, comentá.

Saludos y safe programming!

Related posts:

  1. Error 0×80070643 al instalar Windows Live Essentials
  2. Bison Cam en Windows 7 – (Solución)
:, , , , , , , , ,

2 Comments for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!