{"id":5661,"date":"2020-05-18T14:20:40","date_gmt":"2020-05-18T08:50:40","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5661"},"modified":"2020-05-18T14:20:43","modified_gmt":"2020-05-18T08:50:43","slug":"how-to-add-google-recaptcha-in-php-codeigniter","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-add-google-recaptcha-in-php-codeigniter\/","title":{"rendered":"How to add Google ReCAPTCHA in PHP Codeigniter"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">How to add Google ReCAPTCHA in PHP Codeigniter<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Date: 09-05-2020<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>reCAPTCHA<\/strong>&nbsp;is a free service from&nbsp;Google that helps protect websites from spam and abuse. It is verifying a user is a human or robot. Google reCAPTCHA show some images and give hint&nbsp;in text and ask users to select appropriate images on the subject. If users select perfectly then google verified that the user is not a robot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are creating a login form for your project, then it can change to hack by someone. So at that moment if you use google captcha then it provides the best security for prevent.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"616\" height=\"164\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/newCaptchaAnchor.gif\" alt=\"\" class=\"wp-image-5665\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 1: <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We need to create reCAPTCHA for the required domain and generate a key pair like the site and secret key. We can use the following google link to create reCAPTCHA.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.google.com\/recaptcha\/intro\/v3.html\" target=\"_blank\" rel=\"noopener\">https:\/\/www.google.com\/recaptcha\/intro\/v3.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click on this link then we will get the following page. Click on the <strong>Admin console<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"619\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-1024x619.png\" alt=\"\" class=\"wp-image-5668\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-1024x619.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-300x181.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-768x464.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-1536x928.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-2048x1237.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/1-1-497x300.png 497w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 2:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Once login completed by using Gmail login credentials, <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then we will have to register the site on which you are going to implement reCAPTCHA. We will get the following page.<br><strong>Note:<\/strong> If you already register some domain name in the admin console then the Click Plus symbol on the Admin Console page opened for the setup form. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"676\" height=\"1024\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1-676x1024.png\" alt=\"\" class=\"wp-image-5669\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1-676x1024.png 676w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1-198x300.png 198w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1-768x1163.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1-1014x1536.png 1014w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/2-1.png 1086w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 3:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We have to fill the following details in above page.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Label<\/strong>&#8211; Enter your domain name for which need to implement reCAPTCHA<\/li><li><strong>reCAPTCHA type<\/strong>&#8211; Choose your preferred reCAPTCHA type.<\/li><li><strong>Domains<\/strong>&#8211; Give the domain name to register.<\/li><li><strong>Owners-<\/strong> Your Gmail id will be displayed by default.<\/li><li><strong>Accept the reCAPTCHA Terms of Service<\/strong>&#8211; Click on the checkbox<\/li><li><strong>Send alerts to owners<\/strong>&#8211; Click on the checkbox.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then, click on the <strong>Submit <\/strong>button.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 4:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">After submitting, your website will be registered and click on the settings option then we will get the site and secret key by click on COPY SITE KEY &amp; COPY SECRET KEY from the following page. Then click on the <strong>Save<\/strong> button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"1024\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/3-637x1024.png\" alt=\"\" class=\"wp-image-5681\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/3-637x1024.png 637w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/3-187x300.png 187w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/3-768x1234.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/3.png 877w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 5: To add site key on the PHP file.<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Connect to your server via SSH as the root user using the following command:<br><em><strong>ssh root@your server IP -p port number<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can open login.php by using the following command:<br><strong>vi \/home\/domain name\/application\/views\/login_view.php<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We will add the following code in the required field on the login.php file.<br><br><strong>&lt;div class=&#8221;col-md-6&#8243;&gt;<br>&lt;!&#8211; [PUT THE CAPTCHA WHERE YOU WANT IT] &#8211;&gt;<br>&lt;div class=&#8221;g-recaptcha&#8221; data-sitekey=\u201center site key\u201d&gt;&lt;\/div&gt;<br>&lt;div style=&#8221;margin-top: 5px;color: red; &#8220;&gt;&lt;?php echo<br>form_error(&#8216;g-recaptcha-response&#8217;); ?&gt;&lt;\/div&gt;<br>&lt;\/div&gt;<\/strong><br><br><strong>Note:<\/strong> enter site key &#8211; copy and paste the key from google reCAPTCHA. <br><br>Add the following code in the beginning or end on the login.php file.<br><br><strong>&lt;!&#8211; [PASTE THE PROVIDED API] &#8211;&gt;<br>&lt;script src=&#8217;https:\/\/www.google.com\/recaptcha\/api.js&#8217;&gt;&lt;\/script&gt;<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step &#8211; 6: To add secret key on the PHP file.<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We need to start validating in the controller after form validation.<br><br>We can open login.php by using the following command:<br><strong>vi \/home\/domain name\/application\/controllers\/login.php<\/strong><br><br>Now paste&nbsp;the&nbsp;code just&nbsp;after&nbsp;form&nbsp;validation&nbsp;of&nbsp;the following code.<br><br><strong>&nbsp;\/* [VERIFY CAPTCHA FIRST] *\/<br>$secret = \u2018Enter secret key\u2019; \/\/ CHANGE THIS TO YOUR OWN!<br>$url = &#8220;https:\/\/www.google.com\/recaptcha\/api\/siteverify?<br>secret=$secret&amp;response=&#8221;.$_POST[&#8216;g-recaptcha-response&#8217;];<br>$verify = json_decode(get_web_page($url));<br>if($verify-&gt;success)<\/strong><br><br><strong>Note:<\/strong> Enter secret key &#8211; copy and paste the key from google reCAPTCHA.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Google&nbsp;reCAPTCHA&nbsp;is&nbsp;now ready&nbsp;to use. You can use reCAPTCHA on the website login page. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to add Google ReCAPTCHA in PHP Codeigniter Date: 09-05-2020 Introduction: reCAPTCHA&nbsp;is a free service from&nbsp;Google that helps protect websites [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[341,273,770],"class_list":["post-5661","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter","tag-php","tag-recaptcha","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1tj","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5661"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5661\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}