Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use forget password function in cakePHP

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 212
    Comment on it

    Forget Password function use in cakePhp for making it secure by sending the code to the database in encrypted form, and fetch the value from by using find keyword in the list, this will give you detail on the bases of email id. 
    Forexample: A user forget their password now they want to change it by using their email id, code will sended to the database, that key will be saved in encrpyted from by using the function openssl, it generate a random key of 16 digit length along with it we have to set mail id of sender and receiver too, that link will be send to the user on their mail id, by giving the permission to do reset password.

    Source code:

    public function forgot_password(){
            if (!empty($this->request->data)) {
                 $result = $this->User->find('first', array('conditions' => array('User.email' => $this->request->data['User']['email'])));
                /*Create msg*/
                $i=16;
                $cstrong = true;
                $bytes = openssl_random_pseudo_bytes($i, $cstrong);
                $hex   = bin2hex($bytes);
    
                // print_r($result['User']['id']);die;
                $msg='';
                $msg='Your email has been used in a password reset request at test_HTML <br/>';
                $msg.='If you did not initiate this request, then ignore this message.<br/>';
                $msg.='Copy the link below into your browser to reset your password.<br>';
                $msg.='<a href="http://localhost/test_HTML/users/reset/'.$hex.'>Click the link to Reset Password</a>';
                $msg.='';
         
                $msg=wordwrap($msg,70);
                try{
                        $Email = new CakeEmail();
                        $Email->from(array('me@example.com' => 'My Site'));
                        $Email->to($this->request->data['User']['email']);
                        $Email->subject("Forgot Password");
                        $Email->send($msg);
                        $this->request->data['User']['id'] = $result['User']['id'];
                        $this->request->data['User']['h_key'] = $hex;
                        if ($this->User->save($this->request->data)) {
                            $this->Session->setFlash('Email sent.');
                        }
                }
                catch(Exception $e){
                    echo 'Error:'.$e->getMessage();
                }
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: