{"id":1317,"date":"2022-07-18T09:29:25","date_gmt":"2022-07-18T09:29:25","guid":{"rendered":"https:\/\/samyakonline.biz\/blog\/?p=1317"},"modified":"2024-11-28T07:32:19","modified_gmt":"2024-11-28T07:32:19","slug":"php-inquiry-form-with-google-recaptcha","status":"publish","type":"post","link":"https:\/\/samyakonline.biz\/blog\/php-inquiry-form-with-google-recaptcha.html","title":{"rendered":"PHP Contact Form with Google reCaptcha V3 Example Code"},"content":{"rendered":"<p class=\"post-modified-info\">Last Updated on November 28, 2024 by <a href=\"https:\/\/samyakonline.biz\/blog\/author\/samyakonline\" class=\"last-modified-author\">Subhash Jain<\/a><\/p>\n\n\n<p>There are two main components\/parts of\u00a0<strong>Google reCaptcha Script Integration\u00a0<\/strong>into your website form. Part I involves integration of the code into your website form page &#8211; we call it as Client Side Integration. Part II involves integration of the code into your website action page &#8211; we call it as Server Side Integration. If you want to know &#8211;\u00a0<a href=\"https:\/\/samyakonline.biz\/blog\/how-to-get-google-recaptcha-site-and-secret-key.html\"><strong>How to Get Google reCaptcha Site and Secret key<\/strong><\/a>, please click the link.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Part I: Google reCaptcha Client Side Integration<\/h2>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Paste this snippet with recaptcha Public key before the closing&nbsp;<code>&lt;\/head&gt;&nbsp;<\/code>tag on your HTML template:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script src = 'https:\/\/www.google.com\/recaptcha\/api.js?render=reCaptcha_PUBLIC_KEY'&gt; &lt;\/script&gt;\n    \/\/Paste this snippet in the JavaScript callback for an action on your website.\n    &lt;script&gt;\n    grecaptcha.ready(function(){\n    grecaptcha.execute('reCaptcha_PUBLIC_KEY',\n        {action: 'contact_us_action_form'}).then(function(token)\n        {\n            \/\/ Verify the token on the server.\n            document.getElementById('captcha_token').value = token;\n        });\n    });\n    &lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Insert this token in the &#8220;Hidden Field&#8221; of your form. Please note that on form load, this hidden field will populate the token value from above JS function automatically.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;input type=\"hidden\" id=\"captcha_token\" name=\"captcha_token\" \/&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Part II: Google reCaptcha Server Side Integration<\/h2>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;In the PHP action code, you need to paste at top this snippet. Please don&#8217;t forget to replace&nbsp;<code>reCaptcha_PRIVATE_KEY<\/code>&nbsp;with&nbsp;<code>your registered private key<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(isset($_POST&#91;'captcha_token'])) {\n    \/\/ reCAPTCHA Settings\n    $captcha = $_POST&#91;'captcha_token'];\n    $ip = $_SERVER&#91;'REMOTE_ADDR'];\n    $key = 'reCaptcha_PRIVATE_KEY';\n    $url = 'https:\/\/www.google.com\/recaptcha\/api\/siteverify';\n    \/\/echo $full_url;\n\n    \/\/ reCAPTCH response\n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, \"$url\");\n        curl_setopt($ch, CURLOPT_POST, true);\n        curl_setopt($ch, CURLOPT_POSTFIELDS, \"secret=$key&amp;response=$captcha\");\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        $output = curl_exec($ch);\n        curl_close($ch);\n        \/\/echo $output;\n        \/\/ If you echo above $output, you will get JSON data as similar to shown below:-\n        \/\/ Json:{ \"success\": true, \"challenge_ts\": \"2020-11-22T10:53:46Z\", \"hostname\": \"www.mydomain.com\",\n        \/\/   \"score\": 0.9, \"action\": \"contact_us_action_form\" }\n    $data = json_decode($output);\n    \/\/print_r($data);\n    \/\/ If request was NOT a valid reCAPTCHA token for your site, terminate script execution.\n    if(isset($data-&gt;success) &amp;&amp;  $data-&gt;success === false) {\n        die('reCaptcha Invalid');\n        exit;\n    }\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are two main components\/parts of\u00a0Google reCaptcha Script Integration\u00a0into your website form. Part I involves integration of the code into your website form page &#8211; we call it as Client Side Integration. Part II involves integration of the code into your website action page &#8211; we call it as Server Side Integration. If you want [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1341,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-scripts"],"modified_by":"Anil","_links":{"self":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts\/1317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/comments?post=1317"}],"version-history":[{"count":0,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/media\/1341"}],"wp:attachment":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}