Jump to content

Cum Fac Un Formular De Contact Cu Captcha ?


gaby

Recommended Posts

Formularul de contact cu captcha va arata in felul urmator:

11111111111111111111111n.png

Pentru "construirea" lui, aveti nevoie de 2 fisiere si un font:

  • contact.php
  • captcha.php
  • monofont.ttf downloadabil de aici: monofont.zip

In fisierul contact.php introduceti codul de mai jos:


<?php session_start(); ?>
<!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" />
<meta name="robots" content="noindex,nofollow" />
<title>Contactati-ne !</title>

<style type="text/css">
<!--

* {
   padding:0;
   margin:0;
}

body {
   font-family:Verdana, Geneva, sans-serif;
   font-size:12px;
}

#contact_form div {
   margin:0 0 5px 0;
}

#contact_form label {
   width:90px;
   float:left;
   border:0px #900 solid;
   text-align:right;
   margin:0 5px 0 0;
}

#contact_form input {
   width:150px;
   border:1px #ccc solid;
}

#contact_form input:hover {
   border:1px #333 solid;
}

#contact_form textarea {
   width:300px;
   height:150px;
   border:1px #ccc solid;
}

#contact_form textarea:hover {
   border:1px #333 solid;
}

#contact_form input#trimite {
   width:auto;
   border:none;
   background:#333;
   color:#FFF;
}

#contact_form input#trimite:hover {
   border:none;
}

h1 {
   font-size:18px;
   color:#09F;
   font-weight:normal;
}

#scriptul_php {
   color:#C00;
   padding:0 0 0 95px;
   font-size:10px;
}
-->
</style>

</head>
<body>

<!--contact form-->
<form action="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>" method="post" id="contact_form">
   <h1 style=" padding:0 0 10px 95px;">
       Contactati-ne:
   </h1>
   
   <!--scriptul PHP-->
   <div id="scriptul_php">
   <?php
   //scriptul PHP se executa DOAR DACA este apasat butonul submit $_POST['trimite']
   if (isset($_POST['trimite'])) {
       //definesc arrayul in care voi strange mesajele de eroare
       $erori = array();
                               
       //curat de apostrofuri variabilele prin functia stripslashes()
       //elimin eventualele taguri din mesaj prin functia strip_tags()
       //bag in sesiune variabilele
       if (!empty($_POST['nume'])) {
           $nume = stripslashes(strip_tags($_POST['nume']));
           $_SESSION['nume'] = $nume;
       } else {
           $nume = NULL;
           $_SESSION['nume'] = '';
           
           $erori[] = 'Nu ati completat campul <strong>Nume</strong>.';
       }
                               
       if (!empty($_POST['telefon'])) {
           $telefon = stripslashes(strip_tags($_POST['telefon']));
           $_SESSION['telefon'] = $telefon;
           
           if (!is_numeric($telefon)) {
               $erori[] = 'Campul <strong>Telefon</strong> poate fi completat numai cu cifre.';
           }
       } else {
           $telefon = NULL;
           $_SESSION['telefon'] = '';
           
           $erori[] = 'Nu ati completat campul <strong>Telefon</strong>.';
       }
                               
       if (!empty($_POST['email'])) {
           $email = stripslashes(strip_tags($_POST['email']));
           $_SESSION['email'] = $email;
                                   
           if (!eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$", $email)) {
               $erori[] = 'Adresa de email nu este valida.';
           }
       } else {
           $email = NULL;
           $_SESSION['email'] = '';
                                   
           $erori[] = 'Nu ati completat campul <strong>Email</strong>.';
       }
                               
       if (!empty($_POST['mesaj'])) {
           $mesaj = stripslashes(strip_tags($_POST['mesaj']));
           $_SESSION['mesaj'] = $mesaj;
       } else {
           $mesaj = NULL;
           $_SESSION['mesaj'] = '';
           
           $erori[] = 'Nu ati completat campul <strong>Mesaj</strong>.';
       }
                               
       if ($_SESSION['cod_securitate'] !== $_POST['cod_securitate']) {
           $erori[] = 'Codul de securitate nu a fost introdus corect.';
       }
                               
                               
       //daca nu exista erori datele vor fi trimise
       if (empty($erori)) {
           //daca vreau sa trimit la mai multe adrese de email mesajul, separ adresele de email prin virgula
           //adica linia de cod va arata astfel:
           //$to = 'adresa1@yahoo.com,adresa2@yahoo.com,adresa3@yahoo.com';
           $to = 'adresa_dumneavoastra_de_mail@yahoo.com';
                                   
           $subject = substr($mesaj,0,20).'..';
                                   
           $body = 'Mesaj primit de pe site`ul '.$_SERVER['HTTP_HOST'].'<br /><br />'.
                   '<strong>Nume:</strong> '.$nume.'<br />
                    <strong>Telefon:</strong> '.$telefon.'<br />
                   <strong>Email:</strong> '.$email.'<br />
                   <strong>Mesaj:</strong> <br />
                   <p style=" border:1px #ccc solid; padding:5px; margin:0 0 50px 0;">'.$mesaj.'</p>
                   <strong style=" color:#cc0000;">Nota bene:</strong>
                   <br />
                   Adresa IP a expeditorului: '.$_SERVER['REMOTE_ADDR'].'<br />
                   Browserul si sistemul de operare folosite de expeditor: '.$_SERVER['HTTP_USER_AGENT'].'<br />
                   Data expedierii mesajului: '.date("j F Y, H:i:s",time() + (60 * 60 * 6));
                   //am adaugat  time() + (60 * 60 * 6) pentru a corecta ora decalata de fusul orar
                   //serverul pe care este hostat blogul se afla in SUA
                                                                           
                               
           $headers  = "From: ".$email."\r\n";
           $headers .= 'MIME-Version: 1.0'."\r\n";
           $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
                                   
           //daca datele au fost trimise cu succes se va afisa un mesaj de confirmare
           if (mail($to,$subject,$body,$headers)) {
               echo 'Mesajul dvs. a fost trimis. In cel mai scurt timp veti fi contactat(a).';
           }
       } else {
           foreach ($erori as $item) {
               echo $item.'<br />';
           }
       }
   }
   ?>
   </div>
   <!--/scriptul PHP-->
   
   <div>
       <label for="nume">Nume:</label>
       <input type="text" name="nume" id="nume" value="<?php echo $_SESSION['nume']; ?>" />
   </div>
   
   <div>
       <label for="telefon">Telefon:</label>
       <input name="telefon" type="text" id="telefon" value="<?php echo $_SESSION['telefon']; ?>" />
   </div>
   
   <div>
       <label for="email">Email:</label>
       <input type="text" name="email" id="email" value="<?php echo $_SESSION['email']; ?>" />
   </div>
   
   <div>
       <label for="mesaj">Mesajul Dvs:</label>
       <textarea name="mesaj" rows="1" cols="1" id="mesaj"><?php echo $_SESSION['mesaj']; ?></textarea>
   </div>
   
   <div style=" height:30px;">
       <label><img src="captcha.php" alt="captcha" /></label>
       <input name="cod_securitate" type="text" />
   </div>
   
   <div>
       <label>&nbsp;</label>
       <input type="submit" name="trimite" id="trimite" value="Trimite" />
   </div>
</form>
<!--/contact form-->

</body>
</html>[/code] Iar, in fisierul [b][color=orange]captcha.php[/color][/b] introduceti codul:
[code]<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

   var $font = 'monofont.ttf';

   function generateCode($characters) {
       /* list all possible characters, similar looking characters and vowels have been removed */
       $possible = '23456789bcdfghjkmnpqrstvwxyz';
       $code = '';
       $i = 0;
       while ($i < $characters) {
           $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
           $i++;
       }
       return $code;
   }

   function CaptchaSecurityImages($width='300',$height='50',$characters='4') {
       $code = $this->generateCode($characters);
       /* font size will be 75% of the image height */
       //specific inaltimea fontului caracterelor din imagine
       $font_size = $height * 0.75;
       $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
       /* set the colours */
       $background_color = imagecolorallocate($image, 255, 255, 255);
       $text_color = imagecolorallocate($image, 0, 0, 0);
       $noise_color = imagecolorallocate($image, 0, 153, 255);
       /* generate random dots in background */
       for( $i=0; $i<($width*$height)/3; $i++ ) {
           imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
       }
       /* generate random lines in background */
       for( $i=0; $i<($width*$height)/150; $i++ ) {
           imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
       }
       /* create textbox and add text */
       $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
       $x = ($width - $textbox[4])/2;
       $y = ($height - $textbox[5])/2;
       imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
       /* output captcha image to browser */
       header('Content-Type: image/jpeg');
       imagejpeg($image);
       imagedestroy($image);
       $_SESSION['cod_securitate'] = $code;
   }

}

//specific latimea si inaltimea imaginii si numarul de caractere din imagine
$width = isset($_GET['width']) ? $_GET['width'] : '90';
$height = isset($_GET['height']) ? $_GET['height'] : '30';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '5';

$captcha = new CaptchaSecurityImages($width,$height,$characters);
?>

P.S: Toate cele 3 fisiere (contact.php, captcha.php, monofont.ttf) trebuiesc puse in acelasi folder pe server.

Succes :pct:

  • Upvote 1

Te-ai inregistrat? Ne-ar placea sa te prezinti.

Cum pot sustine forumul?
Cumpara de la eMag folosind acest link.
--------------------
oG2BN9d.gifse1WdXd.gifQG6MtmI.gifRHYjDzD.gifG5p1wui.gif

Link to comment
Share on other sites

  • 1 year later...

Da-mi mai multe detalii te rog, ce fel de poza vrei sa adaugi, si unde anume ?

in cadrul acestui formular sa existe posibilitatea sa atasezi si o poza (sa zicem maxim 5 mega) sau un document arhivat si cind dai send

sa plece si poza (fisierul) odata cu formularul

multumesc anticipat !

Link to comment
Share on other sites

cit de cit am reusit sa adaptez formularul (nu nu prea stiu php) si mai m-am lovit de o problema.

Datele completate in formular raman, nu stiu daca din cauza memeoriei cache (nu stiu sa verific) chiar si dupa ce trmiti formularul sau eventual ii dau refresh.am incercat si in calculator (am instalat xammp) si pe server (folosesc si internet explorer si firefox)

Sper sa nu te deranjeje intrebarile mele.

Multumesc mult..

Link to comment
Share on other sites

cit de cit am reusit sa adaptez formularul (nu nu prea stiu php) si mai m-am lovit de o problema.

Datele completate in formular raman, nu stiu daca din cauza memeoriei cache (nu stiu sa verific) chiar si dupa ce trmiti formularul sau eventual ii dau refresh.am incercat si in calculator (am instalat xammp) si pe server (folosesc si internet explorer si firefox)

Sper sa nu te deranjeje intrebarile mele.

Multumesc mult..

Ciudat, nu ar trebui sa se comporte asa :-?...

Te-ai inregistrat? Ne-ar placea sa te prezinti.

Cum pot sustine forumul?
Cumpara de la eMag folosind acest link.
--------------------
oG2BN9d.gifse1WdXd.gifQG6MtmI.gifRHYjDzD.gifG5p1wui.gif

Link to comment
Share on other sites

Nu e nevoie, te cred. Trebuie sa il incerc si eu sa vad daca si la mine se intampla la fel.

Cel de aici: se comporta la fel ?

asta nu-si mai pastreaza datele in formular dupa ce trimiti mailul, insa primul mail arata mult mai bine e mai atragator, si pare mult mai serios, mai oficial.

Pentru atasament nu ai nici-o solutie?

as vrea mult ca odata ce am completat formularul sa pot sa trimit si o poza sau un alt fisier odata cu datele din formular !

asa cum este aici mai jos la attachments

Link to comment
Share on other sites

  • 7 months later...

Incearca:

<?php session_start(); ?> 
<!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" />
<meta name="robots" content="noindex,nofollow" />
<title>Contactati-ne !</title>

<style type="text/css">
<!--

* {
padding:0;
margin:0;
}

body {
font-family:Verdana, Geneva, sans-serif;
font-size:12px;
}

#contact_form div {
margin:0 0 5px 0;
}

#contact_form label {
width:90px;
float:left;
border:0px #900 solid;
text-align:right;
margin:0 5px 0 0;
}

#contact_form input {
width:150px;
border:1px #ccc solid;
}

#contact_form input:hover {
border:1px #333 solid;
}

#contact_form textarea {
width:300px;
height:150px;
border:1px #ccc solid;
}

#contact_form textarea:hover {
border:1px #333 solid;
}

#contact_form input#trimite {
width:auto;
border:none;
background:#333;
color:#FFF;
}

#contact_form input#trimite:hover {
border:none;
}

#contact_form input#reset {
width:auto;
border:none;
background:#333;
color:#FFF;
}

#contact_form input#reset:hover {
border:none;
}

h1 {
font-size:18px;
color:#09F;
font-weight:normal;
}

#scriptul_php {
color:#C00;
padding:0 0 0 95px;
font-size:10px;
}
-->
</style>

</head>
<body>

<!--contact form-->
<form action="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>" method="post" id="contact_form">
<h1 style=" padding:0 0 10px 95px;">
Contactati-ne:
</h1>

<!--scriptul PHP-->
<div id="scriptul_php">
<?php
//scriptul PHP se executa DOAR DACA este apasat butonul submit $_POST['trimite']
if (isset($_POST['trimite'])) {
//definesc arrayul in care voi strange mesajele de eroare
$erori = array();

//curat de apostrofuri variabilele prin functia stripslashes()
//elimin eventualele taguri din mesaj prin functia strip_tags()
//bag in sesiune variabilele
if (!empty($_POST['nume'])) {
$nume = stripslashes(strip_tags($_POST['nume']));
$_SESSION['nume'] = $nume;
} else {
$nume = NULL;
$_SESSION['nume'] = '';

$erori[] = 'Nu ati completat campul <strong>Nume</strong>.';
}

if (!empty($_POST['telefon'])) {
$telefon = stripslashes(strip_tags($_POST['telefon']));
$_SESSION['telefon'] = $telefon;

if (!is_numeric($telefon)) {
$erori[] = 'Campul <strong>Telefon</strong> poate fi completat numai cu cifre.';
}
} else {
$telefon = NULL;
$_SESSION['telefon'] = '';

$erori[] = 'Nu ati completat campul <strong>Telefon</strong>.';
}

if (!empty($_POST['email'])) {
$email = stripslashes(strip_tags($_POST['email']));
$_SESSION['email'] = $email;

if (!eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$", $email)) {
$erori[] = 'Adresa de email nu este valida.';
}
} else {
$email = NULL;
$_SESSION['email'] = '';

$erori[] = 'Nu ati completat campul <strong>Email</strong>.';
}

if (!empty($_POST['mesaj'])) {
$mesaj = stripslashes(strip_tags($_POST['mesaj']));
$_SESSION['mesaj'] = $mesaj;
} else {
$mesaj = NULL;
$_SESSION['mesaj'] = '';

$erori[] = 'Nu ati completat campul <strong>Mesaj</strong>.';
}

if ($_SESSION['cod_securitate'] !== $_POST['cod_securitate']) {
$erori[] = 'Codul de securitate nu a fost introdus corect.';
}


//daca nu exista erori datele vor fi trimise
if (empty($erori)) {
//daca vreau sa trimit la mai multe adrese de email mesajul, separ adresele de email prin virgula
//adica linia de cod va arata astfel:
//$to = 'adresa1@yahoo.com,adresa2@yahoo.com,adresa3@yahoo.com';
$to = 'adresa_dumneavoastra_de_mail@yahoo.com';

$subject = substr($mesaj,0,20).'..';

$body = 'Mesaj primit de pe site`ul '.$_SERVER['HTTP_HOST'].'<br /><br />'.
'<strong>Nume:</strong> '.$nume.'<br />
<strong>Telefon:</strong> '.$telefon.'<br />
<strong>Email:</strong> '.$email.'<br />
<strong>Mesaj:</strong> <br />
<p style=" border:1px #ccc solid; padding:5px; margin:0 0 50px 0;">'.$mesaj.'</p>
<strong style=" color:#cc0000;">Nota bene:</strong>
<br />
Adresa IP a expeditorului: '.$_SERVER['REMOTE_ADDR'].'<br />
Browserul si sistemul de operare folosite de expeditor: '.$_SERVER['HTTP_USER_AGENT'].'<br />
Data expedierii mesajului: '.date("j F Y, H:i:s",time() + (60 * 60 * 6));
//am adaugat time() + (60 * 60 * 6) pentru a corecta ora decalata de fusul orar
//serverul pe care este hostat blogul se afla in SUA


$headers = "From: ".$email."\r\n";
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

//daca datele au fost trimise cu succes se va afisa un mesaj de confirmare
if (mail($to,$subject,$body,$headers)) {
echo 'Mesajul dvs. a fost trimis. In cel mai scurt timp veti fi contactat(a).';
}
} else {
foreach ($erori as $item) {
echo $item.'<br />';
}
}
}
?>
</div>
<!--/scriptul PHP-->

<div>
<label for="nume">Nume:</label>
<input type="text" name="nume" id="nume" value="<?php echo $_SESSION['nume']; ?>" />
</div>

<div>
<label for="telefon">Telefon:</label>
<input name="telefon" type="text" id="telefon" value="<?php echo $_SESSION['telefon']; ?>" />
</div>

<div>
<label for="email">Email:</label>
<input type="text" name="email" id="email" value="<?php echo $_SESSION['email']; ?>" />
</div>

<div>
<label for="mesaj">Mesajul Dvs:</label>
<textarea name="mesaj" rows="1" cols="1" id="mesaj"><?php echo $_SESSION['mesaj']; ?></textarea>
</div>

<div style=" height:30px;">
<label><img src="captcha.php" alt="captcha" /></label>
<input name="cod_securitate" type="text" />
</div>

<div>
<label>&nbsp;</label>
<input type="submit" name="trimite" id="trimite" value="Trimite" />&nbsp;<input type="reset" name="reset" id="reset" value="Reset" />
</div>

</form>
<!--/contact form-->

</body>
</html>
[/code]

Fisierul [i][color="#FF0000"]captcha.php[/color][/i] ramane acelasi.

Te-ai inregistrat? Ne-ar placea sa te prezinti.

Cum pot sustine forumul?
Cumpara de la eMag folosind acest link.
--------------------
oG2BN9d.gifse1WdXd.gifQG6MtmI.gifRHYjDzD.gifG5p1wui.gif

Link to comment
Share on other sites

  • 1 month later...

cum as putea sa fac sa adaug la link in care sa completeze rubrica nume din formular, deci formularul este la adresa : www.sitetaul.ro/inregistrare.php, completand rebrica nume, scriu urmatorul link: www.siteulmeu.ro/inregistrare.php?nume=numetest dar nu apare nimica la rubrica completata, putin ajutor, rog frumos :) multumesc

ps. inca o rugaminte daca se poate, la rubrica telefon, cum as putea modifica php-ul sa poate lasa si spatiu intre cifre? multumesc frumos din nou

Link to comment
Share on other sites

  • 10 months later...
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.