Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Simple Forgot Password In CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.32k
    Comment on it

    Simple Forgot Password System In CakePHP Via Email

    Hello Readers,

    This blog tutorial will explain how to create a simple Forgot Password system in CakePHP where a users will type email in input box & retrieve new password in their registered email id.

    Firstly you should know that "Which one is the best method to implementing a forgot password function in cakePHP? , Before doing coding practice.

     

    There are many way to create a forgot password, some logic are:


    1. question and answer related query or security question.

    2. Send email to you with your old password

    3. send email with a link to get new password.

    4. send token key in mail to reset password.


    But the first one, "Question & answer query" to get old password which you forgot becomes weakest because it is easier to guess someone answer.


    So follow either 2, 3 or 4 since to retrieve password link in your register email id will give you better security.

    simple forgot password Source Code (3. send email with a link to get new password.)

    Add below code in AppController.php

    public function send_mail($email_data = null)
        {    
    
                //echo "<pre>";print_r($email_data);die;
                //echo WWW_ROOT;die;
            $email         = new CakeEmail('default');
            $email_to      = $email_data['to'];
            $email_msg     = $email_data['body'];
            $email_subject = $email_data['subject'];
            
            $email->to($email_to);
            $email->subject($email_subject);
            $mail_status = @$email->send($email_msg);
         
            if (!$mail_status) {
                return FALSE;
            }
            return TRUE;
        }


    Add below code in UsersController.php

    public function forget_password(){
        
    
         if($this->request->is('post'))
        {
           $user_data = $this->request->data;
    
    
          if(!empty($user_data)){
    
              $this->User->recursive=-1;
            $check_email = $this->User->find('first',array('conditions'=>array('User.email_address'=>$user_data['User']['email_address'])));
    
            
            if(!empty($check_email)){
    
              $data['id'] = $check_email['User']['id'];
    
              $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
              $new_password = '';
              for ($i=0; $i<6; $i++)
              {
              $new_password .= $characters[rand(0, strlen($characters) - 1)];
              }
              
              $data['password'] = md5($new_password);
              $this->User->save($data);
                /* Sending Email to user */
    
              $email=$user_data['User']['email_address'];
              $message = '';  
              $message .= '<html>';
                      $message.='<table style="width:800px;margin:auto;border-collapse:collapse;border:1px solid #5A5A5A;">';
              $message.='<thead style="background:#5A5A5A;">';
              $message.='<tr>';
              $message.='<td width="50%" style="padding:14px 20px;text-align:right;font-size:25px;color:#fff;"></td>';
              $message.='</tr>';
              $message.='</thead>';
              $message.='<tbody>';
              $message.='<tr>';
              $message.='<td style="padding:5px 20px;" colspan="2">';
              $message .= "<h3>New Password  :".$new_password."</h3></br>";
                 
              
              $message .= '<br/><br/>Best Regards';
              $message .= '<br/><br/> My Team';
              $message.='</td>';
              $message.='</tr>';
              $message.='</tbody>';
              $message.='</table>';
              $message .= '<html>';
    
            $data_send['body'] = $message;
                $data_send['subject'] = "Forgot Password - My Team";
         
                $data_send['to'] = $email;
    
                //echo "<pre>";print_r($data_send);die;
    
                    // echo "<pre>";print_r($data_send);die;
           
           $output = $this->send_mail($data_send);
    
    
          
                /* Sending Email to user */
              if($output){  
    
                 $this->Session->setFlash('Password has been changed, Check Your Mail', 'default', array('class' => 'example_class'));
                  $this->redirect(array('controller' => 'users', 'action' => 'login'));
                //echo json_encode(array('status' => 'success', 'message' => "Password has been changed , please check your email")); die;
              }
              else{
                      $this->Session->setFlash('Password has been changed ', 'default', array('class' => 'example_class'));
                    $this->redirect(array('controller' => 'users', 'action' => 'registration'));
              }
            }
    
            else{
                      $this->Session->setFlash('Email Not Exist', 'default', array('class' => 'example_class'));
                    $this->redirect(array('controller' => 'users', 'action' => 'registration'));
            }
          }
    
        }
      }
    
    

    Add below code in Email.php (Path: /opt/lampp/htdocs/Your_Project_name/app/Config)  

     public $default = array(
            'transport' => 'Smtp',
            'from' => array('No-reply@gmail.com' => 'My Team'),
            'host' => 'smtpout.secureserver.net',
            'port' => 25,
            'timeout' => 30,
            'username' => '',
            'password' => '',
            'client' => null,
            'log' => false,
            'emailFormat' => 'html',
            'charset' => 'utf-8',
            'headerCharset' => 'utf-8',
        );

     

 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: