Codice PHP:
if(isset($_POST['submit'])){
session_start();
if(sha1($_POST['codiceverifica']) != $_SESSION['control']){
$error_code="[b]Campo vuoto o non corretto[/b]
";
$t++;
}
$categoria=trim($_POST['categoria']);
$nome= trim($_POST['nome']);
$mail= trim($_POST['mail']);
if(empty($categoria)){
$error= "errore nel campo";
$t++;
}
if(empty($nome)){
$error_nome= "campo vuoto";
$t++;
}
if(empty($mail)){
$error_mail= "campo vuoto";
$t++;
}
else{
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $mail)){
$error_mail="Mail scritta non correttamente";
}
}
if($t ==0){
mail("atuttascienza@live.it","subject","messaggio","header");
echo "inviato";
}
else{
echo "<form name=\"FORM\" method=\"post\" action=\"\">";
echo "<input name=\"categoria\" type=\"text\" value=\"".$categoria."\">$error
";
echo "<input name=\"nome\" type=\"text\" value=\"".$nome."\">$error_nome
";
echo "<input name=\"mail\" type=\"text\" value=\"".$mail."\">$error_mail
";
echo "<img src=\"captcha.php\" width=\"200\" height=\"60\" alt=\"Immagine captcha\">
Inserisci il codice in figura:
<input type=\'text\' name=\'codiceverifica\' size=\"25\">$error_code</p>";
echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
}
}
else{
echo "<form name=\"FORM\" method=\"post\" action=\"\">";
echo "<input name=\"categoria\" type=\"text\">
";
echo "<input name=\"nome\" type=\"text\">
";
echo "<input name=\"mail\" type=\"text\">
";
echo "<img src=\"http://www.atuttascienza.altervista.org/secur/captcha.php\" width=\"200\" height=\"60\" alt=\"Immagine captcha\">
Inserisci il codice in figura:
<input type=\'text\' name=\'codiceverifica\' size=\"25\"></p>";
echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
}
captcha.php
Codice PHP:
session_start();
$x = 200;
$y = 75;
//creaiamo la stringa random e criptiamola
$crypt = sha1(microtime() * mktime());
//preleviamo una porzione della stringa
$str = substr($crypt,0,6);
$code = $str;
$space = $x / (strlen($code)+1);
$img = imagecreatetruecolor($x,$y);
$bg = imagecolorallocate($img,255,255,255);
$border = imagecolorallocate($img,0,0,0);
$colors[] = imagecolorallocate($img,128,64,192);
$colors[] = imagecolorallocate($img,192,64,128);
$colors[] = imagecolorallocate($img,108,192,64);
imagefilledrectangle($img,1,1,$x-2,$y-2,$bg);
imagerectangle($img,0,0,$x-1,$y-2,$border);
for ($i=0; $i< strlen ($code); $i++)
{
$color = $colors[$i % count($colors)];
imagettftext($img,28+rand(0,8),-20+rand(0,40),($i+0.3)*$space,50+rand(0,10),$color,'Vera.ttf',$code{$i});
}
for($i=0;$i<400;$i++)
{
$x1 = rand(3,$x-3);
$y1 = rand(3,$y-3);
$x2 = $x1-2-rand(0,8);
$y2 = $y1-2-rand(0,8);
imageline($img,$x1,$y1,$x2,$y2,$colors[rand(0,count($colors)-1)]);
}
$_SESSION['control'] = sha1($str);
header("Content-type: image/png");
imagepng($img);
allora ho un form contatti che è controllato nella stessa pagina..se campi sono vuoti o non corretti viene segnalato bloccando l'invio alla mail...
il codice captcha però non funziona...la condizione non funziona...nel vecchio form che avevo il meccanismo però funzionava...forse la sessione che passo non funziona...
qualche idea?