将ReCAPTCHA与联系表格正确整合

时间:2016-06-09 17:01:57

标签: php forms recaptcha contact

我之前在这里搜索过一个解决方案,并没有找到任何可以帮助我解决问题的方法,所以我决定寻求帮助。

我有一个联系表单并尝试集成reCAPTCHA但不起作用。

我当前的代码HTML:

<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
        <form id="contact-form" method="post" action="contact.php" action="javascript:alert('success!');">
            <label>Name:</label><input class="form-control" type="text" name="name" value="">

            <label>Email</label><input class="form-control" type="text" name="email" ><br />

            <label>Subject:</label>
                <select name="subject" id="subject" class="select">
                    <option value="">Please Choose a Subject:</option>
                    <option value="sales">Sales/option>
                    <option value="support">Support/option>
                    <option value="other">Others/option>
                </select><br />

                <div align="center">
               <div class="g-recaptcha" data-sitekey="#MYKEY#"></div>
               </div>
                <br />

            <label id="load"></label><button type="submit" name="submit" class="btn btn-primary"> Send Now</button>
            <button type="reset" class="btn btn-danger">Reset</button>
        </form>

这是我的contact.php的PHP代码:

<? php
    $your_secret = "MySecretKey";
    $client_captcha_response = $_POST['g-recaptcha-response'];
    $user_ip = $_SERVER['REMOTE_ADDR'];

    $captcha_verify = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$your_secret&response=$client_captcha_response&remoteip=$user_ip");
    $captcha_verify_decoded = json_decode($captcha_verify);
    if(!$captcha_verify_decoded->success)
      die('DIRTY ROBOT');

error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
    {
    function ValidateEmail($email)
    {

$regex = "([a-z0-9_\.\-]+)". # name

"@". # at

"([a-z0-9\.\-]+){2,255}". # domain & possibly subdomains

"\.". # period

"([a-z]+){2,10}"; # domain extension 

$eregi = eregi_replace($regex, '', $email);

return empty($eregi) ? true : false;
}

$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$to=$toemail.','.$replyto;
$error = '';
$headers="";
$headers.="Reply-to:$replyto\n";
$headers .= "From: $email\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers;

// Checks Name Field

if(!$name)
{
$error .=  Please enter your name.<br />';
}

if(!$email)
{
$error .= ' Please enter your Email.<br />';
}

// This is for Check if Mail it's Valid or Fake

if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.<br />';
}

// Checks Subject Field
if(!$subject)
{
$error .= ' Please Select a Subject.<br />';
}
if(!$error)

    {

$messages="From: $email <br>";
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Subject: $subject <br>";

    $mail = mail($to,$subject,$messages,$headers);  

if($mail)
    {
    echo 'OK';
if($autorespond == "yes")
{
    include("autoreply.php");
}
    }

    }
    else
    {
    echo '<div class="error">'.$error.'</div>';
    }

}
?>

可以帮我解决这个问题吗?提前谢谢。

0 个答案:

没有答案