Tutorial: Formular de contact cu protectie anti spam

Va voi prezenta in continuare un formular simplu de contact in PHP, care are o protectie anti spam sub forma unei imagini captcha. Formularul va trimite datele introduse catre o adresa de mail. Codul PHP il gasiti mai jos:

Trebuie sa avem 3 fisiere PHP.

  • unul index.php (sau orice alt fisier) unde introducem formularul de contact
  • un alt fisier mailer.php unde se seteaza adresa de mail unde dorim sa se trimita datele din formular
  • un alt fisier verificare.php unde se verifica imaginea, folosita ca protectie anti spam.

Sa incepem cu formularul ce trebuie introdus in orice fisier unde dorim afisarea acestuia:

<form action=”mailer.php” method=”post” name=”formular” id=”formular” style=”margin:0px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; width:400px;” onsubmit=”MM_validateForm(‘mail’,”,’TrimiteMail’,’subiect’,”,’R’,’verificare’,”,’R’,’mesaj’,”,’R’);return document.MM_returnValue”>

Emailul dv:<br />
<input name=”mail” type=”text” id=”mail” style=”padding:2px; border:1px solid #CCCCCC; width:180px; height:14px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;” value=”<?php echo $_GET['mail'];?>”/>
<br />
<br />

Subiect:<br />
<input name=”subiect” type=”text” id=”subiect” style=”padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;” value=”<?php echo $_GET['subiect'];?>”/>
<br />
<br />

Introduceti codul de verificare:<br />
<input name=”verifica” type=”text” id=”verifica” style=”padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;”/>
<img src=”verificare.php?<?php echo rand(0,9999);?>” alt=”cod anti spam” width=”50″ height=”25″ align=”bottom” /><br />
<br />

<?php if(isset($_GET['cod_gresit'])){?>
<div style=”border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;”>Cod gresit</div><br />
<?php ;}?>

Mesaj:<br />
<textarea name=”mesaj” cols=”6″ rows=”5″ id=”mesaj” style=”padding:2px; border:1px solid #CCCCCC; width:300px; height:100px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;”><?php echo $_GET['mesaj'];?></textarea>

<input name=”Submit” type=”submit” style=”margin-top:10px; display:block; border:1px solid #000000; width:100px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;” value=”Trimite mesajul”/>
</form>

Mai jos aveti continutul fisierului mailer.php:
Nu uitati sa schimbati “AICI ADRESA DE MAIL” cu emailul unde doriti sa fie trimis formularul.

<?php
$subiect = $_REQUEST["subiect"];
$mesaj = $_REQUEST["mesaj"];
$mail = $_REQUEST["mail"];
$verifica = $_REQUEST["verifica"];

$mesaj = stripslashes($mesaj);
$subiect = stripslashes($subiect);
$mail = stripslashes($mail);

if(md5($verifica).’abcd’ == $_COOKIE['formcont']){
mail(“AICI ADRESA DE MAIL”, ‘Formular de contact: ‘.$subiect, $_SERVER['REMOTE_ADDR'].”\n\n”.$mesaj, “De la: $mail”);
// stergere cookie:
setcookie(‘formcont’,”);
} else {
header(“Location:”.$_SERVER['HTTP_REFERER'].”?subiect=$subiect&mail=$mail&mesaj=$mesaj&cod_gresit=true”);
exit;
}
?>
<!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=iso-8859-1″ />
<title>E-Mail Trimis</title>
<style type=”text/css”>
<!–
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
–>
</style></head>

<body>
Mesaj trimis.<br />
Va multumim.<br />
<br />
Inapoi la <a href=”http://www.revistait.ro/”>pagina principala</a>
</body>
</html>

Si in final fisierul verificare.php:

<?php
header(‘Content-type: image/jpeg’);

$width = 50;
$height = 24;

$imagine = imagecreatetruecolor($width, $height);

imagefill($imagine, 0, 0, 0xFFFFFF);

// add noise
for ($c = 0; $c < 40; $c++){
$x = rand(0,$width-1);
$y = rand(0,$height-1);
imagesetpixel($imagine, $x, $y, 0×000000);
}

$x = rand(1,10);
$y = rand(1,10);

$rand_string = rand(1000,9999);
imagestring($imagine, 5, $x, $y, $rand_string, 0×000000);

setcookie(‘formcont’,(md5($rand_string).’abcd’));

imagejpeg($imagine);
imagedestroy($imagine);
?>

Sper sa va fie de ajutor acest tutorial.

Alternativ, sunt site-uri unde se poate face gratuit un formular de contact si multe alte tipuri de formulare, cu protectia anti-spam CAPTCHA si codul poate fi copiat de catre orice site.

VN:F
Nota articol: 3.5/5 (10 voturi in total)
8 Comments

Add a Comment

Your email address will not be published. Required fields are marked *