reCAPTCHA 未验证时注册表单提交
Register form submitting when reCAPTCHA isn't validated
我将 reCAPTCHA 添加到我的注册表中,当我测试该页面时,我注意到我仍然可以注册一个新帐户而无需使用 reCAPTCHA 进行验证。当我将其中一个文本框留空时,我会收到 "Please verify that you are Human." 此消息应该在用户填写表格并忘记验证时出现。我尝试了 stack overflow 和其他网站提供的许多不同的 if 语句,但问题仍未解决。我还尝试用 "method" 和 "action" 来修改我的表单。
这是我的注册表
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Register for Redchan</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<a href="index.php" title="Home"><img alt="4chan" src="/image/pepe-popcorn.gif" width="300" height="120"></a>
<div class="header">
<h2>Register for Redchan</h2>
</div>
<?php
//Check for form submission
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
//Get form submission data
$username = $_POST['username'];
$email = $_POST['email'];
//Set verification information for the Google reCAPTCHA API
$secretKey = "----------------------------------";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
//Call Google reCAPTCHA API
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
//Decode API response and generate response
$verification = json_decode($response, true);
if ($verification['success'])
echo "<p>Verification success!</p>";
else
echo "<p>Please verify that you are a Human.</p>";
}
?>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="g-recaptcha" data-sitekey="-------------------------------"></div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
您需要将验证码字段本身设置为必需,默认情况下不是。
您可以尝试这样的操作:
<script type="text/javascript">
window.onload = function() {
var $recaptcha = document.querySelector('#g-recaptcha-response');
if ($recaptcha) {
$recaptcha.setAttribute("required", "required");
}
};
</script>
我将 reCAPTCHA 添加到我的注册表中,当我测试该页面时,我注意到我仍然可以注册一个新帐户而无需使用 reCAPTCHA 进行验证。当我将其中一个文本框留空时,我会收到 "Please verify that you are Human." 此消息应该在用户填写表格并忘记验证时出现。我尝试了 stack overflow 和其他网站提供的许多不同的 if 语句,但问题仍未解决。我还尝试用 "method" 和 "action" 来修改我的表单。
这是我的注册表
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Register for Redchan</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<a href="index.php" title="Home"><img alt="4chan" src="/image/pepe-popcorn.gif" width="300" height="120"></a>
<div class="header">
<h2>Register for Redchan</h2>
</div>
<?php
//Check for form submission
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
//Get form submission data
$username = $_POST['username'];
$email = $_POST['email'];
//Set verification information for the Google reCAPTCHA API
$secretKey = "----------------------------------";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
//Call Google reCAPTCHA API
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
//Decode API response and generate response
$verification = json_decode($response, true);
if ($verification['success'])
echo "<p>Verification success!</p>";
else
echo "<p>Please verify that you are a Human.</p>";
}
?>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="g-recaptcha" data-sitekey="-------------------------------"></div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
您需要将验证码字段本身设置为必需,默认情况下不是。
您可以尝试这样的操作:
<script type="text/javascript">
window.onload = function() {
var $recaptcha = document.querySelector('#g-recaptcha-response');
if ($recaptcha) {
$recaptcha.setAttribute("required", "required");
}
};
</script>