将reCaptcha集成到现有的contact.php中

乔·弗莱厄蒂

我无法将新的Google reCaptcha集成到我现有的处理php脚本的表单中。以前,它在contact.html页面将表单重定向到contact.php电子邮件处理程序时绝对可以正常工作,但是我一直收到大量垃圾邮件,因此希望使用reCaptcha。

我使用一个单独的php文件来处理电子邮件。相关的contact.html代码如下:

<form id="contact-form" method="post" action="contact.php" role="form">

<div class="messages"></div>
<div class="controls">

    <div class="row">
        <div class="col-md-7">
            <div class="form-group">
                <label for="form_name">Name *</label>
                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your name *" required="required" data-error="Name is required">
                <div class="help-block with-errors"></div>
            </div>
             <div class="form-group">
                <label for="form_email">Email *</label>
                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email address *" required="required" data-error="A valid email is required">
                <div class="help-block with-errors"></div>
        </div>
         <div class="form-group">
                <label for="form_phone">Telephone</label>
                <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter a contact telephone number (optional)">
                <div class="help-block with-errors"></div>
            </div>
         <div class="form-group">
                <label for="form_message">Message *</label>
                <textarea id="form_message" name="message" class="form-control" placeholder="Please enter your message *" rows="4" required="required" data-error="Please enter your message"></textarea>
                <div class="help-block with-errors"></div>
            </div>
        <p>
          <div class="g-recaptcha" data-sitekey="6LfsexAUAAAAAF_qKlK7De8kA7XM2MGrGKTyK60M"></div></p>

         <input type="submit" class="btn btn-success btn-send" value="Submit"></p>
         <br><p class="text-muted"><strong>*</strong> These fields are required.</p>
         </form>

来自contact.php文件的现有代码是这样的:

<?php


$from= "[email protected]";
$sendTo = "[email protected]";
$subject = "New message from contact form";
$fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.';
$errorMessage = 'There was an error while submitting the form. Please try again later';


try
{
$emailText = "You have new message from contact form\n=============================\n";

foreach ($_POST as $key => $value) {

    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
}

mail($sendTo, $subject, $emailText, "From: " . $from);

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
else {
echo $responseArray['message'];
}

目前,此方法运行良好,但是当我尝试将reCaptcha验证集成到php中时,不会生成电子邮件,并且php中的成功消息也不会显示在网页上。

将reCaptcha验证集成到php文件中的任何帮助将不胜感激!

编辑:reCaptcha的标签是根据需要在html中,并且该小部件在站点上显示且功能正常。但是,我尝试集成到现有php中的每个示例代码都无法正常工作,并且电子邮件也不会生成(因此,为什么我在上面的php文件中将其遗漏了)。提前致谢!

编辑2:我已经修改了php脚本,并尝试按照CoolCodeGuy的有用注释进行清理。但是,鉴于我的预算php技能,它现在不起作用。请帮忙!!

            <?php


    $from= "[email protected]";
    $sendTo = "[email protected]";
    $subject = "New message from contact form";
    $fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
    $okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.';
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $privatekey = "xxxxxxxxx"; //whatever your PRIVATE key is
    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
    $data = json_decode($response);

    try
    {
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
    //verifcation passed
    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
}

    mail($sendTo, $subject, $emailText, "From: " . $from);
    $responseArray = $okMessage;
   }
   else
   {
   //verification failed
   $responseArray = $errorMessage;
   }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
    }
    else {
    echo $responseArray['message'];
    }
CoolCodeGuy

我已经将您的全部代码更新为本来可以做的方式(我运行了一个社交网站)。

        <?php

    $sendTo = "[email protected]";
    $subject = "New message from contact form";
    $headers .= 'From: <[email protected]>' . "\r\n";

    $name = @$_POST['name'];
    $phone = @$_POST['phone'];
    $email = @$_POST['email'];
    $message = @$_POST['message'];

    $okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.';
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $privatekey = "xxxxxxxxx"; //whatever your PRIVATE key is
    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
    $data = json_decode($response);

    $emailText = "Name: $name \n Phone: $phone \n Email: $email \n Message: $message";

if (isset($data->success) AND $data->success==true) {

    mail($sendTo, $subject, $emailText, $headers);
    $responseArray = $okMessage;
   }
   else
   {
   //verification failed
   $responseArray = $errorMessage;
   }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
    }
    else {
    echo $responseArray;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将clojurescript集成到现有的javascript代码库中?

来自分类Dev

将ASP.NET身份集成到现有的DbContext中

来自分类Dev

如何将现有的.m文件集成到simulink .mdl文件中

来自分类Dev

将react-redux集成到现有的Angularjs应用程序中

来自分类Dev

将react-redux集成到现有的Angularjs应用程序中

来自分类Dev

将炼油厂cms集成到现有的Rails应用程序中

来自分类Dev

将现有的免费或付费版本控件集成到系统中

来自分类Dev

将ASP.NET身份集成到现有的DbContext中

来自分类Dev

如何将现有的angular js应用程序集成到Laravel 5中

来自分类Dev

将React-native集成到现有的App中

来自分类Dev

是否可以将现有的 CryFS 卷集成到 KDE 保险库菜单中?

来自分类Dev

如何将新的 ionic 主题集成到现有的 ionic 应用程序中?

来自分类Dev

将基于令牌的安全性集成到现有的Spring Security Web应用程序中

来自分类Dev

将spring-reactor集成到现有的Spring Framework 4 STOMP Over WebSocket应用程序中

来自分类Dev

将Angle和.net核心Web API安全地集成到现有的MVC 5应用中

来自分类Dev

将spring-reactor集成到现有的Spring Framework 4 STOMP Over WebSocket应用程序中

来自分类Dev

IBM Worklight-如何将Worklight功能集成到现有的本机应用程序中?

来自分类Dev

将数组集成到现有函数中

来自分类Dev

将git集成到eclipse中的现有项目中

来自分类Dev

将现有的C代码集成到Go中。将未签名的字符修饰符结果转换为[] byte

来自分类Dev

是否可以使用swift 3将小型cocos2d-x 3.2游戏集成到现有的IOS应用程序中?

来自分类Dev

将cocoapods集成/安装到现有的xcode项目,objective-c或swift中

来自分类Dev

如何将社交登录集成到我现有的用户架构/登录系统中?

来自分类Dev

现有的PHP Laravel 4与衣柜CMS集成

来自分类Dev

将React或React Native嵌入到现有的移动应用程序中

来自分类Dev

我应该如何使用virtualenv将现有的python环境分叉到新环境中?

来自分类Dev

如何将现有的kafka主题分区散布到更多目录中?

来自分类Dev

将.txt导入现有的excel到新的工作表中

来自分类Dev

如何将数据和表插入到现有的表中?

Related 相关文章

  1. 1

    如何将clojurescript集成到现有的javascript代码库中?

  2. 2

    将ASP.NET身份集成到现有的DbContext中

  3. 3

    如何将现有的.m文件集成到simulink .mdl文件中

  4. 4

    将react-redux集成到现有的Angularjs应用程序中

  5. 5

    将react-redux集成到现有的Angularjs应用程序中

  6. 6

    将炼油厂cms集成到现有的Rails应用程序中

  7. 7

    将现有的免费或付费版本控件集成到系统中

  8. 8

    将ASP.NET身份集成到现有的DbContext中

  9. 9

    如何将现有的angular js应用程序集成到Laravel 5中

  10. 10

    将React-native集成到现有的App中

  11. 11

    是否可以将现有的 CryFS 卷集成到 KDE 保险库菜单中?

  12. 12

    如何将新的 ionic 主题集成到现有的 ionic 应用程序中?

  13. 13

    将基于令牌的安全性集成到现有的Spring Security Web应用程序中

  14. 14

    将spring-reactor集成到现有的Spring Framework 4 STOMP Over WebSocket应用程序中

  15. 15

    将Angle和.net核心Web API安全地集成到现有的MVC 5应用中

  16. 16

    将spring-reactor集成到现有的Spring Framework 4 STOMP Over WebSocket应用程序中

  17. 17

    IBM Worklight-如何将Worklight功能集成到现有的本机应用程序中?

  18. 18

    将数组集成到现有函数中

  19. 19

    将git集成到eclipse中的现有项目中

  20. 20

    将现有的C代码集成到Go中。将未签名的字符修饰符结果转换为[] byte

  21. 21

    是否可以使用swift 3将小型cocos2d-x 3.2游戏集成到现有的IOS应用程序中?

  22. 22

    将cocoapods集成/安装到现有的xcode项目,objective-c或swift中

  23. 23

    如何将社交登录集成到我现有的用户架构/登录系统中?

  24. 24

    现有的PHP Laravel 4与衣柜CMS集成

  25. 25

    将React或React Native嵌入到现有的移动应用程序中

  26. 26

    我应该如何使用virtualenv将现有的python环境分叉到新环境中?

  27. 27

    如何将现有的kafka主题分区散布到更多目录中?

  28. 28

    将.txt导入现有的excel到新的工作表中

  29. 29

    如何将数据和表插入到现有的表中?

热门标签

归档