Hei! nessuno che ha voglia di darmi una mano?
Nel frattempo ho cercato di arrangiarmi da solo ed ho modificato la validazione sostituendo le funzioni deprecate (eregi) con preg_match. Ho anche cercato di utilizzare le funzioni filter e sanitize di php5.
Ecco qui il mio nuovo codice:
Codice PHP:
<?php
//If the form is submitted
if( isset($_POST['submit']) )
{
// NAME (requested) field validation
if (trim($_POST['name']) == '') {
$hasError = true;
echo "errore: NAME vuoto
";
} else {
$name = filter_input(trim($_POST['name']), FILTER_SANITIZE_STRING);
}
// TELEPHONE (optional) field validation
if (!preg_match('/[0-9]+/', trim($_POST['telephone']))) {
$hasError = true;
echo "errore: TELEPHONE con caratteri non permessi
";
} else {
$telephone = filter_input(trim($_POST['telephone']), FILTER_SANITIZE_NUMBER_INT);
}
// FAX (optional) field validation
if (!preg_match('/[0-9]+/', trim($_POST['fax']))) {
$hasError = true;
echo "errore: FAX con caratteri non permessi
";
} else {
$fax = filter_input(trim($_POST['fax']), FILTER_SANITIZE_NUMBER_INT);
}
// EMAIL (requested) field validation
if (trim($_POST['email']) == '') {
$hasError = true;
echo "errore: EMAIL vuoto
";
} else if (!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)) {
$hasError = true;
echo "errore: EMAIL con caratteri non permessi
";
} else {
$email = filter_input(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
}
// ADDRESS (optional) field validation
if (!preg_match('/[a-zA-Z0-9]+/', trim($_POST['address']))) {
$hasError = true;
echo "errore: ADDRESS con caratteri non permessi
";
} else {
$address = filter_input(trim($_POST['address']), FILTER_SANITIZE_STRING);
}
// CITY (optional) field validation
if (!preg_match('/[a-zA-Z]+/', trim($_POST['city']))) {
$hasError = true;
echo "errore: CITY con caratteri non permessi
";
} else {
$city = filter_input(trim($_POST['city']), FILTER_SANITIZE_STRING);
}
// Check to make sure MESSAGE (requested) were entered
if (trim($_POST['message']) == '') {
$hasError = true;
echo "errore: MESSAGGIO vuoto";
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = filter_input(trim($_POST['message']), FILTER_SANITIZE_SPECIAL_CHARS);
}
}
// If there is no error, send the email
if ( !$hasError ) {
$emailTo = 'indirizzo@dominio.est'; //destination email address here
$body = "Nome e Cognome: $name \n\n
Telefono: $telephone \n\n
Fax: $fax \n\n
Email: $email \n\n
Indirizzo: $address \n\n
Città: $city \n\n
Messaggio:\n $message";
$headers = 'From: Contact Form Sito <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<!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" xml:lang="it" lang="it">
<head>
<title>form</title>
</head>
<body id="contatti" onload="initialize()">
<?php if(isset($hasError)) { //If errors are found ?>
<hr /><p class="form-error" style="color:red">Messaggio di errore</p><hr />
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<hr /><p class="form-success" style="color:green">Email inviata correttamente</p><hr />
<?php } ?>
<h3>Contact form</h3>
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label class="inlined" for="name">Nome e Cognome</label>
<input type="text" class="input-text validate[required,custom[onlyLetter],length[0,50]]" id="name" name="name" value="<?php echo $_POST['name'] ?>" />
<label class="inlined" for="telephone">Telefono (optional)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyNumber],length[0,20]]" id="telephone" name="telephone" value="<?php echo $_POST['telephone'] ?>"/>
<label class="inlined" for="fax">Fax (opzionale)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyNumber],length[0,20]]" id="fax" name="fax" value="<?php echo $_POST['fax'] ?>"/>
<label class="inlined" for="email">Email</label>
<input type="text" class="input-text validate[required,custom[email],length[0,50]]" id="email" name="email" value="<?php echo $_POST['email'] ?>"/>
<label class="inlined" for="address">Indirizzo (opzionale)</span></label>
<input type="text" class="input-text validate[optional,length[0,100]]" id="address" name="address" value="<?php echo $_POST['address'] ?>" />
<label class="inlined" for="city">Citta' (opzionale)</span></label>
<input type="text" class="input-text validate[optional,custom[onlyLetter],length[0,50]]" id="city" name="city" value="<?php echo $_POST['city'] ?>" />
<label class="textarea inlined" for="message">Messaggio</label>
<textarea class="input-text validate[required,length[6,300]]" id="message" name="message" rows="6" cols="150"><?php echo $_POST['message'] ?></textarea>
<input class="button" type="submit" value="invia" name="submit" />
</form>
</body>
</html>
Nonostante ciò però ci sono ancora diversi errori:
1. il form erroneamente NON invia la mail se i campi opzionali (telephone, fax, address, city) non sono compilati
2. nella mail con vengono stampate le stringhe inserite nel form se non quella del message (probabilmente ho commesso qualche errore nello scrivere le funzioni di filter e sanitize)
Qualche dritta da chi ha l'occhio più allenato di me?
Thanks...