{"id":2170,"date":"2019-08-23T11:50:56","date_gmt":"2019-08-23T10:50:56","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2170"},"modified":"2025-12-04T19:40:54","modified_gmt":"2025-12-04T19:40:54","slug":"validate-recaptcha-response-in-php","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/html\/forms-html\/recaptcha-forms-html\/validate-recaptcha-response-in-php","title":{"rendered":"Validate reCAPTCHA response in PHP"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Simple solution<\/h4>\n\n\n\n<p> This is a copy of the code at <a href=\"https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819\">https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/----- VERIFY THE RECAPTCHA -----\n$post_data = http_build_query(\n    array(\n        'secret' =&gt; CAPTCHA_SECRET,\n        'response' =&gt; $_POST&#91;'g-recaptcha-response'],\n        \/\/'remoteip' =&gt; $_SERVER&#91;'REMOTE_ADDR']\n        'remoteip' =&gt; (isset($_SERVER&#91;\"HTTP_CF_CONNECTING_IP\"]) ? $_SERVER&#91;\"HTTP_CF_CONNECTING_IP\"] : $_SERVER&#91;'REMOTE_ADDR'])    \/\/Needed for cloudflare users\n    )\n);\n$opts = array('http' =&gt;\n    array(\n        'method'  =&gt; 'POST',\n        'header'  =&gt; 'Content-type: application\/x-www-form-urlencoded',\n        'content' =&gt; $post_data\n    )\n);\n$context  = stream_context_create($opts);\n$response = file_get_contents('https:\/\/www.google.com\/recaptcha\/api\/siteverify', false, $context);\n$result = json_decode($response);\nif (!$result-&gt;success) {\n    throw new Exception('CAPTCHA verification failed.', 1);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Really fast solution using CURL if available<\/h4>\n\n\n\n<p>This is a copy of CreativForm&#8217;s nifty code from <a href=\"https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819\">https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/****************************************\n\/\/****************************************\n\/\/********** VALIDATE RECAPTCHA **********\n\/\/****************************************\n\/\/****************************************\nfunction ValidateRechapcha($response){\n\t\/\/ Verifying the user's response (https:\/\/developers.google.com\/recaptcha\/docs\/verify)\n\t$verifyURL = 'https:\/\/www.google.com\/recaptcha\/api\/siteverify';\n\t\n\t\/\/ Collect and build POST data\n\t$post_data = http_build_query(\n\t\tarray(\n\t\t\t'secret' => 'YOUR_SECRET_KEY',\n\t\t\t'response' => $response,\n\t\t\t'remoteip' => (isset($_SERVER&#91;\"HTTP_CF_CONNECTING_IP\"]) ? $_SERVER&#91;\"HTTP_CF_CONNECTING_IP\"] : $_SERVER&#91;'REMOTE_ADDR'])\n\t\t)\n\t);\n\t\n\t\/\/ Send data on the best possible way\n\tif(function_exists('curl_init') &amp;&amp; function_exists('curl_setopt') &amp;&amp; function_exists('curl_exec')) {\n\t\t\/\/ Use cURL to get data 10x faster than using file_get_contents or other methods\n\t\t$ch =  curl_init($verifyURL);\n\t\t\tcurl_setopt($ch, CURLOPT_POST, 1);\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);\n\t\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n\t\t\tcurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\n\t\t\tcurl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);\n\t\t\tcurl_setopt($ch, CURLOPT_TIMEOUT, 5);\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application\/json', 'Content-type: application\/x-www-form-urlencoded'));\n\t\t\t$response = curl_exec($ch);\n\t} else {\n\t\t\/\/ If server not have active cURL module, use file_get_contents\n\t\t$opts = array('http' =>\n\t\t\tarray(\n\t\t\t\t'method'  => 'POST',\n\t\t\t\t'header'  => 'Content-type: application\/x-www-form-urlencoded',\n\t\t\t\t'content' => $post_data\n\t\t\t)\n\t\t);\n\t\t$context  = stream_context_create($opts);\n\t\t$response = file_get_contents($verifyURL, false, $context);\n\t}\n\n\t\/\/ Verify all reponses and avoid PHP errors\n\tif($response) {\n\t\t$result = json_decode($response);\n\t\tif ($result->success===true) {\n\t\t\treturn true;\n\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t\/\/ Dead end\n\treturn false; \n}<\/code><\/pre>\n\n\n\n<p>Call with this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/----- CHECK THE RECAPTCHA -----\n  if (!ValidateRechapcha($_POST&#91;'g-recaptcha-response']))\n  {\n    wp_redirect( home_url( '\/' ) );\n    die;\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple solution This is a copy of the code at https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819 Really fast solution using CURL if available This is a copy of CreativForm&#8217;s nifty code from https:\/\/gist.github.com\/jonathanstark\/dfb30bdfb522318fc819 Call with this<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[],"class_list":["post-2170","post","type-post","status-publish","format-standard","hentry","category-recaptcha-forms-html"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2170"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":5250,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2170\/revisions\/5250"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}