[英]I am confused about the php involving reCAPTCHA
我正在嘗試制作一個包含reCAPTCHA的非常基本的網站。 我已經獲取了我的站點密鑰和秘密密鑰,並且到目前為止還沒有遵循2個教程
該站點的目標是使用表單從用戶那里獲得一個數字作為輸入,並在reCAPTCHA成功並且按下了提交按鈕后顯示一個字符串。
到目前為止,這是我的代碼
<!DOCTYPE HTML>
<html> <!-- template-->
<head>
<title>template</title>
<script src="lib/jquery-2.1.4.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form action="/verify.php" method="get">
Number:<br>
<input type="text" name="firstname"><br>
<div class="g-recaptcha" data-sitekey="6LcKeGwUAAAAAOdDqu2CzJxZdgYUXEUEPQKZBOtn"></div>
<input type="submit" value="Submit" />
</form>
</body>
</html>
這是我的PHP
<html>
<body>
The number is <?php echo $_GET["number"]; ?><br>
<?php
if ($_GET["number"] == 42)
echo "42 is the right answer!";
?>
</body>
</html>
截至目前,該網站工作正常……除了我不知道如何添加reCAPTCHA代碼,而googles文檔使我感到困惑,因為我對php知之甚少。
非常感謝任何代碼示例或指向簡單文檔的鏈接。 這是我關於stackoverflow的第一篇文章...我希望我能遵守足夠的規則
這將是您的verify.php
$post_data = http_build_query(
array(
'secret' => CAPTCHA_SECRET, //as provided from google
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($opts);
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$result = json_decode($response);
if ($result->success) {
//success,
echo $_GET["firstname"]; //your input field name was 'firstname' not 'number'
}else{
echo 'we think you are a bot';
//fail
}
我建議將您的HTML代碼更改為此:
<form method="post" action="verify.php">
Number:<br>
<input type="text" name="number"><br>
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
<!-- more of your HTML content -->
</body>
並在verify.php中添加以下內容:
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
if (isset($_POST['number']) && $_POST['number'] == '42') {
echo "42 is the right answer!";
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.