Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Forgot Password link code in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 282
    Comment on it

    Hello Readers! Here is the code to send link to your email address when you click on Forgot Password link during login in cakephp.

    Create a view forgot_password.ctp and paste the below code to 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>Reset Password</p></h1>
              <?php echo $this->Form->create('User',array('controller'=>'users','inputDefaults' => array('label' => false,'div' => false))) ;?>
                <ul class="login">
                  <li>
                    <?php echo $this->Form->input('email',array('type'=>'text','placeholder'=>"Enter Email")); ?>
                  </li>
                  <li>
                    <?php echo $this->Form->submit('Send',array('class'=>'btnNew')); ?>
                    <p>Click here to <a href="registration">Sign up</a> now</p>
                  </li>
                </ul>
              </form>
          </div>
        </div>
      </div>
    

     

    Now create a function in UserController.php:

    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 = 'Click the link to reset password <a href="http://localhost/htmltophp/users/reset/' . $hex .'">http://localhost/htmltophp/users/reset/' . $hex .'</a>';
    
    
    			$msg .= '';
    
    			$msg = wordwrap($msg, 70);
    			try {
    				$Email = new CakeEmail();
    				$Email->from(array(
    					'abc@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']['hexkey'] = $hex;
    				if ($this->User->save($this->request->data)) {
    					$this->Session->setFlash('Email sent. Please check your email id');
    				}
    			}
    			catch (Exception $e) {
    				echo 'Error:' . $e->getMessage();
    			}
    		}
    
    	}

    WE have used CakeEmail class to send email which is new in cakephp.

    Note:  Firstly add this line to UserController.php to ensure that the class is loaded.

    App::uses('CakeEmail', 'Network/Email');

    Next thing you are required to do is change the default function of email.php with the following code:

    public $default = array(
    		'transport' => 'Smtp',
    		'from' => array('abc@example.com' => 'My Site'),
    		'host' => 'smtpout.secureserver.net',
    		'port' => 25,
    		'timeout' => 30,
    		'username' => 'abc@example.com',
    		'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: