我正在尝试让reCaptcha为我的表单工作。如果没有reCaptcha,表单本身可以完美运行。以下是我的.html和.php文件中的相关代码。
问题是reCAPTCHA小部件根本没有显示在页面上。我可以让提交按钮工作但当然它总是因为缺少小部件而失败。
sign_up.html
<div id="form">
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'white'
};
</script>
<form class="pure-form" action="myprocessingscript.php" method="post">
<fieldset class="pure-group">
<input name="field1" type="text" class="pure-input-1-2" placeholder="Name">
<input name="field2" type="text" class="pure-input-1-2" placeholder="Username">
<input name="field3" type="email" class="pure-input-1-2" placeholder="Email">
</fieldset>
<?php
require_once('recaptchalib.php');
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(censored)";
echo recaptcha_get_html($publickey);
?>
<button name="submit" type="submit" class="pure-button pure-input-1-2 pure-button-primary">Skicka</button>
</form>
</div>
myprocessingscript.php
<?php
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx(censored)";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
if(isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . '-' . $_POST['field3'] . "\n";
$ret = file_put_contents('submissions.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
}
?>
感谢您的帮助!
答案 0 :(得分:0)
1.将sign_up.html
页保存为sign_up.php
并删除代码,首先使其无需自定义。
2.您是否已将recaptchalib.php
文件上传到根目录。
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'white'
};
</script>