{"id":6584,"date":"2019-09-21T07:58:18","date_gmt":"2019-09-21T07:58:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=6584"},"modified":"2021-11-16T10:02:15","modified_gmt":"2021-11-16T10:02:15","slug":"how-to-enable-google-recaptcha-with-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-enable-google-recaptcha-with-laravel\/","title":{"rendered":"How to enable Google reCaptcha with Laravel?"},"content":{"rendered":"\n<p> <strong>Step 1<\/strong> &#8211; First go to <strong>https:\/\/www.google.com\/recaptcha\/intro\/v3.html <\/strong>  then click on <strong>Admin console<\/strong> <\/p>\n\n\n\n<p><strong>Step 2<\/strong> &#8211; Genereate new recaptcha (<em>click on <\/em><strong><em>+<\/em><\/strong><em> button to create<\/em>)<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Type <strong>label name<\/strong><\/li><li>Select <strong>ReCaptcha v2<\/strong>   \u25ba  select &#8220;I&#8217;m not a robot&#8221; Checkbox<\/li><li>Domains (<strong>localhost<\/strong>) or your website address eg <em>www.example.com<\/em><\/li><li>Accept the ReCaptcha <em>term of service<\/em> ( tick checkbox)<\/li><li>send alerts to owners (tick checkbox already)  \u25ba  click on submit button<\/li><\/ol>\n\n\n\n<p><strong>Step 3<\/strong> &#8211; after that you will see <em><strong>site key<\/strong><\/em> &#8211; like this<br> (<em>6LdtjbYUAAAAANm-tqllPU_O-rHyZ-0HwR0COkYk<\/em>)<\/p>\n\n\n\n<p><strong>Step 4<\/strong> &#8211; and <em><strong>secret key<\/strong><\/em> like this (<em>6LdtjbYUAAAAABDNEWDL2EgBN2pq76AdgHQoLx77<\/em>)<\/p>\n\n\n\n<p><strong>Step 5<\/strong> &#8211; copy both keys in notepad.<\/p>\n\n\n\n<p><strong>Step 6<\/strong> -Put below javascript code in <em>resources\/views\/layouts\/app.blade.php<\/em><\/p>\n\n\n\n\n\n<p><strong>Step 7<\/strong> &#8211; and put below code in your form area above <strong>submit<\/strong> button<\/p>\n\n\n<div class=\"g-recaptcha\" data-sitekey=\"{{env('CAPTCHA_KEY')}}\"><\/div>\n<p>                                @if(errors-&gt;has(&#8216;g-recaptcha-response&#8217;))<br \/>\n                                    <span class=\"invalid-feedback\"><br \/>\n                                        <strong>{{$errors-&gt;first(&#8216;g-captcha-response&#8217;)}}<\/strong><br \/>\n                                    <\/span><br \/>\n                                @endif<\/p>\n\n\n\n<p><strong>Step 8<\/strong> &#8211; open any terminal like <strong>gitbash<\/strong> or <strong>PowerShell<\/strong> and type this command (composer require google\/recaptcha &#8216;~1.1&#8217; press enter<br> \u25ba  then run command <em>php artisan make:rule Captcha<\/em> (press enter)<br> \u25ba then open <em>app\/Rules\/Captcha.php<\/em> and add below lines<\/p>\n\n\n<p>use ReCaptcha\\ReCaptcha;<\/p>\n\n\n\n<p> \u25ba  and add below codes to <strong>Captcha.php<\/strong><\/p>\n\n\n<p>public function passes($attribute, $value)<br \/>\n    {<br \/>\n        $recaptcha =  new ReCaptcha(env(&#8216;CAPTCHA_SECRET&#8217;));<br \/>\n        $response = $recaptcha-&gt;verify($value, $_SERVER[&#8216;REMOTE_ADDR&#8217;]);<br \/>\n        return $response-&gt;isSuccess();<br \/>\n    }<br \/>\n&#8212;&#8211;<br \/>\npublic function message()<br \/>\n    {<br \/>\n        return &#8216;Please complete the recaptcha to submit the form&#8217;;<br \/>\n    }<\/p>\n\n\n\n<p><strong>Step 9<\/strong> &#8211; and add below lines to <em>app\/Http\/User\/JobController.php<\/em><\/p>\n\n\n<p>use App\\Rules\\Captcha;<\/p>\n\n\n\n<p><strong>Step 10<\/strong> &#8211; and add under validator &#8216;g-captcha-response&#8217; =&gt; new Captcha(),<\/p>\n\n\n\n<p>If you have any query then comment us we will solve your issue and give you also some awesome steps and tricks to solve it. Please follow us for future reference on other topics like PHP, Laravel and other languages.<\/p>\n\n\n\n<p>For More reference Please check this out :<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\"  id=\"_ytid_20036\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/mE6NU5ABel4?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  epyt-is-override  no-lazyload\" title=\"YouTube player\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe>\n<\/div><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_51175\"><iframe loading=\"lazy\"  id=\"_ytid_99579\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_51175\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Step 1 &#8211; First go to https:\/\/www.google.com\/recaptcha\/intro\/v3.html then click on Admin console Step 2 &#8211; Genereate new recaptcha (click on + button to create) Type label name Select ReCaptcha v2&#8230; <\/p>\n","protected":false},"author":8,"featured_media":6596,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201],"tags":[5251,5250,5252,5253,5203,4921,1009,1840,5248],"class_list":["post-6584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-captcha-key","tag-domains","tag-g-capthcha","tag-gitbash","tag-laravel","tag-localhost","tag-notepad","tag-powershell","tag-recaptcha"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=6584"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6584\/revisions"}],"predecessor-version":[{"id":25432,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/6584\/revisions\/25432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/6596"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=6584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=6584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=6584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}