Pages

Showing posts with label google reCaptcha. Show all posts
Showing posts with label google reCaptcha. Show all posts

Tuesday, September 15, 2020

Add Google's reCAPTCHA V2 in Codeigniter and verify reCaptcha

// Controller to Load Recaptcha 

$this->load->library('recaptcha');
$recaptcha = $this->recaptcha->create_box();
$data['recaptcha'] = $recaptcha; 
$this->load->view('home',$data); 

/**************************************************/ 

// View to Show reCaptcha in Form 

<!DOCTYPE html>
<html>
<head>
	<title>CodeIgniter reCAPTCHA</title>
	<!-- reCAPTCHA JavaScript API -->
	<script src='https://www.google.com/recaptcha/api.js'></script>
</head>

<body>
	<form action="/path/to/controller">
		<?=$recaptcha?>
		<button type="submit" name="action" value="submit">Submit</button>
	</form>
</body>

</html>

/**************************************************/ 

// Action Controller
public function send_mail() { 

   $from_name = $this->input->post('name');
   $from_email = $this->input->post('email');
   $from_message = $this->input->post('message');
   $to_email = "zakeerio25@gmail.com";

   //Load email library $this->load->library('email');
    $this->load->library('recaptcha');
    $this->email->from($from_email,$from_name);
    $this->email->to($to_email);
    $this->email->subject('contact us form submitted');
    $this->email->message($from_message);
    $is_valid = $this->recaptcha->is_valid();
    if($is_valid['success']) {
        if($this->email->send()) {
            echo "success";
         } else {
            echo "failure email";
    
    }
    } else {
         echo "failure recaptcha";
    }
}


Further Details:

https://github.com/mehdibo/Codeigniter-recaptcha