Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/05/2010, 16:59
Avatar de CCB
CCB
 
Fecha de Ingreso: noviembre-2009
Ubicación: Perú
Mensajes: 65
Antigüedad: 14 años, 5 meses
Puntos: 3
Formulario de Contacto Seguro en PHP

Bueno este es un formulario de contacto hecho en php. Esta funcionando OK, ya que renove un script antiguo e inseguro. La pregunta es la siguiente que tan seguro es este script que acabo de hacer. La verdad que recien estoy por el curso de seguridad en php, y px siempre hay dudas. Por otro lado este script sirve como aportación para los mas novatos en PHP.

Código PHP:
$name htmlspecialchars($_POST['name']);
$email htmlspecialchars(str_replace(" ","",$_POST['email']));
$mensaje htmlspecialchars($_POST['mensaje']);

function 
comprobar_email($email){
        
//compruebo unas cosas primeras
        
if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
           if ((!
strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
              
//miro si tiene caracter .
              
if (substr_count($email,".")>= 1){
                 
//obtengo la terminacion del dominio
                 
$term_dom substr(strrchr ($email'.'),1);
                 
//compruebo que la terminación del dominio sea correcta
                 
if (strlen($term_dom)>&& strlen($term_dom)<&& (!strstr($term_dom,"@")) ){
                    
//compruebo que lo de antes del dominio sea correcto
                    
$antes_dom substr($email,0,strlen($email) - strlen($term_dom) - 1);
                    
$caracter_ult substr($antes_dom,strlen($antes_dom)-1,1);
                    if (
$caracter_ult != "@" && $caracter_ult != "."){
                       return 
true;
                    }
                    else {
                        return 
false;
                    }
                 }
              }
           }
        }
}


if (empty(
$name) || empty($mensaje)) {
    echo 
"Rellene todos los campos del formulario, Volver al <a href=\"form.html\">FORM</a>";

else
{
    if (
strlen ($name) > '100') { 
    echo 
"El Nombre no debe superar los 100 caracteres";
    }
    else
    {
        if (
strlen ($mensaje) > '600') { 
         echo 
"Mensaje muy grande"
         }
         else
         {
            if (
comprobar_email($email)) {
               
                
$remitente "[email protected]"/* Correo a donde se enviara el mensaje */
                
$destinatario "[email protected]"/* Correo que envia el mensaje, es util para que no envie siempre el mensaje a correo no deseado */

                
$headers "MIME-Version: 1.0 \r\n";
                
$headers .= "From: $destinatario \r\n";
                
$headers .= "Reply-To: $remitente \r\n";
                
$headers .= "Return-path: $remitente \r\n";
                
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n"

                
$body "
                <table><tr><td width=\"150\" valign=\"top\"><strong>Nombre:</strong></td><td> "
.$name."</td></tr>"."
                <tr><td valign=\"top\"><strong>Email:</strong></td><td> "
.$email."</td></tr>"."
                <tr><td valign=\"top\"><strong>Mensaje:</strong></td><td>"
.$mensaje."</td></tr></table>";
                  
                
mail($remitente,"Probando",$body$headers);
                echo 
"Mensaje enviado con Exito";  
            } else
            {
                echo 
"Esta mal escrito el mail, porfavor volver a <a href=\"form.html\" >FORM</a>";
            }
        }
    }

form.html
Código HTML:
<html>
<head>
<title>Formulario de Contacto</title>
<script type="text/javascript">
function setFocus(aField) {
document.forms[0][aField].focus();
}

function isAnEmailAddress(aTextField) {

if (document.forms[0][aTextField].value.length<5) {
return false;
}
else if (document.forms[0][aTextField].value.indexOf("@") < 3) {
return false;
}
else if (document.forms[0][aTextField].value.length -
 document.forms[0][aTextField].value.indexOf("@") < 4) {
return false;
}
else { return true; }
}

function isEmpty(aTextField) {
if ((document.forms[0][aTextField].value.length==0) ||
 (document.forms[0][aTextField].value==null)) {
return true;
}
else { return false; }
}
function validate() {

if (isEmpty("name")) {
	alert("Por favor, rellene su nombre.");
	setFocus("name");
	return false;
}

if (isEmpty("mensaje")) {
	alert("Por favor, Escriba su mensaje.");
	setFocus("mensaje");
	return false;
}

if (!isAnEmailAddress("email")) {
	alert("La dirección de correo electrónico no es válida.");
	setFocus("email");
	return false;
}
return true;

}
</script>
</head>
<body>

<form action="enviar.php" method="post" onSubmit="return validate();">
Nombre
<input type="text" name="name" /><br /><br />
Email
<input type="text" name="email" /><br /><br />
Mensaje
<textarea type="text" name="mensaje"></textarea><br /><br />
<input type="submit"  value="Enviar" />
</form>
</body>
</html>