Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/11/2009, 13:05
Avatar de mauro770
mauro770
 
Fecha de Ingreso: mayo-2009
Mensajes: 109
Antigüedad: 15 años
Puntos: 1
Desacuerdo Este codigo es seguro ? :S

hola que tal estoy desarrollando un proyecto y creo como le a tocado a todos en algun momento , ando con el mambo de la inyeccion xD .... y queria consultar si este codigo esta seguro de la inyeccion sql o cuales son sus fallos , o si se podria mejorar ... es sencillo no es muy largo ...
Código PHP:
<?php require_once('../Connections/Cnn_usuarios.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}
$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}
// aca empieso yo.
if(array_key_exists('enviar'$_POST) && !empty($_POST)){
// hacemos las comparaciones, si existe un caracter extraño lo hara saber
$v_nombre preg_replace("/[^a-zA-Z0-9\sñÑáéíóúÁÉÍÓÚ]/"""$_POST['nombre']);
$v_pass preg_replace("/[^a-zA-Z0-9\sñÑáéíóúÁÉÍÓÚ]/"""$_POST['pass']);
if ((
$v_nombre !== $_POST['nombre']) || ($v_pass !== $_POST['pass'])){
 
$error "Han surguido los siguientes errores:<br/>
     No se permiten caracteres extra&ntilde;os. Ejemplo : (,),{,},[,],?,¿,¡,!,;,#,',&,|,/, etc.Por favor si has ingresado alguno elim&iacute;nalos.<br/>"
;

if(
$v_nombre !== $_POST['nombre']){
     
$error .= "Por favor verifica tu NOMBRE.<br/>";
     }
if(
$v_pass !== $_POST['pass']){
     
$error .= "Por favor verifica la contraseña.<br/>";
     }
} else {
// verificamos q los campos no esten vacios
if(empty($_POST['nombre']) || empty($_POST['pass'])){
$error "Han surguido los siguientes errores:<br/>";
if(empty(
$_POST['nombre'])){
 
$error .="Ingresa tu NOMBRE<br/>";
 }
if(empty(
$_POST['pass'])){
 
$error .="Ingresa una contraseña<br/>";}
 }
}
}
if (
array_key_exists('enviar'$_POST) && !isset($error)){
if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "registrar_usuario")) {
  
$insertSQL sprintf("INSERT INTO prueba (nombre, pass) VALUES (%s, %s)",
                       
GetSQLValueString($_POST['nombre'], "text"),
                       
GetSQLValueString($_POST['pass'], "int"));

  
mysql_select_db($database_Cnn_usuarios$Cnn_usuarios);
  
$Result1 mysql_query($insertSQL$Cnn_usuarios) or die(mysql_error());

  
$insertGoTo "borrrraaaaameeee.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
}
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<form action="<?php echo $editFormAction?>" id="registrar_usuario" name="registrar_usuario" method="POST">nombre<br />
  <input type="text" name="nombre" id="nombre" />
.
<p>
    pass<br /> 
    <input type="text" name="pass" id="pass" />
  .</p>
 <p>

   <input type="submit" name="enviar" id="enviar" value="Enviar" />
 </p>
 <input type="hidden" name="MM_insert" value="registrar_usuario" />
</form><?php if(array_key_exists('enviar'$_POST) && isset($error)){
        echo 
$error;
        
        } 
?>
</body>
</html>
tambien me gustaria preguntar si alguien sabe para que es " GetSQLValueString() " y "sprintf()" por que eso me lo crea automaticamente DW y he leido que sirve para este tema ... pero no me ha quedado muy clara su funcion.
muchas gracias desde ya.

saludos!

Última edición por mauro770; 04/11/2009 a las 13:20 Razón: cambie el titulo