我在我的网站上有google recaptcha,并有后端form-process.php文件来验证某些字段,例如名称/电子邮件和recaptcha,尽管recaptcha实时显示为打勾,但表单验证返回失败并说请检查Recaptcha吗?
任何人都可以帮助查看我的验证字段是否设置错误,或者recapthcas部分是否存在处理错误
该网站为sellgarrard301.com
先谢谢您
form_process.php和front-page.php如下:
form_process.php
<?php
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => 'xxx',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
$res = post_captcha($_POST['g-recaptcha-response']);
$name_error = $email_error = $captchafailed = "";
$name = $email = $message = $sent = "";
if (isset($_POST['submit']) AND (!$res['success'])) {
$captchafailed = "please check reCaptcha";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name1"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name1"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and ($res['success']) ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
$email = $_POST['email'];
$to = 'xxx';
$subject = 'Contact Form Submit';
$headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n" ;
if (mail($to, $subject, $message, $headers)) {
$sent = "Message sent";
$name = $email = $message = '';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
front-page.php
<?php get_header(); ?>
<article id="home">
<section class='section-class' data-section-name='sellgarrard301'>
<h1 class="home-h1">SELL<br class="title-wide">GARRARD<br class="title-wide">301.COM</h1>
<h2 class="home-h2">Selling your Garrard 301 or Garrard 401 turntable? Contact us on <a href="tel:07990738850">07990738850</a>
</h2>
</section>
</article>
<article id="getintouch">
<section class='section-class' data-section-name='speaktous'>
<div id="column-flex-left">
<div class="image-spacer"></div>
<div class="garrard-image"><img src="<?php bloginfo('template_directory'); ?>/images/detail301.jpg" alt="detail of 301 record turntable"></div>
<h1 class="git-h1">Selling your Garrard turntable?</h1>
</div>
<div id="column-flex-right">
<h2 class="git-h2">sellgarrard301.com comprises of a group of dedicated audio
enthusiasts with a passion for Garrard 301 or 401 record players.
To us these antique turntables represent the golden era of British
design and engineering, they can also sound wonderful too! We
are always interested in complete Garrard 301 record players in
plinths, motor units or parts, regardless of condition. – Click
contact
</h2>
<a class="quicklink" href="#contact-page">
<p class="quicklink">Contact us...</p>
</a>
</div>
</section>
</article>
<article id="cont-301">
<section class='section-class' data-section-name='Garrard-301'>
<div id="column-flex-left-301">
<div class="image-spacer"></div>
<div class="garrard-image"><img src="<?php bloginfo('template_directory'); ?>/images/301.jpg" alt="detail of 301 record turntable"></div>
<h1 class="git-h1-turntables">Wanted Garrard 301 Turntables</h1>
</div>
<div id="column-flex-right-301">
<h2 class="git-h2-turntables"><span style="font-style: italic;">Sell your Garrard 301 turntable</span></h2>
<h3 class="git-h3-turntables"><span style="font-weight: 700;">We buy your Garrard 301 turntables, any condition considered</span></h3>
<p class="git-p-turntables">Following the advent of CD’s many Garrard 301 turntables
found their way into lofts or storage. We want to give
them a new home! There are many reasons for
selling your turntable: downsizing, lack of use or sadly
bereavement are some of the reasons people call us. Over
the 20 years we have been working on Garrard 301’s we have
also become experienced in dealing with: Charities, solicitors
firms, house clearance experts, auction houses, BBC audio
engineers and studio engineers.</p>
</div>
</section>
</article>
<article id="cont-401">
<section class='section-class' data-section-name='Garrard-401'>
<div id="column-flex-left-301">
<div class="image-spacer"></div>
<div class="garrard-image"><img src="<?php bloginfo('template_directory'); ?>/images/401.jpg" alt="detail of 301 record turntable"></div>
<h1 class="git-h1-turntables">Wanted Garrard 401 Turntables</h1>
</div>
<div id="column-flex-right-301">
<h2 class="git-h2-turntables"><span style="font-style: italic;">Sell your Garrard 401 turntable</span></h2>
<h3 class="git-h3-turntables"><span style="font-weight: 700;">We buy your Garrard 401 turntables, any condition considered</span></h3>
<p class="git-p-turntables">sellgarrard301.com comprises of a small group of amateur audio enthusiasts
with a passion for Garrard 401 record players. These 50 year old units require restoration
in the same way a 50 year old car might.
If you Own a Garrard 301 we’d love to speak with you
We are always interested in complete Garrard 401 turntables or parts regardless of condition
and always pay cash on collection where possible.</p>
</div>
</section>
</article>
<article id="contact-page">
<section class='section-class' data-section-name='Contact'>
<?php include ('form_process.php');?>
<form id="contact" method="post" >
<div id="column-flex-left-contact">
<div class="image-spacer"></div>
<h1 class="h1-contact">Contact Sell Garrard 301 below or call us on <a href="tel:07990738850">07990738850</a></h1>
<fieldset id="field-no-ui">
<input placeholder="Your name" type="text" tabindex="1" name="name1" value="<?= $name ?>" >
</fieldset>
<span class="error"><?= $name_error ?></span>
<fieldset id="field-no-ui">
<input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2" >
</fieldset>
<span class="error"><?= $email_error ?></span>
</div>
<div id="column-flex-right-contact">
<fieldset id="field-no-ui">
<textarea id="field-no-ui" class="msg-area" placeholder="Type your Message Here...." name="message" value="<?= $message ?>" tabindex="3" ></textarea>
</fieldset>
<div class="g-recaptcha" data-sitekey="xxx" ></div>
<span class="captcha-failed"><?= $captchafailed; ?></span>
<span class="sent"><?= $sent; ?></span>
<fieldset id="field-no-ui-submit">
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
</div>
</form>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</section>
</article>
</div> <!-- END OF CONTAINER -->
<?php get_footer(); ?>