Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding 'Forgotten Password' and 'Reset' To CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 419
    Comment on it

    When you Login to a page, Sometimes you forget your password  and want to reset your password. For this you have to follow followings steps to create forget password and reset to your form. 

    Step 1 : Create a simple login form in login.ctp which includes forget password link in it.

    
    <?php echo $this->Session->flash();?>
    <div class="container">
      <div class="midBlock clearfix">
        <div class="col-md-12">
          <h1 class="text-center">Enter your credentials below to <p>Login</p></h1>
          <!-- <form action="login_driver_submit" method="get" accept-charset="utf-8"> -->
          <?php  echo $this->Form->create('User');?>
          <ul class="login">
                  
    
                   <li>
                    <?php echo $this->Form->input('email', array('label' => false, 'type'=>'text','placeholder' => 'Enter  Email / Username')); ?>
    
                  </li>
                  <li>
                    <?php echo $this->Form->password('password', array('class'=>'passw','label' => false,'placeholder' => '******')); ?>
                  </li>
    
                  <li class="clearfix">
    
                   <div class="checkboxDemo">
                    <?php echo $this->Form->input(null,array('label' => false,'type'=>'checkbox','div'=>false,"name" => "remember_me"));?>
                    
                  </div> 
                  <p>
                    Remember me
                  </p>
                  <a href="forgot">Forgot Password</a>
                </li>
                <li>
               
                 <?php echo $this->Form->submit('LOGIN',['class'=>" btnNew"]);?>
                 <p>Click here to <a href="register">Sign up</a> now</p>
               </li>
             </ul>
           </div>
         </div>
       </div>
    
    <script >
      
     $('#UserLoginForm').validate({
          rules: {
            "data[User][email]": { required:true, email:true},
            "data[User][password]": { required: true}, 
            
          },
         messages: {
           "data[User][email]" : { required : "Please enter username",email : "Please enter valid email address"},
           "data[User][password]": { required: "Please provide passsword."}
        }
      });
    
    </script>
    

    Step 2 : Create forget.ctp, where you have to give input field for email address in which you have to enter your email id where the link to reset your password is sent.

    <?php echo $this->Session->flash();?>
    <div class="container">
      <div class="midBlock clearfix">
        <div class="col-md-12">
        <h1 class="text-center">Enter your Email below to <p>Reset Password</p></h1>
     
          <?php  echo $this->Form->create('User');?>
          <ul class="login">
                 
    
                   <li>
                      <?php echo $this->Form->input('email', array('class'=>'passw','label' => false, 'type'=>'email','placeholder' => 'Enter your email')); ?>
                      
    
                      
                    </li>
                  
                  
                <li>
                 
                 <?php echo $this->Form->submit('SUBMIT',['class'=>" btnNew"]);?>
                 
               </li>
             </ul>
           </div>
         </div>
       </div>
    
    <script >
      
    
        $("#UserForgotForm").validate({
    
        rules: {
          "data[User][email]": { required: true },
          },
        messages: {
          "data[User][email]": { 
            required: "Please provide email."
          },
          
          }
      });
    
    
    
       </script>

    Step 3 : Create reset.ctp, which will open when you click on the link send to your email id to reset your password.

    
    <div class="container">
      <div class="midBlock clearfix">
        <div class="col-md-12">
        <h1 class="text-center">Forget Password??? <p>Reset Password</p></h1>
          <!-- <form action="login_driver_submit" method="get" accept-charset="utf-8"> -->
          <?php  echo $this->Form->create('User');?>
          <ul class="login">
                  
                   <li>
                      <?php echo $this->Form->input('password', array('class'=>'passw','label' => false, 'type'=>'password','placeholder' => 'Enter Password')); ?>
                      
    
                     
                    </li>
    
                    <li>
                      <?php echo $this->Form->input('confpassword', array('class'=>'passw','label' => false, 'type'=>'password','placeholder' => 'Confirm Password')); ?>
                      
    
                    
                    </li>
                  
                  
                  
                <li>
                
                 <?php echo $this->Form->submit('SUBMIT',['class'=>" btnNew"]);?>
                 
               </li>
             </ul>
           </div>
         </div>
       </div>
    
        <script>
    
      $("#UserResetForm").validate({
    
        rules: {
          
          "data[User][password]": { required: true , minlength:6},
          "data[User][confpassword]":{equalTo:"#UserPassword"},
          
        },
        messages: {
          
          "data[User][password]": { 
              required: "Please provide password.",
              minlength:"Length  6 characters"
            },
          "data[User][confpassword]":{equalTo:"Password does not match"},
          
        }
      });
    
    </script>
    

    Step 4 : In UserController.php, make two functions for forget password and reset password respectively.

    Forget Password :-

    public function forgot()
    	{
    		$this->layout = 'main';
    
    		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 = 'Click the link to reset password <a href="http://localhost/cakephp/users/reset/' . $hex .'">http://localhost/cakephp/users/reset/' . $hex .'</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']['tkey'] = $hex;//'tkey' add column in database
    				if ($this->User->save($this->request->data)) {
    					$this->Session->setFlash('Email sent. Please check your email id');
    				}
    			}
    			catch (Exception $e) {
    				echo 'Error:' . $e->getMessage();
    			}
    		}
    
    
    
    	}

    Reset Password : -

    public function reset($key = null)
    	{
    		$this->layout = 'main';
    		$result       = $this->User->find('first', array(
    			'conditions' => array(
    				'User.tkey' => $key
    				)
    			));
    
    		if ($this->request->is('post')) {
    			@$this->request->data['User']['id'] = $result['User']['id'];
    			if ($this->User->save($this->request->data)) {
    				$this->Session->setFlash("Password Changed successfully!!");
    				$this->redirect(array(
    					'controller' => 'users',
    					'action' => 'login'
    					));
    			} else {
    				$this->Session->setFlash("Something went Wrong!!");
    			}
    
    
    		}
    	}
    

    Also do this :- Go to you folder->app->config->email.ctp.default(rename it to email.ctp) and the following :-

    public $default = array(
    		'transport' => 'Smtp',
    		'from' => array('site@localhost' => 'My Site'),
    		'host' => 'smtpout.secureserver.net',
    		'port' => 25,
    		'timeout' => 30,
    		'username' => 'abc@abc.com',//add your default email id
    		'password' => '*******,//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: