将cha重新捕获到自定义PHP表单中


Recaptcha into custom PHP form

我正试图将recaptcha添加到我的自定义PHP表单中,但我对如何添加感到困惑。recaptcha需要将recaptchalib.php添加到表单中,但是如果我添加verify.php,那么我的表单将不会处理,因为我正在使用我的PHP文件来处理我的表单。

<form method="POST" action="process.php" id="form-ok">

文档有点混乱。我的问题是,我需要做什么来处理两个动作?

知道我该怎么做吗?

Process.php

<?php
$redirectTo = '/thankyou.html';
$subject = 'New message from site'; // Email SUBJECT field
$receive = array(
    'example@example.com'
 );
 if($_POST['email_check'] == '') {
    if (isset($_POST['first_name'])){
    $message = '<table width="100%" border="0" cellspacing="0" cellpadding="8" style="border:1px solid #f3f3f3">
                <tr>
                    <td colspan="3" height="30" style="font-size:20px"><strong>' . $subject . '</strong></td>
                </tr>
                <tr>
                    <td width="100" bgcolor="#f3f3f3"><strong>First Name: </strong></td>
                    <td width="14" height="30" bgcolor="#f3f3f3">&nbsp;</td>
                    <td width="305" bgcolor="#f3f3f3">' . $_POST ['first_name'] . '</td>
                </tr>
                <tr>
                    <td><strong>Last Name: </strong></td>
                     <td width="14" height="30">&nbsp;</td>
                    <td>' . $_POST ['last_name'] . '</td>
                </tr>
                <tr>
                    <td bgcolor="#f3f3f3"><strong>Email: </strong></td>
                     <td bgcolor="#f3f3f3" width="14" height="30">&nbsp;</td>
                    <td bgcolor="#f3f3f3">' . $_POST ['email'] . '</td>
                </tr>
                <tr>
                    <td><strong>Phone Number: </strong></td>
                     <td width="14" height="30">&nbsp;</td>
                    <td>' . $_POST ['phone'] . '</td>
                </tr>
                <tr>
                    <td bgcolor="#f3f3f3"><strong>Check: </strong></td>
                     <td bgcolor="#f3f3f3" width="14" height="30">&nbsp;</td>
                    <td bgcolor="#f3f3f3">';
                    foreach($_POST['role'] as $value)
                    {
                      $message.=$value.'<br>';
                    }
                    $message.='</td>
                </tr>
                <tr>
                    <td><strong>Message: </strong></td>
                     <td width="14" height="30">&nbsp;</td>
                    <td>' . $_POST ['message'] . '</td>
                </tr>
                <tr>
                    <td><strong>Referer:</strong></td>
                     <td width="14" height="30">&nbsp;</td>
                    <td>' . $_SERVER ['HTTP_REFERER'] . '</td>
                </tr>
                <tr>
                </table>';
        for ($i = 0; $i < count($receive); $i++){
        $to = $receive[$i];
        $headers  = 'MIME-Version: 1.0' . "'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'n";
        $headers .=  'From: ' . $_POST['email'] . "'n" . 'Reply-To: ' . $_POST['email'] . "'n";
        mail($to, $subject, $message,$headers);
        }
        header('Location: '.$redirectTo);
    }
}
else{
header('Location:'.$_SERVER['HTTP_REFERER']); die();
}
?>

在目录中添加您的repatchalib.php

您的流程.PHP:

require_once "../recaptchalib.php"; // where you store recaptchalib.php
$secret = "6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"; //your secret key
$resp = null;
$error = null;
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
    $resp = $reCaptcha->verifyResponse($_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]);
    }
if (isset($_POST['cmdlogin'])){
    if ($resp != null && $resp->success) {
        echo "<script>alert('Success Verifying Recaptcha!');</script>";
        echo "<meta http-equiv='refresh' content='0; url=login.php'>";
        exit();
        }
<form method="post" action="process.php">
.....other codes---
    <div class="g-recaptcha" data-
sitekey="6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx">
    </div>
.....other codes---
</form>

完整教程,请查看此处:https://github.com/google/ReCAPTCHA/tree/master/php

客户端(如何显示CAPTCHA图像)

 <form method="post" action="process.php">
   <?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><br>
<!-- more of your HTML content -->

服务器端以下代码应放在process.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
     }
?>