如何使Recaptcha与验证一起使用

时间:2018-10-30 21:51:16

标签: php recaptcha

我在代码中尝试了此操作,然后输入了正确的密钥,只是不想将其放在那里,但是每次我提交表单时,即使检查了它也显示出未填写的错误,没有形式的验证码东西就可以了。有人可以帮我解决此问题,我希望它可以确保在您将其发送到我的电子邮件之前将其填写完毕!

<h2 class="text-center" id="whatwedo">Contact form</h2><hr class="titlehr"></div></div><br>
    <?php
    if (isset($_POST["submit"])) {
        $fullname = $_POST['fullname'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $subject = $_POST['subject'];
        $description = $_POST['description'];
           $captcha = $_POST['g-recaptcha-response'];

        $from = $fullname; 
        $to = 'mcgarrywebdesign@gmail.com'; 
        $subject = 'Contact Form';

        $body = "From: $fullname\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n description: $description";

        // Check if name has been entered
        if (!$_POST['fullname']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['phone']) {
            $errPhone = 'Please enter your phone number';
        }
        if (!$_POST['subject']) {
            $errSubject = 'Please enter the subject';
        }
        if (!$_POST['description']) {
            $errDescription = 'Please enter the description';
        }



        if(!$captcha){
          $errcaptcha = "Please check the the captcha form";

        }


        // If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errSubject && !$errDescription && !$errcaptcha) {
      $secretKey = "secret key";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          $result =  "You are spammer ! Get the @$%K out";
        } else {



    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! We will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }}
?>
<?php echo "<p class='text-danger'>$result</p>";?>
    <div class="row">
        <div class="col-md-12 col-lg-6"> <form action="contact.php" method="post">

  <div class="form-group">
    <label for="exampleInputEmail1">Full Name</label>
    <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Full Name" name="fullname">
    <?php echo "<p class='text-danger'>$errName</p>";?>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Email Address</label>
    <input type="email" class="form-control" id="exampleInputPassword1" placeholder="Enter Email" name="email">
   <?php echo "<p class='text-danger'>$errEmail</p>";?>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Phone Number</label>
    <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Phone Number" name="phone">
    <?php echo "<p class='text-danger'>$errPhone</p>";?>
  </div>
</div>
        <div class="col-md-12 col-lg-6">  
   <div class="form-group">
    <label for="exampleInputEmail1">Subject</label>
    <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Subject" name="subject">
    <?php echo "<p class='text-danger'>$errSubject</p>";?>
  </div>
       <div class="form-group">
    <label for="exampleTextarea">Description</label>
    <textarea class="form-control" id="exampleTextarea" rows="3" name="description" placeholder="Description"></textarea>
    <?php echo "<p class='text-danger'>$errDescription</p>";?>
  </div>

 <br>

            </div></div>


            <div class="g-recaptcha text-center mx-auto d-block" data-sitekey="6LdhyXcUAAAAANrj8qTSLKcrbjVX6ij07Dqw0awe"></div>
<?php echo "<p class='text-danger'>$errcaptcha</p>";?>


        <div class="row"><div class="col-lg-12"><button type="submit" name="submit" value="send" class="btn btn-primary mx-auto d-block" style="width: 190px !important;height: 60px !important;font-size: 25px;">Submit</button></div></div></form>
    </div>

0 个答案:

没有答案
相关问题