Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to add google reCAPTCHA in php contact form

How to add google reCAPTCHA in php contact form

Posted on September 1, 2019December 17, 2022 By XpertPhp 3 Comments on How to add google reCAPTCHA in php contact form

In this tutorial, we are going to integrate the Google reCAPTCHA in PHP. normally captcha is used for security purpose and only human users can pass through. computers or bots are not solving a captcha. so we will learn about captcha that how to integrate the Google reCAPTCHA in PHP.

First, we need to Google reCAPTCHA code library, site key and secret key. so first we go to Google reCAPTCHA panel and get site key and secret key.

After getting key, we will create a form using the bootstrap and pass the site key.

When the user fills the form data with resolves the reCAPTCHA code that time response token will be returned.

index.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html lang="en">
<head>
  <title>How to add google reCAPTCHA in php contact form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div class="container">
   <form id="comment_form" action="check.php" method="post">
   <div class="form-group">
    <label for="name">Name:</label>
    <input type="text" class="form-control" id="name" name="name">
   </div>
 
   <div class="form-group">
    <label for="email">Email address:</label>
    <input type="email" class="form-control" id="email" name="email">
   </div>
 
   <div class="form-group">
      <div class="g-recaptcha" data-sitekey="here_enter_site_key"></div>
    </div>
     <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>
</body>
</html>
See also  PHP Datatables CRUD Example

When the user submits the form that time sends the response token with form data. After checking the validation and return the final response.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
    
    $name = $_POST['name'];
    $email = $_POST['email'];
 
    if(!empty($name) && !empty($email))
    {
        if(isset($_POST['g-recaptcha-response']))
        $captcha=$_POST['g-recaptcha-response'];
 
        if(!$captcha){
            echo '<h2>Please check the the captcha form.</h2>';
            exit;
        }
 
        $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=here_enter_secret_key&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
        if($response['success'] == false)
        {
            echo '<h2>something went wrong please try again</h2>';
        }
        else
        {
            echo '<h2>Thanks for posting comment.</h2>';
        }
 
    }
?>

If you like this article then You can see, comment and share this article demo

Show Demo

Php Tags:Google reCAPTCHA in PHP

Post navigation

Previous Post: file upload with resize image using codeigniter
Next Post: FullCalendar with Event Modal Dialog Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com