gpt4 book ai didi

php - 挣扎于 recaptcha v2 和表单提交

转载 作者:IT王子 更新时间:2023-10-28 23:57:30 25 4
gpt4 key购买 nike

https://developers.google.com/recaptcha/docs/verify

if(isset($_POST['submit'])){
$recaptchaResponse = $_POST['g-recaptcha-response'];
$secretKey = 'MYKEY';
$request = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$recaptchaResponse);

if(!strstr($request,"false")){
echo '<div class="notification error clearfix"><p><strong>Attention!</strong> You didnt complete the captcha.</p></div>';
exit();

然后 php 文件的其余部分会邮寄表单,但即使您没有完成 recaptcha,它也会发送。基本上,如果 JSON 返回错误,我希望它不会发送并显示错误

如果有帮助的话,这里还有页面中的表格,我可能也在那里做错了......

<form method="POST" action="post.php" name="contactform" id="contactform" class="container">

<fieldset>
<div class="form-field grid-half">
<label for="name">Name</label>
<span><input type="text" name="name" id="name" /></span>
</div>
<div class="form-field grid-half">
<label for="email">Email</label>
<span><input type="email" name="email" id="email" /></span>
</div>
<div class="form-field grid-full">
<label for="message">Message</label>
<span><textarea name="message" id="message"></textarea></span>
</div>
<div class="form-field grid-full">
<div class="g-recaptcha" data-sitekey="MYKEY"></div>
</div>
</fieldset>
<div class="form-click grid-full">
<span><input type="submit" name="submit" value="Submit" id="submit" /></span>
</div>

<div id="alert" class="grid-full"></div>
</form>

最佳答案

使用 curl 而不是 file_get_contents(如果您像我一样希望在服务器设置中禁用 file_get_contents)

$post_data = "secret=__your_secret_key__&response=".
$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR'] ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Content-Length: ' . strlen($post_data)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$googresp = curl_exec($ch);
$decgoogresp = json_decode($googresp);
curl_close($ch);

if ($decgoogresp->success == true)
{
// Success
}

关于php - 挣扎于 recaptcha v2 和表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27264459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com