【发布时间】:2015-02-25 11:39:40
【问题描述】:
我是 PHP、jQuery 和 AJAX 的新手。
我正在尝试实施新的 Google Recapcha。
看起来像-
点击后-
并且经过验证-
而index.php的代码是-
<html>
<head>
<title>Google recapcha demo - Codeforgeek</title>
</head>
<body>
<h1>Google reCAPTHA Demo</h1>
<form id="comment_form" action="received.php" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<!-- ---------------------------------------Capcha Start------------------------------------- -->
<script src='https://www.google.com/recaptcha/api.js'></script>
<?php $siteKey="6LdLqv8SAAAAADT3YEjm6ONCwnPD95frMSZ92Ftv" ?>
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
<!-- ----------------------------------------Capcha End------------------------------------ -->
<br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
</form>
</body>
</html>
以及收到的部分——received.php
<?php
//////////////////////////////////////////Check Capch Function Start
function CapchaCheck()
{
$captcha;
if(isset($_POST['g-recaptcha-response']))
{
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha)
{
return false;
}
$secreatKey="6LdLqv8SAAAAAIWxKcn2zIKjWau2Mdz6yzE3Kkcm";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secreatKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
var_dump($response);
if($response.success==false)
{
return false;
}
else
{
return true;
}
}
//////////////////////////////////////////Check ReCaptcha Function End
if(CapchaCheck())
{
echo '<h2>Thanks for posting comment.</h2>';
}
else
{
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}
?>
效果很好。
但我不想在提交后检查 ReCapcha 是否正确。如果 ReCaptcha 错误,我想阻止用户提交。
所以,我想我需要 jQuery。
但我不知道如何实现。
谁能帮帮我。
提前感谢您的帮助。
【问题讨论】:
-
这里有一个答案:stackoverflow.com/a/28044629/2022751 这个问题是在你的问题之后 :)
-
您可能希望从示例代码中删除“secreatKey”,因为这对黑客来说很方便...
标签: php jquery google-api recaptcha