PHP HTML联系人表单中的验证码验证
几天来,我一直在努力将验证码应用到我的联系表格中。我阅读了多个教程和stackoverflow讨论,但似乎我仍然做了一些错误的事情(我对PHP还不是很有经验,所以我可能错过了一些东西) a) 我的联系人表单工作正常(发送电子邮件、检查空白字段等) b) 验证码显示正确 c) 验证码验证不起作用 对于联系人表单,我使用3个文件-.html文件-包含网页和两个.php文件(captcha.php用于创建captcha,send-email.php用于发送电子邮件和验证) 1) kontakt.html:PHP HTML联系人表单中的验证码验证,php,html,Php,Html,几天来,我一直在努力将验证码应用到我的联系表格中。我阅读了多个教程和stackoverflow讨论,但似乎我仍然做了一些错误的事情(我对PHP还不是很有经验,所以我可能错过了一些东西) a) 我的联系人表单工作正常(发送电子邮件、检查空白字段等) b) 验证码显示正确 c) 验证码验证不起作用 对于联系人表单,我使用3个文件-.html文件-包含网页和两个.php文件(captcha.php用于创建captcha,send-email.php用于发送电子邮件和验证) 1) kontakt.htm
<!DOCTYPE html><html lang="de">
<head>...</head>
<body>
<div class="contact-form-container">
<form class="contact-form" action="php/send-email.php" method="post" novalidate>
<div class="row">
<div class="column width-6">
<input type="text" name="fname" class="form-fname form-element large" placeholder="Firma*" tabindex="1" required>
</div>
<div class="column width-6">
<input type="text" name="lname" class="form-lname form-element large" placeholder="Ihr Name*" tabindex="2" required>
</div>
<div class="column width-6">
<input type="email" name="email" class="form-email form-element large" placeholder="E-Mail Adresse*" tabindex="3" required>
</div>
<div class="column width-6">
<input type="tel" name="telefonnummer" class="form-website form-element large" placeholder="Telefonnummer*" tabindex="4" required>
</div>
</div>
<div class="row">
<div class="column width-12">
<div class="field-wrapper">
<textarea name="message" class="form-message form-element large" placeholder="Nachricht*" tabindex="7" required></textarea>
</div>
</div>
<div class="column width-6">
<input type="text" name="honeypot" class="form-honeypot form-element large">
</div>
<div class="elem-group column width-12">
<label for="captcha">Captcha</label>
<img src="php/captcha.php" alt="CAPTCHA" class="captcha-image"><i class="form fas fa-redo refresh-captcha"></i>
<br><br>
<input class="form-message form-element" type="text" id="captcha" name="captcha_challenge" pattern="[A-Z]{6}">
</div>
<div class="column width-12">
<input type="submit" value="Senden" class="form-submit button medium bkg-theme bkg-hover-theme color-white color-hover-white">
</div>
</div>
</form>
<div class="form-response center"></div>
</div>
</body>
</html>
...
验证码
2) php(以生成验证码图像,据我所知,它工作正常)
3) send-email.php(发送电子邮件并进行验证)
您缺少会话_start()
on send email.php我在这里粘贴captcha.php
<?php
$post = [
'secret' => '6LfcOEcUAAAAABldSkevRJL75VSrvheVbc_4z9wU', //Enter the secret key provided by the google (just copy and paste it)
'response' => $_POST['googleResponse'],
];
$ch = curl_init();
echo
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
var_export ($response);
// echo $response;
?>
这是我的index.php
<!DOCTYPE html>
<html>
<head>
<title>captcha</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div id="html_element"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer>
</script>
<script src="captcha.js"></script>
</body>
</html>
验证码
使用此代码段包括captchav2验证。您只需将其包含在代码中,就可以开始了,它只包含与captchav2相关的代码,没有额外的内容。我希望它能帮助你。你为什么不使用谷歌的reCaptcha?做一个var_转储($\u POST['captcha_challenge'])代码>和变量转储($\会话['captcha\u text'])在比较它们之前查看它们包含的内容。在send email.php
file@MilanG没有帮助。除了执行recaptcha(有时点击10-20秒)可能需要很长时间这一事实外,并不是每个人都想使用第三方解决方案或强迫他们的客户培训谷歌的机器学习算法。谢谢,我不确定我怎么会错过它。它现在几乎可以工作了-如果用户输入正确的验证码,那么电子邮件就会被发送。但是,如果验证码不正确,则在提交后会冻结,我已将'else{echo'验证码验证错误。采取其他操作。';}'更改为else{die(验证码验证错误。采取其他操作。);}但在提供了错误的验证码后仍不会取消自身。我采用其他方法。如果$captcha!=$userCaptcha然后取消执行并返回错误。
if ($_POST['captcha_challenge'] == $_SESSION['captcha_text']) {
//code
else {
} echo '<p>Captcha verification is wrong. Take other action.</p>';
}
<?php
$post = [
'secret' => '6LfcOEcUAAAAABldSkevRJL75VSrvheVbc_4z9wU', //Enter the secret key provided by the google (just copy and paste it)
'response' => $_POST['googleResponse'],
];
$ch = curl_init();
echo
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
var_export ($response);
// echo $response;
?>
//////// GOOGLE RECAPTCHA CLIENT SIDE AND SERVER SIDE VERIFICATION /////////////
//----------------------------------------------------/
// onloadCallback()
// this
//----------------------------------------------------/
var onloadCallback = function() {
grecaptcha.render('html_element', { // oncallback render a div with id html_element
'sitekey' : '6LfcOEcUAAAAAAia1cMp60bnm1PMaFrmJ808il_D', // sitekey for the captcha
'theme' : 'light', // change the theme for light and dark
'widgetId': 'widgetId', // add widget id attribute which is optional
callback(){
console.log( 'another callback function here');
response = grecaptcha.getResponse(); // get the value of response when user submits recaptcha
console.log('response from google : ', response);
// send post method to captcha php that is usin curl post request for cross domain
$.post("captcha.php",
{
googleResponse: response // pass the google response
},
function(response, status){ // pass two parameters respnse and status
console.log("response after ssv : ", response, status);
if ( status == 'success'){
captchaSuccess = status;
captchares = captchaSuccess;
console.log("captchaSuccess :", captchaSuccess);
}
// alert response and the status here after verification from google
});
}
});
};
<!DOCTYPE html>
<html>
<head>
<title>captcha</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div id="html_element"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer>
</script>
<script src="captcha.js"></script>
</body>
</html>