public function page1() { $view = new View('signup/page1'); // Load Captcha library, you can supply the name of the config group you would like to use. $captcha = new Captcha(); // Form info $form = array('username' => '', 'password' => '', 'password_confirm' => '', 'email' => ''); // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; // Ban bots (that accept session cookies) after 50 invalid responses. // Be careful not to ban real people though! Set the threshold high enough. if ($captcha->invalid_count() > 49) { exit('Bye! Stupid bot.'); } // Form submitted if ($_POST) { // Add some rules, the input field, followed by a list of checks, carried out in order $valid_c = Captcha::valid($this->input->post('captcha_response')); $this->user = ORM::factory('user'); $post = $this->input->post(); if ($this->user->validate($post) && $valid_c) { $this->user->save(); $this->session->set('uid', $this->user->id); $this->user->add(ORM::factory('role', 'login')); $this->auth->login($this->user, $post['password']); url::redirect('/signup/page2'); exit(0); } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); $errors = arr::overwrite($errors, $post->errors('signup_errors')); if (!$valid_c) { $errors['captcha_response'] = "Invalid"; } } } // Put the vars in the template $view->set("errors", $errors); $this->template->content = $view; $this->template->content->captcha = $captcha; $this->template->content->form = $form; }
/** * Demontrates how to use the Captcha library. */ public function captcha() { // Look at the counters for valid and invalid // responses in the Session Profiler. new Profiler(); // Load Captcha library, you can supply the name // of the config group you would like to use. $captcha = new Captcha(); // Ban bots (that accept session cookies) after 50 invalid responses. // Be careful not to ban real people though! Set the threshold high enough. if ($captcha->invalid_count() > 49) { exit('Bye! Stupid bot.'); } // Form submitted if ($_POST) { // Captcha::valid() is a static method that can be used as a Validation rule also. if (Captcha::valid($this->input->post('captcha_response'))) { echo '<p style="color:green">Good answer!</p>'; } else { echo '<p style="color:red">Wrong answer!</p>'; } // Validate other fields here } // Show form echo form::open(); echo '<p>Other form fields here...</p>'; // Don't show Captcha anymore after the user has given enough valid // responses. The "enough" count is set in the captcha config. if (!$captcha->promoted()) { echo '<p>'; echo $captcha->render(); // Shows the Captcha challenge (image/riddle/etc) echo '</p>'; echo form::input('captcha_response'); } else { echo '<p>You have been promoted to human.</p>'; } // Close form echo form::submit(array('value' => 'Check')); echo form::close(); }