Contact Form process in PHP with google captcha validation.
First Create Google reCAPTCHA here.
Protect your website from spam and abuse while letting real people pass through with ease.
The new reCAPTCHA is here. A significant number of your users can now attest they are human without having to solve a CAPTCHA. Instead with just a single click they’ll confirm they are not a robot. We’re calling it the No CAPTCHA reCAPTCHA experience.
submit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <?php if(isset($_POST['email']) && !empty($_POST['email'])) { if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) { //your site secret key $secret = 'your site secret key'; //get verify response data $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']); $responseData = json_decode($verifyResponse); if($responseData->success){ //contact form submission code $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $msg=$_POST['msg']; $date=date('d-m-Y H:i:s'); $readed="no"; $read_date="no"; $email_to = "info@yoursite.com"; $email_subject = "Email From Your Website"; $message = "\n\nName: ".$name. "\n\nEmail: ".$email. "\n\nPhone: ".$phone. "\n\nMessage: ".$msg."\n\n"; // create email headers // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= "X-Priority: 3\r\n"; $headers .= "X-Mailer: PHP". phpversion() ."\r\n"; // More headers $headers .= "Reply-To:info@sendermail.com\r\n"; $headers .= "Return-Path:info@sendermail.com\r\n"; $headers .= 'From:info@sendermail.com' . "\r\n"; $mail_sent=@mail($email_to, $email_subject, $message, $headers); // insert in to DB $name2=htmlspecialchars($_POST['name'],ENT_QUOTES); $email2=htmlspecialchars($_POST['email'],ENT_QUOTES); $phone2=htmlspecialchars($_POST['phone'],ENT_QUOTES); $msg2=htmlspecialchars($_POST['msg'],ENT_QUOTES); $insert=mysql_query("INSERT INTO contact(name,email,phone,msg,date,readed,read_date) VALUES ('$name2','$email2','$phone2','$msg2','$date2','$readed2','$read_date2')"); echo"<div class='alert alert-success'>Thank You! We will get back to you as soon as possible!</div>"; } else { $errMsg = "<div class='alert alert-danger'>Robot verification failed, please try again.</div>"; } } else { $errMsg = "<div class='alert alert-danger'>Please click on the reCAPTCHA box.</div>"; } echo"$errMsg"; } ?> |