在我的Web表单上使用reCAPTCHA和PHP在使用XAMPP测试时不起作用?
我只是尝试将reCAPTCHA插入我的Web表单,并使用XAMPP1.8.1对其进行测试。发生的情况如下: 1.reCAPTCHA在我的表单底部成功显示 2.我填写了表格,电子邮件成功转发到我的电子邮件地址 问题是reCAPTCHA字段不是强制性的,所以无论我是否输入所需的两个单词,我仍然会收到电子邮件。如果用户没有填写reCAPTCHA字段,那么这个reCAPTCHA字段不应该是强制性的,这样我就无法收到消息吗 不知道我做错了什么 下面是我的email.php代码(reCAPTCHA代码位于底部):在我的Web表单上使用reCAPTCHA和PHP在使用XAMPP测试时不起作用?,php,html,recaptcha,Php,Html,Recaptcha,我只是尝试将reCAPTCHA插入我的Web表单,并使用XAMPP1.8.1对其进行测试。发生的情况如下: 1.reCAPTCHA在我的表单底部成功显示 2.我填写了表格,电子邮件成功转发到我的电子邮件地址 问题是reCAPTCHA字段不是强制性的,所以无论我是否输入所需的两个单词,我仍然会收到电子邮件。如果用户没有填写reCAPTCHA字段,那么这个reCAPTCHA字段不应该是强制性的,这样我就无法收到消息吗 不知道我做错了什么 下面是我的email.php代码(reCAPTCHA代码位于底
您忘记在email.php中验证reCaptcha 您不能将
表单
放在表单
中。创建一个包含姓名输入、电子邮件输入、消息输入和reCaptcha(不含
)的表单。使用verify.php
内部email.php
中的代码,如下所示:
// load recaptcha library
require_once('recaptchalib.php');
// config - you can read it from some config file
$publickey = "my_public_key_goes_here"; // you got this from the signup page
$privatekey = "my_private_code_goes_here";
// rest of your code
// Is form sent?
if( isset( $_POST['submit'] ) ) {
// begin: reCAPTCHA CODE - validate answer
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$errors[] = 'Please enter a valid captcha';
}
// end: reCAPTCHA CODE - validate ansver
// Validate $_POST['name']
// rest of your code
}
<!-- rest of your HTML -->
<form method="POST"> <!-- you don't need `action` for the same page -->
<!-- rest of your form -->
<!-- begin: reCAPTCHA CODE - print widget -->
<?php echo recaptcha_get_html($publickey); ?></br>
<!-- end: reCAPTCHA CODE - print widget -->
<input name="submit" type="submit" value="Submit" />
</form>
//加载recaptcha库
require_once('recaptchalib.php');
//配置-您可以从一些配置文件中读取它
$publickey=“我的公钥在这里”//你从注册页面上看到的
$privatekey=“我的私人密码在这里”;
//代码的其余部分
//表格寄出了吗?
如果(isset($_POST['submit'])){
//开始:reCAPTCHA代码-验证答案
$resp=repatcha\u check\u response($privatekey,
$\u服务器[“远程地址”],
$\u POST[“重演挑战场”],
$(POST[“recaptcha_response_field”]);
如果(!$resp->有效){
$errors[]=“请输入有效的验证码”;
}
//结束:reCAPTCHA代码-验证ANVER
//验证$\u POST['name']
//代码的其余部分
}
编辑:
最简单的工作示例:
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key";
$privatekey = "your_private_key";
if( isset( $_POST["recaptcha_response_field"] ) )
{
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
echo "reCaptcha incorrect";
} else {
echo "reCaptcha OK";
}
}
?>
<form method="POST">
<? echo recaptcha_get_html($publickey); ?>
</form>
编辑:
您的代码与可工作的recaptcha(也)
email.php
<?php
require_once 'PHPMailer/class.phpmailer.php';
// load recaptcha library
require_once('recaptchalib.php');
// config - you could read it from some config file
$publickey = "your_public_key";
$privatekey = "your_private_key";
// Form url sanitizing
$php_self = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// Variable initializing
$name = '';
$email = '';
$message = '';
$errors = array();
// Is form sent?
if( isset( $_POST['submit'] ) ) {
// begin: reCAPTCHA - VALIDATE
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$errors[] = 'Please enter a valid captcha';
}
// end: reCAPTCHA - VALIDATE
// Validate $_POST['name']
$name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
if( '' == $name ) {
$errors[] = 'Please enter a valid name';
}
// Validate $_POST['email']
$email = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL );
if( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
$errors[] = 'Please enter a valid email';
}
// Validate $_POST['message']
$message = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
if( '' == $message ) {
$errors[] = 'Please enter a valid message';
}
// If no errors
if( empty( $errors ) ) {
// Values are valid, lets send an email
//echo "I'm send mail (virtually) ;)"; // debug
$mail = new PHPMailer();
// Base parameters that are working for me
$mail->IsSMTP(); // Use SMTP
$mail->Host = "smtp.gmail.com"; // GMail
$mail->Port = 587; // If not working, you can try 465
$mail->SMTPSecure = "tls"; // If not working, you can try "ssl"
$mail->SMTPAuth = true; // Turn on SMTP authentication
// Adjust these lines
$mail->Username = "myemailaddress@gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom($email, $name);
$mail->AddAddress('myotheremailaddress@gmail.com', 'MyName'); // This is the email address (inbox) to which the message from a webform will be sent
$mail->Subject = "Web Form Message"; // This will be the subject of the message(s) you receive through the webform
$mail->Body = $message;
// Sending
if(!$mail->Send()) {
// First error message is just for debugging. This don't generate messages a user should read
// Comment this and uncomment the second message for a more user friendly message
$errors[] = "Mailer Error: " . $mail->ErrorInfo;
//$errors[] = "email couldn't be send";
// Output Sanitizing for repopulating form
$name = filter_var( $name, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$email = filter_var( $email, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
} else {
// Generating a success message is good idea
echo "<p>Thank you <strong>$name</strong>, your message has been successfully submitted.</p>";
// Clear fields
$name = '';
$email = '';
$message = '';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>self referencing form</title>
<link rel='stylesheet' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="button" class="title">
<h6>Contact</h6>
</div>
<div id="dropbox">
<header class="title">
<h6>Whats up?</h6>
</header>
<?php if(!empty($errors)): ?>
<ul class="error">
<li><?php echo join('</li><li>', $errors); ?></li>
</ul>
<?php endif; ?>
<div class="contact-form">
<form method="POST">
<!-- input element for the name -->
<h6><img src="img/person.png" alt=""> Name</h6>
<input type="text"
name="name"
value="<?php echo $name; ?>"
placeholder="Please enter your full name here"
required>
<!-- input element for the email -->
<h6><img src="img/email.png" alt=""> E-mail</h6>
<input type="email"
name="email"
value="<?php echo $email; ?>"
placeholder="Please enter your e-mail address"
required>
<!-- input element for the message -->
<h6><img src="img/message.png" alt=""> Message</h6>
<textarea name="message" placeholder="Type your message..." required><?php echo $message; ?></textarea>
<!-- begin: reCAPTCHA - RENDERING-->
<?php echo recaptcha_get_html($publickey); ?></br>
<!-- end: reCAPTCHA - RENDERING-->
<input name="submit" type="submit" value="Submit" />
</form>
</div>
</div>
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script src='dropbox.js'></script>
<?php if( !empty($errors) ): ?>
<script>
$(function() {
$('#dropbox').show();
});
</script>
<?php endif ?>
</body>
</html>
注释不是编写代码的好地方。最好编辑问题以添加一些代码。我用你们的代码做一个最简单的工作示例-。请参阅上面我的答案中的代码。如果有些东西不起作用,请在许多地方使用echo
和print\r
,查看PHP正在做什么(一步一步)。例如:您可以在if(empty($errors))
之前添加print\r($errors)
$errors
决定发送邮件。顺便说一下:您应该显示邮件“mail was send”,甚至向客户端发送邮件-一些客户端希望这样。客户可能会认为由于并没有信息,所以出现了问题,他可能会反复使用联系方式。若有人使用假邮件,你们会立即收到来自服务器的消息“我不能发送邮件”。有人可以使用其他人的邮件。您的邮件将问题告知了那个人-您必须添加取消订阅的机会。它是UX(用户体验)的一部分。最后-在PHP标题(“刷新:5;url=index.html”)中使用错误颜色(大部分为红色)和消息颜色(绿色)代码>或HTML标记
内部
标记。使用JavaScript,您甚至可以倒计时-请参阅:
// load recaptcha library
require_once('recaptchalib.php');
// config - you can read it from some config file
$publickey = "my_public_key_goes_here"; // you got this from the signup page
$privatekey = "my_private_code_goes_here";
// rest of your code
// Is form sent?
if( isset( $_POST['submit'] ) ) {
// begin: reCAPTCHA CODE - validate answer
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$errors[] = 'Please enter a valid captcha';
}
// end: reCAPTCHA CODE - validate ansver
// Validate $_POST['name']
// rest of your code
}
<!-- rest of your HTML -->
<form method="POST"> <!-- you don't need `action` for the same page -->
<!-- rest of your form -->
<!-- begin: reCAPTCHA CODE - print widget -->
<?php echo recaptcha_get_html($publickey); ?></br>
<!-- end: reCAPTCHA CODE - print widget -->
<input name="submit" type="submit" value="Submit" />
</form>
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key";
$privatekey = "your_private_key";
if( isset( $_POST["recaptcha_response_field"] ) )
{
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
echo "reCaptcha incorrect";
} else {
echo "reCaptcha OK";
}
}
?>
<form method="POST">
<? echo recaptcha_get_html($publickey); ?>
</form>
<?php
require_once 'PHPMailer/class.phpmailer.php';
// load recaptcha library
require_once('recaptchalib.php');
// config - you could read it from some config file
$publickey = "your_public_key";
$privatekey = "your_private_key";
// Form url sanitizing
$php_self = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// Variable initializing
$name = '';
$email = '';
$message = '';
$errors = array();
// Is form sent?
if( isset( $_POST['submit'] ) ) {
// begin: reCAPTCHA - VALIDATE
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
$errors[] = 'Please enter a valid captcha';
}
// end: reCAPTCHA - VALIDATE
// Validate $_POST['name']
$name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
if( '' == $name ) {
$errors[] = 'Please enter a valid name';
}
// Validate $_POST['email']
$email = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL );
if( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
$errors[] = 'Please enter a valid email';
}
// Validate $_POST['message']
$message = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
if( '' == $message ) {
$errors[] = 'Please enter a valid message';
}
// If no errors
if( empty( $errors ) ) {
// Values are valid, lets send an email
//echo "I'm send mail (virtually) ;)"; // debug
$mail = new PHPMailer();
// Base parameters that are working for me
$mail->IsSMTP(); // Use SMTP
$mail->Host = "smtp.gmail.com"; // GMail
$mail->Port = 587; // If not working, you can try 465
$mail->SMTPSecure = "tls"; // If not working, you can try "ssl"
$mail->SMTPAuth = true; // Turn on SMTP authentication
// Adjust these lines
$mail->Username = "myemailaddress@gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom($email, $name);
$mail->AddAddress('myotheremailaddress@gmail.com', 'MyName'); // This is the email address (inbox) to which the message from a webform will be sent
$mail->Subject = "Web Form Message"; // This will be the subject of the message(s) you receive through the webform
$mail->Body = $message;
// Sending
if(!$mail->Send()) {
// First error message is just for debugging. This don't generate messages a user should read
// Comment this and uncomment the second message for a more user friendly message
$errors[] = "Mailer Error: " . $mail->ErrorInfo;
//$errors[] = "email couldn't be send";
// Output Sanitizing for repopulating form
$name = filter_var( $name, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$email = filter_var( $email, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
} else {
// Generating a success message is good idea
echo "<p>Thank you <strong>$name</strong>, your message has been successfully submitted.</p>";
// Clear fields
$name = '';
$email = '';
$message = '';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>self referencing form</title>
<link rel='stylesheet' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="button" class="title">
<h6>Contact</h6>
</div>
<div id="dropbox">
<header class="title">
<h6>Whats up?</h6>
</header>
<?php if(!empty($errors)): ?>
<ul class="error">
<li><?php echo join('</li><li>', $errors); ?></li>
</ul>
<?php endif; ?>
<div class="contact-form">
<form method="POST">
<!-- input element for the name -->
<h6><img src="img/person.png" alt=""> Name</h6>
<input type="text"
name="name"
value="<?php echo $name; ?>"
placeholder="Please enter your full name here"
required>
<!-- input element for the email -->
<h6><img src="img/email.png" alt=""> E-mail</h6>
<input type="email"
name="email"
value="<?php echo $email; ?>"
placeholder="Please enter your e-mail address"
required>
<!-- input element for the message -->
<h6><img src="img/message.png" alt=""> Message</h6>
<textarea name="message" placeholder="Type your message..." required><?php echo $message; ?></textarea>
<!-- begin: reCAPTCHA - RENDERING-->
<?php echo recaptcha_get_html($publickey); ?></br>
<!-- end: reCAPTCHA - RENDERING-->
<input name="submit" type="submit" value="Submit" />
</form>
</div>
</div>
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script src='dropbox.js'></script>
<?php if( !empty($errors) ): ?>
<script>
$(function() {
$('#dropbox').show();
});
</script>
<?php endif ?>
</body>
</html>