php 个提醒和 google 个验证码
php alerts and google captcha
我有一个密码请求页面,我正在使用 google 验证码,我的问题是如何让它们同时触发。
http://www.camteach.com/user-password-request.php
如果我在没有完成任何事情的情况下点击提交,我会同时收到 "Please check the anti-spam 'I am not a robot' checkbox!" 和 "No email specified!"
的警告
<?php function validateGoogleCaptcha(){
$errorsAndAlerts = "";
if (!@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "Please check the anti-spam 'I am not a robot' checkbox!<br/>\n"; }
else {
// check recaptcha
$postdata = array();
$postdata['secret'] = '6LcwKCwUAAAAAK5CXed1YJGfMk7iVBL5NgN2vPVd';
$postdata['response'] = @$_REQUEST['g-recaptcha-response'];
$postdata['remoteip'] = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?". http_build_query($postdata, '', '&');
list($json, $httpStatusCode, $headers, $request) = getPage($url, 5, '', true);
$recaptchaResponse = json_decode($json, true);
if (!$recaptchaResponse['success']) {
if (is_array($recaptchaResponse['error-codes'])) {
if (in_array('missing-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (no secret)<br/>\n"; }
if (in_array('invalid-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (invald secret)<br/>\n"; }
if (in_array('missing-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please fill out the recaptcha box!<br/>\n"; }
if (in_array('invalid-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please fill out the recaptcha box again, your answer was incorrect!<br/>\n"; }
}
if (!$errorsAndAlerts) { $errorsAndAlerts .= "Invalid captcha response, please try again or contact us directly and let us know."; }
@trigger_error("Failed recaptcha on signup form", E_USER_NOTICE);
}
}
return $errorsAndAlerts;
}
// error checking
$errorsAndAlerts = alert();
if (@$CURRENT_USER) {
$errorsAndAlerts = "You are already logged in! <a href='/'>Click here to continue</a> or <a href='?action=logoff'>Logoff</a>.";
}
?>
<?php // process form
if (@$_POST['action']):?>
<?php $errorsAndAlerts = "";
$errorsAndAlerts .= validateGoogleCaptcha();
?>
<?php endif ?>
<?php
### send reset email
if (@$_POST['action'] == 'sendPasswordReminder') {
global $SETTINGS, $TABLE_PREFIX;
// display errors
if (!@$_REQUEST['usernameOrEmail']) { $errorsAndAlerts .= "No email specified!<br/>\n"; }
// send emails
if (@$_REQUEST['usernameOrEmail']) {
$where = mysql_escapef("? IN (`username`,`email`)", $_REQUEST['usernameOrEmail']);
$user = mysql_get(accountsTable(), null, $where);
// send message
if ($user) {
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'USER-PASSWORD-RESET',
'placeholders' => array(
'user.username' => $user['username'],
'user.email' => $user['email'],
'loginUrl' => realUrl($GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']),
'resetUrl' => realUrl($GLOBALS['WEBSITE_LOGIN_RESET_URL'] . "?userNum={$user['num']}&resetCode=" . _generatePasswordResetCode( $user['num'] )),
)));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }
//
$errorsAndAlerts .= "Thanks, we've emailed you instructions on resetting your password.<br/><br/>
If you don't receive an email within a few minutes check your spam filter for messages from elvis himself<br/>\n";
// clear form
$_REQUEST['usernameOrEmail'] = '';
}
//
if (!$user) { $errorsAndAlerts .= "No matching username or email was found!<br/>\n"; }
}
}
?>
如果我只点击 Google "I am not a robot" 验证码,我会收到 "No email specified!" 警报
但如果我不检查验证码,只输入一个电子邮件地址,在这种情况下输入 emmalinnery@gmail.com,它就会通过。我想要它,所以当输入正确的电子邮件并且未检查验证码以显示警报时 "Please check the anti-spam 'I am not a robot' checkbox!"
如有任何意见,我将不胜感激。干杯
简单的解决方案,在顶部添加一个 $captcha 变量并添加此代码
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo "<script> alert('Please check the the captcha form.'); </script>";
}
我推荐使用这个教程,它对我帮助很大
https://codeforgeek.com/2014/12/google-recaptcha-tutorial/
希望对您有所帮助:)
我有一个密码请求页面,我正在使用 google 验证码,我的问题是如何让它们同时触发。
http://www.camteach.com/user-password-request.php
如果我在没有完成任何事情的情况下点击提交,我会同时收到 "Please check the anti-spam 'I am not a robot' checkbox!" 和 "No email specified!"
的警告<?php function validateGoogleCaptcha(){
$errorsAndAlerts = "";
if (!@$_REQUEST['g-recaptcha-response']) { $errorsAndAlerts .= "Please check the anti-spam 'I am not a robot' checkbox!<br/>\n"; }
else {
// check recaptcha
$postdata = array();
$postdata['secret'] = '6LcwKCwUAAAAAK5CXed1YJGfMk7iVBL5NgN2vPVd';
$postdata['response'] = @$_REQUEST['g-recaptcha-response'];
$postdata['remoteip'] = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?". http_build_query($postdata, '', '&');
list($json, $httpStatusCode, $headers, $request) = getPage($url, 5, '', true);
$recaptchaResponse = json_decode($json, true);
if (!$recaptchaResponse['success']) {
if (is_array($recaptchaResponse['error-codes'])) {
if (in_array('missing-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (no secret)<br/>\n"; }
if (in_array('invalid-input-secret', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "There's a problem with recaptcha, please let us know! (invald secret)<br/>\n"; }
if (in_array('missing-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please fill out the recaptcha box!<br/>\n"; }
if (in_array('invalid-input-response', $recaptchaResponse['error-codes'])) { $errorsAndAlerts .= "Please fill out the recaptcha box again, your answer was incorrect!<br/>\n"; }
}
if (!$errorsAndAlerts) { $errorsAndAlerts .= "Invalid captcha response, please try again or contact us directly and let us know."; }
@trigger_error("Failed recaptcha on signup form", E_USER_NOTICE);
}
}
return $errorsAndAlerts;
}
// error checking
$errorsAndAlerts = alert();
if (@$CURRENT_USER) {
$errorsAndAlerts = "You are already logged in! <a href='/'>Click here to continue</a> or <a href='?action=logoff'>Logoff</a>.";
}
?>
<?php // process form
if (@$_POST['action']):?>
<?php $errorsAndAlerts = "";
$errorsAndAlerts .= validateGoogleCaptcha();
?>
<?php endif ?>
<?php
### send reset email
if (@$_POST['action'] == 'sendPasswordReminder') {
global $SETTINGS, $TABLE_PREFIX;
// display errors
if (!@$_REQUEST['usernameOrEmail']) { $errorsAndAlerts .= "No email specified!<br/>\n"; }
// send emails
if (@$_REQUEST['usernameOrEmail']) {
$where = mysql_escapef("? IN (`username`,`email`)", $_REQUEST['usernameOrEmail']);
$user = mysql_get(accountsTable(), null, $where);
// send message
if ($user) {
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'USER-PASSWORD-RESET',
'placeholders' => array(
'user.username' => $user['username'],
'user.email' => $user['email'],
'loginUrl' => realUrl($GLOBALS['WEBSITE_LOGIN_LOGIN_FORM_URL']),
'resetUrl' => realUrl($GLOBALS['WEBSITE_LOGIN_RESET_URL'] . "?userNum={$user['num']}&resetCode=" . _generatePasswordResetCode( $user['num'] )),
)));
$mailErrors = sendMessage($emailHeaders);
if ($mailErrors) { alert("Mail Error: $mailErrors"); }
//
$errorsAndAlerts .= "Thanks, we've emailed you instructions on resetting your password.<br/><br/>
If you don't receive an email within a few minutes check your spam filter for messages from elvis himself<br/>\n";
// clear form
$_REQUEST['usernameOrEmail'] = '';
}
//
if (!$user) { $errorsAndAlerts .= "No matching username or email was found!<br/>\n"; }
}
} ?>
如果我只点击 Google "I am not a robot" 验证码,我会收到 "No email specified!" 警报
但如果我不检查验证码,只输入一个电子邮件地址,在这种情况下输入 emmalinnery@gmail.com,它就会通过。我想要它,所以当输入正确的电子邮件并且未检查验证码以显示警报时 "Please check the anti-spam 'I am not a robot' checkbox!"
如有任何意见,我将不胜感激。干杯
简单的解决方案,在顶部添加一个 $captcha 变量并添加此代码
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo "<script> alert('Please check the the captcha form.'); </script>";
}
我推荐使用这个教程,它对我帮助很大
https://codeforgeek.com/2014/12/google-recaptcha-tutorial/
希望对您有所帮助:)