使用Google ReCaptcha

时间:2015-03-22 21:35:53

标签: recaptcha

我热衷于使用Google ReCaptcha。我使用公钥在页面上获取了验证码,但不知道如何在我的表单处理器文档中使用私钥:

    <?php

//SMTP SETTINGS
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.webhost.co.nz'; // Specify main and backup server
$mail->SMTPAuth = true;  // Enable SMTP authentication
$mail->Username = ‘xxxxx@xxxxxxxx.co.nz';  // SMTP username
$mail->Password = ‘xxxxx@@xxxxxx’;  // SMTP password
$mail->SMTPSecure = 'ssl';   // Enable encryption, 'ssl' also accepted
$mail->Port = 465;
$mail->isHTML(true);  // Set email format to HTML
//SMTP SETTINGS

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
print_r($_POST);

$mailheader = "From: $email";
$to = "tony@finelinecreative.co.nz";  // Here is email send to
$subject = "Finelinecreative Enquiry";
$message = "Name: $name<br/>Email: $email<br/>Message: $message";

// Send the mail
$mail->From=$mail->Username;
$mail->FromName = 'finelinecreative';
$mail->addAddress($to);  
$mail->addReplyTo($email, $email);
$mail->Subject = $subject;
$mail->Body    = $message;
$mail->IsHTML(true);
$result = $mail->send();

header('location: http://www.finelinecreative.co.nz/index.php/thanks');

?>

想法好吗?

1 个答案:

答案 0 :(得分:0)

https://codeforgeek.com/2014/12/google-recaptcha-tutorial/有一个很好的教程可以很好地解释它。

实质上,您正在检查$ _POST变量'g-recaptcha-response'是否存在(通过在表单上包含recaptcha,与表单上的其他值一起发送)。如果是,则发送file_get_contents调用(发送密钥,g-recaptcha-response POST值和用户的IP地址)。您解码它的结果(作为JSON发送,您可能希望将其作为键值对访问),并查明查询是否成功。

这是其实施的相关部分。

if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
    echo '<h2>Please check the the captcha form.</h2>';
    exit;
}
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
    echo '<h2>You are spammer ! Get the @$%K out</h2>';
} else {
    // Send the email. In your case, you can wrap pretty all of your preprocessor in this.
}