我是PHP的新手,我正在尝试在PHP邮件表单中添加reCaptcha。
代码类似于:
PHP:
<?php if (isset($_POST['send'])) {
$to = "mymail@gmail.com";
$subject = "subject";
$message = 'Name: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'IP: ' . $_SERVER['REMOTE_ADDR'] . "\n";
$message .= 'Message: ' . $_POST['message'];
$success = mail($to, $subject, $message); }?>`
HTML:
<form method="post">
<p>name</p>
<input type="text" name="name" class="contactformimput"/>
<p>email*</p>
<input type="text" name="email" class="contactformimput"/>
<p>number</p>
<input type="text" name="number" class="contactformimput"/>
<p>Messaggio*</p>
<input type="text" name="message" onkeyup="adjust_textarea(this)" class="contactformimput" id="contactformtext">
<div class="g-recaptcha" data-sitekey="__MYPUBBLICKEY__"></div>
<div id="divcontactbutton">
<input type="reset" name="send" value="Resetta" class="button" id="resetmessage"/>
<input type="submit" name="send" value="Invia Messaggio" class="button" id="sendmessage"/>
</div>
</form>
答案 0 :(得分:0)
使用Google的recaptcha易于使用, 首先,您需要在Google recaptcha上注册您的网站GoogleRECAPTCHA
提交后,Google会向您提供以下两条信息。
将其集成到head标签内的网站中使用此代码
<script src='https://www.google.com/recaptcha/api.js'></script>
要在表单中显示窗口小部件,请在表单中使用此代码
<div class="g-recaptcha" data-sitekey="SITE-KEY"></div>
当表单提交到服务器时,此脚本将发送g-recaptcha-response
作为POST数据。您需要验证它以查看用户是否已检查过Captcha
用于检查的PHP代码
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Check captcha .</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
//redirect back
}else
{
//your mail code
}