Add reCAPTCHA V2

时间:2017-04-06 17:21:36

标签: php html5 recaptcha

it is already 1 week that I am trying to add a reCAPTCHA V2 module on my static website. (I bought the html5 template since I do not have any experience with web-dev).

That's my html part:

 <form action="include/sendmail.php" method="post">
   <div class="field">
     <label class="required" for="sc_contact_form_username">Name</label>
     <input type="text" name="username" id="sc_contact_form_username" />
   </div>
   <div class="field">
     <label class="required" for="sc_contact_form_email">Email</label>
     <input type="text" name="email" id="sc_contact_form_email" />
   </div>
   <div class="field message">
     <label class="required" for="sc_contact_form_message">Your Message</label>
     <textarea name="message" id="sc_contact_form_message"></textarea>
   </div>
   <div class="g-recaptcha" data-theme="dark" data-sitekey="MYKEY"></div>
   <div class="button"> <a class="enter" href="#"><span>Submit</span></a>   </div>
 </form>
 <div class="result sc_infobox"></div>

So far so good...the difficult part for me it's the PHP, how do I modify the current PHP to add the validation and make it more secure?

<?php 
global $_REQUEST;
$response = array('error'=>'');

    $user_name = substr($_REQUEST['user_name'], 0, 20);
    $user_email = substr($_REQUEST['user_email'], 0, 40);
    $user_msg = $_REQUEST['user_msg'];

    $contact_email = 'your_mail@mail.com';  

    if (trim($contact_email)!='') {
        $subj = 'Message from ShiftCV HTML';
        $msg = "Name: $user_name
        E-mail: $user_email
        Message: $user_msg";

        $head = "Content-Type: text/plain; charset=\"utf-8\"\n"
            . "X-Mailer: PHP/" . phpversion() . "\n"
            . "Reply-To: $user_email\n"
            . "To: $contact_email\n"
            . "From: $user_email\n";

        if (!@mail($contact_email, $subj, $msg, $head)) {
            $response['error'] = 'Error send message!';
        }
    } else 
            $response['error'] = 'Error send message!';

    echo json_encode($response);
    die();

?>

1 个答案:

答案 0 :(得分:0)

我建议您查看一下这个可以添加到您的应用程序中的编辑器,以进行必要的调用并验证reCaptcha响应:https://github.com/google/recaptcha