我真的很感激在我的网站上实现reCAPTCHA的一些帮助。我通过修改我下载的模板创建了一个简单的单页面站点。问题是我的收件箱被垃圾邮件侵袭了。有人告诉我reCAPTCHA的事,我想用它。
我在网上关注过多个例子,在这里搜索过,也关注过谷歌文档。到目前为止,我已经遇到了各种各样的错误,并设法解决了每个错误。
我有自己的私钥和密钥,并且我已经注册了我的实际domain.com和本地主机。
我使用的最后一个教程是这样的;
https://bootstrapious.com/p/bootstrap-recaptcha
我使用以下命令将google API库下载到我的项目的根目录中;
git clone -b v1-master https://github.com/google/google-api-php-client.git.
现在,我收到以下错误。
Fatal error: Class 'ReCaptcha\ReCaptcha' not found in /Users/myName/Desktop/project/contactform/contactform.php on line 30.
我已经把我的项目上传到了gitHub,希望有人能帮我看看。我已经从站点中删除了所有敏感信息,但它仍将在您的本地主机上运行。
任何帮助或建议都将不胜感激。花了几个小时看着它,尝试着不同的东西。
https://github.com/Peakapot/reCAPTCHA-Help
谢谢
发布于 2017-11-22 23:55:12
在我这里尝试几天后,Google reCAPTCHA可能会对不同的解决方案感到相当困惑。以下是对我有效的方法:
<?php
if(isset($_POST['<data here>'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$secretkey = "<secret key here>";
$response = file_get_contents($url."?secret=".$secretkey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success == true) {
// enter any code here
} else {
// False - display error
header('Location: /page.php?CaptchaFail=True'); // make sure it's on the same page
}
}
?>
确保上面的代码片段位于上面包含reCAPTCHA的页面的顶部。在HTML文档中:
<?php if(isset($_GET['CaptchaFail'])){ ?>
<div>Captcha Failed. Please try again!</div>
<?php } ?>
<div class="g-recaptcha" data-sitekey="<site key here>"></div>
上面我使用的代码片段来自YouTube视频,实际上对我很有效-- https://www.youtube.com/watch?v=XjN0j4JQqVI&t=951s
发布于 2017-11-26 03:34:32
好的,在尝试了许多不同的方法之后,我终于让它工作了。
我找到了答案here。
我已经包含了一份为我工作的代码副本。
<?php
// require ReCaptcha class
require('../google-api-php-client/src/Google/autoload.php');
//GOOGLE RECAPTCH CODE
require_once('../recaptcha-master/src/ReCaptcha/ReCaptcha.php');
require_once('../recaptcha-master/src/ReCaptcha/RequestMethod.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestParameters.php');
require_once('../recaptcha-master/src/ReCaptcha/Response.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/Post.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/Socket.php');
require_once('../recaptcha-
master/src/ReCaptcha/RequestMethod/SocketPost.php');
$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$secret = 'MY_KEY';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
//DO ACTION IF SUCCESSFUL
if (isset($_POST['name']) && isset($_POST['email']) &&
isset($_POST['subject']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$outputMessage = $message ."\r\n\r\n" . $name;
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail("mail@mail.com", $subject, $outputMessage, $headers);
}
} else {
//$errors = $resp->getErrorCodes();
echo "It appears you may be a robot. Please try again.";
}
我意识到单独包含所有这些文件并不是一个好的做法,但这是一个我现在可以接受的变通办法。
希望这对某些人有帮助。
Peakapot
https://stackoverflow.com/questions/47438452
复制相似问题