Recaptcha submission

<form role="form" id="contact-form" action="contactos" method="post"> <input type="hidden" id="recaptcha" name="ContactForm[recaptcha]" required=""> <input type="text" id="name" maxlength="100" name="ContactForm[name]" class="form-control" required=""> <input type="email" id="email" maxlength="100" name="ContactForm[email]" class="form-control" required=""> <button class="btn btn-outline btn-block address g-recaptcha" data-sitekey="6Lf9SSgUAAA" data-callback="onSubmit" type="submit">Enviar</button> </form> <script> var onSubmit = function (token) { $('#recaptcha').val(token); var form = document.getElementById("contact-form"); form.submit(); }; </script> <?php class Controller { public function contactos() { $model = new ContactForm; if (isset($_POST['ContactForm'])) { $model->attributes = $_POST['ContactForm']; $rs = new RecaptchaService(); if ($model->validate() && $rs->check($model->recaptcha)) { $app->setFlash('success', 'Obrigado pelo seu contacto. Iremos responder-lhe assim que possivel.'); } } $this->render('contactos',['model' => $model]); } } class RecaptchaService { public $secret = "SECREAT_KEY"; public $params = array(); public $api = "https://www.google.com/recaptcha/api/siteverify"; /** * * @param type $response * @return boolean */ public function check($response) { $this->params = "secret=$this->secret&&response=$response"; // Set the CURL options $cURL = curl_init(); curl_setopt($cURL, CURLOPT_URL, $this->api); curl_setopt($cURL, CURLOPT_POST, TRUE); curl_setopt($cURL, CURLOPT_POSTFIELDS, $this->params); curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=utf-8', 'Content-Length: '.strlen($this->params))); // Execute and close CURL $result = json_decode(curl_exec($cURL)); curl_close($cURL); // Parse the result if ($result->success) { return true; } return false; } } ?>

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.