Aynı Sayfada İki Google reCAPTCHA Kullanma

Merhaba Arkadaşlar,

Bazen aynı sayfa içinde birden fazla form bulunabiliyor. Mesela inşaat firmaları sitelerinde Beni Arayın ve İletişim formları tek bir sayfa içinde bulunabiliyor. Bu durumda formlarda spamleri engellemek için nasıl Google reCAPTCHA uygulamasını aynı sayfa içinde birden fazla kullanabileceğimizi göstermek istiyorum.

Html sayfamız aşağıdaki gibi.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>

<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
    <script>
      var recaptcha1;
      var recaptcha2;
      var myCallBack = function() {
        //Render the recaptcha1 on the element with ID "recaptcha1"
        recaptcha1 = grecaptcha.render('recaptcha1', {
          'sitekey' : '', //Replace this with your Site key
          'theme' : 'light'
        });
        
        //Render the recaptcha2 on the element with ID "recaptcha2"
        recaptcha2 = grecaptcha.render('recaptcha2', {
          'sitekey' : '', //Replace this with your Site key
          'theme' : 'light'
        });
      };
    </script>
</head>
<body>

<header>

</header>

<!-- Site içinde ki HTML kodları -->

            <form method="POST" action="gonder.php" onsubmit="return kontrol();">
                <div class="form-group">
                    <input type="text" class="form-control" id="name" name="name" placeholder="Ad / Soyad" required>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon" required>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="email" name="email" placeholder="E-mail" required>
                </div>
                <div class="form-group">
                    <textarea class="form-control" id="message" name="message" placeholder="Mesajınız" rows="3" required></textarea>
                </div>
                <div class="col-md-8">
                    <div class="form-group">
                        <div id="recaptcha1"></div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="row">
                        <div class="help-block">
                            <div class="squared">
                                <input type="checkbox" value="privacy" id="privacy" name="privacy" >
                                <label for="privacy"></label>
                            </div>
                            <span style="font-size:10px">Proje ile ilgili gelişmelerden haberdar olmak istiyorum.</span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-7">
                            <button type="submit" class="btn btn-info gonder">Gönder</button>
                        </div>  
                    </div>
                </div>
            </form>

<!-- Site içinde ki HTML kodları -->

            <form method="POST" action="gonder1.php" onsubmit="return kontrol();">
                <div class="form-group">
                    <input type="text" class="form-control" id="name1" name="name1" placeholder="Ad / Soyad" required>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="telefon1" name="telefon1" placeholder="Telefon" required>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="email1" name="email1" placeholder="E-mail" required>
                </div>
                <div class="form-group">
                    <textarea class="form-control" id="message1" name="message1" placeholder="Mesajınız" rows="5" required></textarea>
                </div>
				<div class="form-group">
					<div id="recaptcha2"></div>
				</div>
                <button type="submit" class="btn btn-info">Gönder</button>                
                <div class="help-block">
                    <div class="squared">
                        <input type="checkbox" value="privacy" id="privacy1" name="privacy1">
                        <label for="privacy1"></label>
                    </div>
                    Proje ile ilgili gelişmelerden haberdar olmak istiyorum.
                </div>
            </form>
			
<!-- Site içinde ki HTML kodları -->			

<footer>

</footer>

</body>
</html>

 

Maili göndermek için kullanacağımız php sayfalarımız da aşağıdaki gibi.

gonder.php

<?php
session_start();

function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$";
      if (eregi($pattern, $email)){ 
         return true;
      }
      else {
         return false;
      }   
   }

@setlocale(LC_ALL,"tr_TR");
header("Content-type: text/html; charset=utf-8");
if ($_POST) {
$kime = "mail adresiniz";

    $ad_soyad = $_POST['name'];
	 $telefon = $_POST['telefon'];
	   $email = $_POST['email'];
	   $mesaj = $_POST['message'];

if(isset($_POST['recaptcha1'])){
echo "<script>alert('Lütfen robot olmadığınızı doğrulayın!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
        }		
if (!$ad_soyad or !$telefon or !$email or !$mesaj) {
echo "<script>alert('Bilgileri girdikten sonra tekrar deneyin!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
}
else if(!isValidEmail($email)){
echo "<script>alert('E-posta adresini kontrol edin!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
}
else {
$captcha=$_POST['recaptcha1'];
$secretKey = '';
        $response=file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
        $responseKeys = json_decode($response,true);
        if(!$responseKeys["success"] === true) {		
echo "<script>alert('Lütfen robot olmadığınızı doğrulayın!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
        } else {
        

@$Gidecek_Mesaj = "<p><strong>Konu: </strong>Form</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>Adi Soyadi: </strong>".$ad_soyad." <".$email."></p>\n";
@$Gidecek_Mesaj .= "<p><strong>Telefon: </strong>".$telefon."</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>E-Mail: </strong>".$email."</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>Mesaj: </strong>".$mesaj."</p>\n\n";

include "class.phpmailer.php";

$mail = new PHPMailer;
$mail->Host = ""; // Mail sunucusu
$mail->isSMTP(); 
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->IsHTML(true);
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet  ="SET NAMES UTF8";
$mail->Username = "noreply@..."; // Mail adresi
$mail->Password = ""; // Mail adresimizin sifresi
$mail->SetFrom("noreply@...", "Form"); // Mail attigimizda gorulecek ismimiz
$mail->AddAddress($kime);
$mail->Subject = "Form"; // Konu basligi
$mail->MsgHTML($Gidecek_Mesaj); // Mailin icerigi
if($mail->Send()){
    echo '<meta http-equiv="refresh" content="0;url=tesekkurler.html" />'; 
	exit();
} else {
    echo "<script>alert('Hata olustu!');</script>";
	echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
    exit();
}
}
}
}
?>

 

gonder1.php

 

<?php
session_start();

function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$";
      if (eregi($pattern, $email)){ 
         return true;
      }
      else {
         return false;
      }   
   }

@setlocale(LC_ALL,"tr_TR");
header("Content-type: text/html; charset=utf-8");
if ($_POST) {
$kime = "mail adresiniz"; 

    $ad_soyad = $_POST['name1'];
	 $telefon = $_POST['telefon1'];
	   $email = $_POST['email1'];
	   $mesaj = $_POST['message1'];


if(isset($_POST['recaptcha2'])){
echo "<script>alert('Lütfen robot olmadığınızı doğrulayın!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
        }	
if (!$ad_soyad or !$telefon or !$email or !$mesaj) {
echo "<script>alert('Bilgileri girdikten sonra tekrar deneyin!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
}
else if(!isValidEmail($email)){
echo "<script>alert('E-posta adresini kontrol edin!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
}
else {
$captcha=$_POST['recaptcha2'];
$secretKey = '';
        $response=file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
        $responseKeys = json_decode($response,true);
        if(!$responseKeys["success"] === true) {		
echo "<script>alert('Lütfen robot olmadığınızı doğrulayın!!');</script>";
echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
exit();
        } else {
        

@$Gidecek_Mesaj = "<p><strong>Konu: </strong>Form</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>Adi Soyadi: </strong>".$ad_soyad." <".$email."></p>\n";
@$Gidecek_Mesaj .= "<p><strong>Telefon: </strong>".$telefon."</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>E-Mail: </strong>".$email."</p>\n\n";
@$Gidecek_Mesaj .= "<p><strong>Mesaj: </strong>".$mesaj."</p>\n\n";

include "class.phpmailer.php";

$mail = new PHPMailer;
$mail->Host = ""; // Mail sunucusu
$mail->isSMTP(); 
$mail->SMTPAuth = true;
$mail->Port = 587; 
$mail->IsHTML(true);
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet  ="SET NAMES UTF8";
$mail->Username = "noreply@..."; 
$mail->Password = ""; // Mail adresimizin sifresi
$mail->SetFrom("noreply@...", "Form"); // Mail attigimizda gorulecek ismimiz
$mail->AddAddress($kime);
$mail->Subject = "Form"; // Konu basligi
$mail->MsgHTML($Gidecek_Mesaj); // Mailin icerigi
if($mail->Send()){
    echo '<meta http-equiv="refresh" content="0;url=tesekkurler.html" />'; 
	exit();
} else {
    echo "<script>alert('Hata olustu!');</script>";
	echo '<meta http-equiv="refresh" content="0;url=index.html" />'; 
    exit();
}
}
}
}
?>

 

Siz tabi tek bir php dosyası ile de yapabilirsiniz.

Mail göndermek için kullandığımız phpmailer sınıfı dosyalarına buradan ulaşabilirsiniz.

 

 

Bunlar da hoşunuza gidebilir...

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir