在联系表单 PHP 文件上安装 Google reCaptcha 信息 [英] Install Info For Google reCaptcha on Contact Form PHP File

查看:76
本文介绍了在联系表单 PHP 文件上安装 Google reCaptcha 信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为联系人页面安装 Google reCaptcha,但我对 php 的了解非常有限.我不确定 Google 需要的信息应该放在我的 php 文件中的什么位置.以下是 Google 的说明:

<块引用>

当您的用户提交您集成了 reCAPTCHA 的表单时,您将获得一个名为g-recaptcha-response"的字符串作为有效负载的一部分.为了检查 Google 是否已验证该用户,请发送带有以下参数的 POST 请求:

网址:https://www.google.com/recaptcha/api/siteverify

秘密(必填) - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

response (required) - 'g-recaptcha-response' 的值.

remoteip - 最终用户的 IP 地址.

这是我使用的表单的 php.

<身体><p>请更正以下错误:</p><strong><?php echo $myError;?></strong><p>点击后退按钮并重试</p></html><?php出口();}?>

HTML 表单

<div class="col-md-6 message"><h2>给我们发送消息</h2><表单名称=联系表单"方法=发布"动作=index.php"类=形式垂直"><div class="form-group"><输入类型=文本"类=表单控制"id=输入名称"名称=输入名称"占位符=第一个和最后一个">

<div class="form-group"><label for="inputEmail";class="control-label">Email*</label><输入类型=文本"类=表单控制"id="inputEmail";名称=输入电子邮件"占位符=必需">

<div class="form-group"><label for="inputPhone";class="control-label">电话号码</label><输入类型=文本"类=表单控制"id=输入电话"名称=输入电话"占位符=可选">

<div class="form-group"><label for="inputMessage";class="control-label">Message<textarea class="form-control";行=5"id=输入消息"名称=输入消息"placeholder=简要说明"></textarea>

<div class="g-recaptcha";data-sitekey=此处的数据站点密钥"></div><div class="form-group"><按钮类型=提交"class="btn btn-custom pull-right hvr-underline-from-left">发送</button>

</表单>

<!-- 结束 col-md-6 -->

我真的不确定上述信息应该放在哪里.非常感谢任何帮助.

解决方案

google reCaptcha 机制在您的表单中注入了一个隐藏的 IFrame,并向您的处理脚本返回一个名为g-recaptcha-response"的哈希字符串.

所以,在你上面的 PHP 脚本中,在 /* Set e-mail receiver */ 之前添加以下内容:

<身体><p>请更正以下错误:</p><strong><?php echo $myError;?></strong><p>点击后退按钮并重试</p></html><?php出口();}?>

应该可以正常工作.在检查其他内容或向您发送任何电子邮件之前,该代码将检查 reCaptcha 是否已正确传递.

祝你好运.

I am trying to install the Google reCaptcha for a Contact page and I have really limited knowledge with php. I'm unsure as to where the information Google requires should go in my php file. Here are Google's instructions for that:

When your users submit the form where you integrated reCAPTCHA, you'll get as part of the payload a string with the name "g-recaptcha-response". In order to check whether Google has verified that user, send a POST request with these parameters:

URL: https://www.google.com/recaptcha/api/siteverify

secret (required) - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

response (required) - The value of 'g-recaptcha-response'.

remoteip - The end user's ip address.

And here is my php for the form I use.

<?php
$secret = 'SECRET KEY HERE';
$verificationResponse = $_POST["g-recaptcha-response"];

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$verificationResponse);
$response = json_decode($response, true);
if($response["success"] === true){
// actions if successful
}else{
// actions if failed
}

/* Set e-mail recipient */
$myemail = "info@thewiseinvestor.net";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = check_input($_POST['inputPhone']);
$message = check_input($_POST['inputMessage'], "Brief Description");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Contact Message from thewiseinvestor.net";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Phone: $phone

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

HTML Form

<div class="row">
    <div class="col-md-6 message">
    <h2>Send Us A Message</h2>
    <form name="contactform" method="post" action="index.php" class="form-vertical">
      <div class="form-group">
        <label for="inputName" class="control-label">Name</label>
          <input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last">
      </div>
      <div class="form-group">
        <label for="inputEmail" class="control-label">Email*</label>
          <input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required">
      </div>
      <div class="form-group">
        <label for="inputPhone" class="control-label">Phone Number</label>
          <input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional">
      </div>
      <div class="form-group">
        <label for="inputMessage" class="control-label">Message</label>
          <textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea>
      </div>
      <div class="g-recaptcha" data-sitekey="DATA SITE KEY HERE"></div>
      <div class="form-group">
        <button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button>
      </div>
    </form>
    </div> <!-- end col-md-6 --> 

I'm really unsure as to where the above information should go. Any assistance is much appreciated.

解决方案

The google reCaptcha mechanism injects a hidden IFrame within your form, and returns a hashed string to your processing script called 'g-recaptcha-response'.

So, in your above PHP script, before /* Set e-mail recipient */ please add the following:

<?php

// error_reporting(E_WARNING);

function readURL($url)
{
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch); 
    return $output;
}

$secret = "PASTE-YOUR-SECRET-KEY-HERE";
$verificationResponse = $_POST["g-recaptcha-response"];
if( empty($verificationResponse) ) die("Google did not POST the required g-recaptha-response");

$response = readURL("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $verificationResponse . "");

$responseArray = json_decode($response, true);
if( $responseArray["success"] !== true) die("Invalid reCaptcha <a href=\"javascript:history.go(-1);\">Try Again</a>");

/* Set e-mail recipient */
$myemail = "info@thewiseinvestor.net";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = check_input($_POST['inputPhone']);
$message = check_input($_POST['inputMessage'], "Brief Description");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Contact Message from thewiseinvestor.net";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Phone: $phone

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}

?>

Should work without any problems. The code will check if the reCaptcha was passed correctly before checking other things or sending you any emails.

Good luck.

这篇关于在联系表单 PHP 文件上安装 Google reCaptcha 信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆