Recaptcha验证失败:请检查验证码

时间:2016-03-07 09:12:46

标签: php recaptcha

相关表格:

     <script language="JavaScript" src="gen_validatorv4.js" type="text/javascript"></script>
        <script src='https://www.google.com/recaptcha/api.js'></script>
   <div id="contactform">
            <div id="details">
        <form method="POST" name="contactform" action="contact-form-handler.php"> 
            <p>
                <label for='name'>Full name *:</label><br>
                <input type="text" name="name">
            </p>
            <p>
                <label for='address'>Address *:</label><br>
                <input type="text" name="address">
            </p>
            <p>
                <label for='unit'>Unit:</label><br>
                <input type="text" name="unit">
            </p>
            <p>
                <label for='postal'>Postal code *:</label><br>
                <input type="text" name="postal">
            </p>
            <p>
                <label for='email'>Email:</label> <br>
                <input type="text" name="email">
            </p>
            <p>
                <label for='phone'>Telephone *:</label><br>
                <input type="text" name="phone">
            </p>
             <p>
                <label for='date'>Preferred pickup date* :</label> <br>
                <input type="date" name="date">
            </p>
            <p>
                <label for='time'>Preferred timeslot* :</label><br>
                <span id="small">Please provide us with a 2 Hr time slot<br> (10% discount if we don't make it in time <strong>after we agreed</strong> on a timeslot</span><br>
                <input type="text" name="time1"> to <input type="text" name="time2">
            </p>
            </div>
            <div id="products">

            <p><label for='itemsharp'>Items for sharpening:</label><br>
                <textarea name="itemsharp"></textarea>
            </p>
            <p>
                <label for='itemrepair'>Items for repair:</label><br>
                <textarea name="itemrepair"></textarea>
            </p>
            <p>
                <label for='comment'>Comment: </label><br>
                <textarea name="comment"></textarea>
            </p>
            <div class="g-recaptcha" data-sitekey="[mysitekey]" data-theme="dark"></div>
            <input class="button" type="submit" value="Submit"><br>
        </form>
            </div>
        </div>

php:

<?php 
$errors = '';
$myemail = '[aperfectlyvalidemailadress]';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['address']) ||
   empty($_POST['postal']) ||
   empty($_POST['date']) ||
   empty($_POST['time1']) ||
   empty($_POST['time2']) ||
   empty($_POST['phone']))
{
    $errors .= "\n Error: fields with a * are required";
}


$name = $_POST['name']; 
$email_address = $_POST['email'];
$address  = $_POST['address'];
$postal = $_POST['postal'];
if(!empty($_POST['unit'])){
    $unit = $_POST['unit'];
}
$phone = $_POST['phone'];
$date = $_POST['date'];
$time1 = $_POST['time1'];
$time2 = $_POST['time2'];
$itemsharp = $_POST['itemsharp'];
$itemrepair = $_POST['itemrepair'];
$comment = $_POST['comment'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Please provide a valid email address";
}

       $captcha;
        if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the captcha form.</h2>';
          exit;
        }
        $secretKey = "[mysecretkeyisfilledinhere]";
        $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) {
          echo '<h2>You are spammer ! Get the @$%K out</h2>';
        } else {

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Sharpening request: $name";
    $email_body = "You have received a new sharpening request. ".
    "From:\n Name: $name \n Email: $email_address \n Phone: $phone \n Address: $address $unit $postal \n \n Date: $date \n Between: $time1 and $time2 \n Items for sharpening: $itemsharp \n Items for repair: $itemrepair \n Comment: \n $comment "; 

    $headers = "From: request@sharpsteel.ca\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: requestthankyou.html');
} 
        }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Request failed</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

其中大部分是从各种来源拼凑而成的。我的php并不那么强大。

我在第一次创建时测试了它,它工作正常。

现在这个网站已经上线了....没什么。任何提交表单的尝试都会导致&#34;请检查验证码&#34;错误页面。我没有改变任何事情,这就是为什么我感到沮丧。

由于显而易见的原因,我在这里模糊了电子邮件地址和密钥。

我已经查看了其他问题,但它们不相关(使用不同且对我不熟悉的语言)或者我根本不理解我为此道歉。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

html表单存在明确的标记问题 - 有div标签打破了表单。

下面的标记看起来是相同的,但请放心,表单的格式和div标签已经更改,以确保表单是vald。

<script language="JavaScript" src="gen_validatorv4.js" type="text/javascript"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div id="contactform">
    <form method="POST" name="contactform" action="contact-form-handler.php">
        <div id="details">
            <p>
                <label for='name'>Full name *:</label>
                <br>
                <input type="text" name="name">
            </p>
            <p>
                <label for='address'>Address *:</label>
                <br>
                <input type="text" name="address">
            </p>
            <p>
                <label for='unit'>Unit:</label>
                <br>
                <input type="text" name="unit">
            </p>
            <p>
                <label for='postal'>Postal code *:</label>
                <br>
                <input type="text" name="postal">
            </p>
            <p>
                <label for='email'>Email:</label>
                <br>
                <input type="text" name="email">
            </p>
            <p>
                <label for='phone'>Telephone *:</label>
                <br>
                <input type="text" name="phone">
            </p>
            <p>
                <label for='date'>Preferred pickup date* :</label>
                <br>
                <input type="date" name="date">
            </p>
            <p>
                <label for='time'>Preferred timeslot* :</label>
                <br>
                <span id="small">Please provide us with a 2 Hr time slot<br> (10% discount if we don't make it in time <strong>after we agreed</strong> on a timeslot</span>
                <br>
                <input type="text" name="time1"> to
                <input type="text" name="time2">
            </p>
        </div>
        <div id="products">
            <p>
                <label for='itemsharp'>Items for sharpening:</label>
                <br>
                <textarea name="itemsharp"></textarea>
            </p>
            <p>
                <label for='itemrepair'>Items for repair:</label>
                <br>
                <textarea name="itemrepair"></textarea>
            </p>
            <p>
                <label for='comment'>Comment: </label>
                <br>
                <textarea name="comment"></textarea>
            </p>
            <div class="g-recaptcha" data-sitekey="[mysitekey]" data-theme="dark"></div>
            <input class="button" type="submit" value="Submit">
            <br>
        </div>
    </form>
</div>