download libraray http://www.captcha.ru/kcaptcha.zip
unzip and past it in vendor
app/vendor/place here
-----------------------------------------------------
code controller model and view as normal ------------------------------------------------------
class KCaptchaComponent extends Object
{
function startup(&$controller)
{
$this->controller = $controller;
}
function render()
{
vendor('kcaptcha/kcaptcha');
$kcaptcha = new KCAPTCHA();
$this->controller->Session->write('captcha', $kcaptcha->getKeyString());
}
}
?>
save this as [web]/app/controllers/components/kcaptcha.php Model of users class User extends AppModel
{
var $name = 'User';
/**
* validation rules
*/
var $validate = array (
'user_login' => array(
'exists' => array(
'rule' => array( 'checkUnique', 'user_login' ),
'message' => 'The Username you entered has been taken.'
),
'minLength' => array(
'rule' => array('minLength', 3),
'message' => 'Username must at least be 3 character long.'
)
),
'user_pass' => array(
'mingLength' => array(
'rule' => array('minLength', '6'),
'message' => 'Mimimum 6 characters long'
)
),
'user_name' => array(
'minLength' => array(
'rule' => array('minLength', 3),
'message' => 'Username must at least be 3 character long.'
)
),
'user_email' => array (
'email' => array(
'rule' => 'email',
'message' => 'Please supply a valid email address.'
),
'exists' => array(
'rule' => array( 'checkUnique', 'user_email' ),
'message' => 'The email you entered has been registered.'
)
)
);
/**
* Validate if the data is unique.
*
* @param $data The data to be compared.
* @param $fieldName The field name to check.
* @return true If the field name unique. False otherwise.
*/
function checkUnique( $data, $fieldName ) {
$valid = false;
if(isset($fieldName) && $this->hasField($fieldName)) {
$valid = $this->isUnique(array($fieldName => $data));
}
return $valid;
}
}
?>
Controller class UsersController extends AppController
{
var $name = 'Users';
var $components = array('Auth', 'KCaptcha');
function beforeFilter() {
$this->Auth->fields = array('username' => 'user_login', 'password' => 'user_pass');
$this->Auth->allow('register','kcaptcha' );
}
/**
* This method handle the user registration process.
* It will first of all, get the user basic information.
* After user submit the information, a hash key will be generated and
* stored in the database. An email will then send to user and pending
* for user activation
*/
function forget() {
if (!empty($this->data))
{
$username = $this->data['User']['username'];
if( strtolower($this->data['User']['captcha']) == strtolower( $this->Session->read('captcha'))) {
if ($user = $this->User->find(array('or' => array('username' => $username, 'email' => $username)), 'id, username, password, email, first_name, last_name'))
{
extract($user['User']);
$reset_code = Security::hash($username.$id.$password);
$this->set('reset_code', $reset_code);
$this->User->id = $id;
if($this->User->saveField('password_reset', $reset_code) &&
$this->_sendMailTemplate($email, $this->forgetPassword, $this->forgetSubject, 'forget')) {
$this->Session->setFlash('A password reset request has been sent. Check you email address: '.$username);
} else {
$this->Session->setFlash('Error resetting password. Contact Administrator');
}
$this->redirect('/users/login');
}
else {
$this->Session->setFlash('User does not exist.');
}
unset( $this->data['User']['captcha']);
}
else{
$this->Session->setFlash('captcha verification failed');
}
}
}
function kcaptcha()
{
$this->KCaptcha->render();
}
}
?>
views/users/forget.ctp flash('auth'); ?>
flash(); ?>
create('User', array('action' => 'forget'));?>
Forget Password echo $form->input('username', array('label' => 'Username or Email Address')); echo $form->input('captcha', array( 'label' => 'Please type the text presented in the image : ', 'before' => 'user image html tag here="'. $form->url('/users/kcaptcha').'" ')); ?> end('Submit');?>