Edición de «Ejemplo de reCAPTCHA en KumbiaPHP v2»
De KumbiaPHP Framework Wiki
Advertencia: no has iniciado sesión. Tu dirección IP se hará pública si haces cualquier edición. Si inicias sesión o creas una cuenta, tus ediciones se atribuirán a tu nombre de usuario, además de otros beneficios.
Puedes deshacer la edición. Antes de deshacer la edición, comprueba la siguiente comparación para verificar que realmente es lo que quieres hacer, y entonces guarda los cambios para así efectuar la reversión.
Revisión actual | Tu texto | ||
Línea 37: | Línea 37: | ||
*/ | */ | ||
private static $_privateKey = 'TU_CLAVE_PRIVADA'; | private static $_privateKey = 'TU_CLAVE_PRIVADA'; | ||
− | |||
− | |||
− | |||
− | |||
/** | /** | ||
* Genera el HTML con el código reCaptcha | * Genera el HTML con el código reCaptcha | ||
* | * | ||
+ | * @param $error | ||
* @return string | * @return string | ||
*/ | */ | ||
− | public static function html() | + | public static function html($error = NULL) |
{ | { | ||
− | return recaptcha_get_html(self::$_publicKey, | + | return recaptcha_get_html(self::$_publicKey, $error); |
} | } | ||
/** | /** | ||
* Valida que el código colocado sea el correcto | * Valida que el código colocado sea el correcto | ||
− | * @return | + | * @return Response |
*/ | */ | ||
− | public static function | + | public static function validate() |
{ | { | ||
$ret = recaptcha_check_answer (self::$_privateKey, | $ret = recaptcha_check_answer (self::$_privateKey, | ||
Línea 61: | Línea 58: | ||
$_POST['recaptcha_challenge_field'], | $_POST['recaptcha_challenge_field'], | ||
$_POST['recaptcha_response_field']); | $_POST['recaptcha_response_field']); | ||
− | + | ||
− | + | return $ret; | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
Línea 76: | Línea 69: | ||
<?php | <?php | ||
Load::Lib("recaptcha"); | Load::Lib("recaptcha"); | ||
− | class CaptchaController extends | + | class CaptchaController extends ApplicationController |
{ | { | ||
public function index() | public function index() | ||
{ | { | ||
// Si vienen datos de formulario, los enviamos a la vista de nuevo | // Si vienen datos de formulario, los enviamos a la vista de nuevo | ||
− | if ( | + | if ($this->has_post('Datos')) $this->Datos = $this->post('Datos'); |
// Comprobamos que se haya rellenado el reCAPTCHA | // Comprobamos que se haya rellenado el reCAPTCHA | ||
− | if ( | + | if ($this->has_post("recaptcha_response_field")) |
{ | { | ||
// Realizamos la comprobacion | // Realizamos la comprobacion | ||
− | + | $ret = reCaptcha::validate(); | |
− | $this->Datos = | + | if ($ret->is_valid) { |
− | + | $this->Datos = $this->post('Datos'); | |
+ | $this->render("validado"); | ||
} | } | ||
+ | // Enviamos el error a la vista | ||
+ | $this->previousError = $ret->error; | ||
} | } | ||
} | } | ||
Línea 101: | Línea 97: | ||
<strong>Rellena el cuadro con tu nombre: </strong> | <strong>Rellena el cuadro con tu nombre: </strong> | ||
<?php | <?php | ||
− | echo | + | echo form_tag('captcha/'); |
− | echo | + | echo input_field_tag('Datos.nombre'); |
− | echo reCaptcha::html(); | + | // Cargamos la librería y le pedimos un código html |
+ | Load::Lib("recaptcha"); | ||
+ | echo reCaptcha::html($previousError); | ||
− | echo | + | echo submit_tag('Enviar datos'); |
− | |||
− | |||
?> | ?> | ||
</source> | </source> | ||
− | Y por último, para de paso mostrar el método '' | + | Y por último, para de paso mostrar el método ''render'' de KumbiaPHP, creamos la vista que se mostrará en cuanto validemos correctamente el captcha. |
La vista es ''app/views/captcha/validado.phtml'': | La vista es ''app/views/captcha/validado.phtml'': | ||
Línea 175: | Línea 171: | ||
refresh_btn : 'Volver a generar una clave de seguridad.', | refresh_btn : 'Volver a generar una clave de seguridad.', | ||
help_btn : 'Ayuda', | help_btn : 'Ayuda', | ||
− | incorrect_try_again : 'Incorrecto. Por favor, inténtelo de nuevo.' | + | incorrect_try_again : 'Incorrecto. Por favor, inténtelo de nuevo.', |
}, | }, | ||
theme : 'clean' | theme : 'clean' | ||
Línea 181: | Línea 177: | ||
</script> | </script> | ||
</source> | </source> | ||
+ | |||
== Prueba == | == Prueba == |