Breaking News
You are here : Home / Web development / CakePHP / cakePHP Reset Forgot Password via Email
Reset forgot password in cakePHP

cakePHP Reset Forgot Password via Email

CakePHP Reset Forgot Password via Email :

Resetting Forgot password Using cakePHP is a great feature to allow your users to reset their lost or forgot password using the cakePHP. CakePHP is great Framework ,and i love working on it.So today i m gonna share This forgot password script for cakePHP .I have implemented it and now i m gonna put all my experience here so that it might be useful for cakePHP Developers .So we start with the cakePHP reset forgot password tutorial.

[separator /]

CakePHP Reset Forgot Password via Email :: Tutorial :-

For Implementing forgot password logic in cakePHP we first need to know what we require .we require the Email component for sure and also Html , Session ,Form helpers and Session component as well ,but they are very common .i m sure we all use these components. But Email component is necessary to send the email to use with reset Token.So the First thing we do is to include all necessary components in our users controller or appcontroller .

Another important thing is that your Users Table must have Token field.In this tutorial we have tokenhash field in users table.

[jbox title=”Important” color=”red”]
Another important thing is that your Users Table must have Token field.In this tutorial we have tokenhash field in users table.
[separator /]

Resetting forgotten password via Email in cakePHP – Include components :

The very First step is to include important components and helpers .

app/controllers/users_controller.php –

Now After Adding the Necessary components and helpers we’ll  set up our reset password logic in out controller which is users controller.
[separator /]

CakePHP reset forgot password via Email – Controller setup :

Now we gonna set up our users controller and add the forget password logic which will be used to reset our password.In Users controller i am gonna create two functions here.these will be named as forgetpwd and reset.and there will be two views forgetpwd.ctp and reset.ctp ,and also we gonna be using email templates So we gonna create those as well ,the email element we will be using named resetpw.ctp .So lets first set up our controller .

app/controllers/users_controller.php –

[jtabs theme=”blitzer”]
ForgetPw action ::

Reset action ::

The Above are the two functions, the one function forgetpw takes the email Address from user and generates the unique token for that use and store it in the database tokenhash field and create a url with the token appended and send it to the email address of the registered user.Then User clicks on the Link the Link takes user to reset function which first verify if the token is valid or not then you can use the form to replace your password with new one .Now we’ll create view for above two actions.
[separator /]

CakePHP reset forgot password via Email – Views setup :

We need to have the views for those functions or actions we just created above.So we create two views called reset.ctp and forgetpw.ctp.lets see them.

[jtabs theme=”blitzer”]

forgetpw.ctp ::

app/views/users/forgetpw.ctp –


reset.ctp ::

app/views/users/reset.ctp –

You can see the view code is pretty simple and easy to understand .But still the code wont work properly till we have our email element.So we take a look at email elements also which is necessary for resetting cakePHP user password via email.
[separator /]

CakePHP reset forgot password via Email -Emails Template setup :

Now we gonna setup Email Elements which is very important for resetting the password.we gonna create both html and plain text templates as we are sending email as both html and text .So lets take a look at our Email templates.
[jtabs theme=”blitzer”]
resetpw.ctp(Html) ::

app/views/elements/email/html/resetpw.ctp –

resetpw.ctp(plain text) ::

app/views/elements/email/text/resetpw.ctp –

These are the Email Templates which are quite simple.The $ms variable is set by the fogetpw function which is actually a url.
The Your Code should work fine and Your users will able to reset their password using this mechanism . Also don’t forget to add Forget password Link to the Login page.Add the following link to your Login template.SO that users know your site has this feature.

Well that was all ! i hope you liked this Tutorial ! happy resetting password in you cakePHP application.Thanks for visiting and keep visiting.
[separator /]

Tags : , , , ,

12 Replies

  1. Davor Lozic says:

    Thank you. It helped a lot. 🙂

  2. Kyle says:

    having an issue with this:

    I keep getting the error saying that forgetpwd is not in UsersController.

    Any idea why?

  3. Lenon Bhebe says:

    Thank you. It helped a lot.

  4. Ajay Pratap Singh says:

    I have issue when we open reset password p[age and enter password it always reached last else condition

  5. Elvis says:

    Hi Ramandeep,

    it gives me error when i submit my email.
    Notice (8): Undefined index: active [APP\Controller\UsersController.php, line 216]

    line 216 if ($fu[‘User’][‘active’]){


    what is the active for? does it mean to check for if the user is active?


    1. Paul says:

      I believe Ramandeep has ‘active’ and ‘inactive’ users within the database. For me, I do not have a similar schema so I just removed the if statement as well as the corresponding closing bracket, and it worked out fine.

  6. YCKK says:

    Hello there, i just wanted to know if you have made any changes to the database (Tables) and if you did, what exactly did you change. Thanks again for a good tut.

  7. Robert says:

    Great post. It helped me out immensely. Thanks!

  8. pgiani says:

    Dont forget to add to the AppController the reset and forgetpwd pages to the Auth allow pages or the user will not be able to retrive it.


    Thank You , your tutorial is great

  9. Taleeb Ahmad says:

    I follow the steps but it change the password in database but it give me an error when i try to login with new password.I can login with my old password but in the database password is changed

  10. abhilash says:

    I am getting problem You must be logged in to view this page.

  11. abhilash says:

    I am getting problem of You must be logged in to view this page. when I click on forget password button. I have follow each and every steps also created field tokenhash in database. please help I am stuck

Leave a Reply

Want to join the discussion ? Feel Free to contribute !