23

Send Forgotten Password By Mail Using PHP And MySql

Send Forgotten Password by mail using PHP and MySql is a simple script to send password to user email, if user forgets their password. They will fill in E-mail or user id, if the e-mail or database matches in our database value, then the password will be delivered to user email id.

This is a continuation for the last two scripts, this script adds functionality of retrieving forgotten password(Send Forgotten Password by mail using PHP and MySql) by the user name and sending them the login credentials by email.

  1. Simple User Registration Script in PHP and MySql
  2. Simple Login Script in PHP and mysql
  3. User Login with activation check using PHP and MySql

First we will connect to the database, and if the form is submitted, submitted values is assigned to a variable. After that we will fetch the database, if the username matches in the database, and if the results count is equal to one, then we will assign the values of password, email to variables, after that initiate the process of sending email to the user’s email address.

Below is the My Video on User Login, Registration Script in PHP & MySQL

Buy My Complete Video Course at $19 Instead $120

More than 3000+ stdents Joined, It’s 5 star rated course.

You can get My complete video course with all project files for as low as $19 only 10 coupons available

If you missed the above offer, you can use these below offers.

Access for $29 only 20 coupons available

Access for $39 only 50 coupons available

Hurry!! If these coupons runs out, you have to get this course at full price $120.

Code for Send Forgotten Password by mail using PHP and MySql

If you are following from the previous scripts, then add one field in database as email, and add an email address in that field.

We will create two pages

  1. connect.php
  2. forgotpassword.php

Create connect.php file

If you are following from previous tutorials, you should have this file. Below code is to connecting database and selecting the database.

Create a Form for Forgot Password

Here we will display only username field, based on the username will fetch the data to send email.

Add styles

Here I’m using Bootstrap to add styling and some custome styles.

Save this below code as styles.css file.

PHP Logic to send Forgotten Password by E-Mail

Complete code of forgotpassword.php file

If you have problem arranging above bits of code, here is the complete code you can use.

If you want to learn other things you can read other posts – registration, login, user login with activation check, and sending forgotten password by email .

I hope this article helped you to learn user registration using PHP and MySQL . To get latest news and updates follow us on twitter & facebook, subscribe to newsletter. If you have any feedback please let us know by using comment form.

Buy My Complete Video Course at $19 Instead $120

More than 3000+ stdents Joined, It’s 5 star rated course.

You can get My complete video course with all project files for as low as $19 only 10 coupons available

If you missed the above offer, you can use these below offers.

Access for $29 only 20 coupons available

Access for $39 only 50 coupons available

Hurry!! If these coupons runs out, you have to get this course at full price $120.

Incoming search terms:

  • forgot password code in php
  • forgot password in php
  • forgot password php code
  • Forgot password php
  • php forgot password script
  • forgot password php script
  • forget password code in php
  • php code for forgot password
  • ?gander?ttshandlingarna your password will be sent to the email address you enter
  • php forgot password email verification
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 23 comments
Ayisha Raj

http://codingcyber.com/send-forgotten-password-by-mail-using-php-and-mysql-35/#

Thi$ code i$ not \/\/orking…..plea$e $end a $uitable code for the $ame

Reply
    Vivek Vengala

    Hi Ayisha Raj,

    What’s the issue, you are facing? Download entire script and try it on web server.

    Check whether mails are working in your server with simple mail function.

    Reply
silvia

thank you so much its working

Reply
Fadi

Thanks Mate..!!

You Really helped me out ..!!

Reply
akinzben

it really worked tanks for dat bro

Reply
ranjit kumar

why not your forgetpass code is not workig

Reply
    Vivek Vengala

    Hi Ranjith, Thanks for using my code.

    Please try it on web server, it will work. Let me know the feedback.

    Reply
Dennis Briggs

It’s nice that you posted the script forgotpassword.php as a whole. If you uncomment line 13 and 15 it does not show you the email or password. The script just goes blank on post.

Reply
    Vivek Vengala

    Hi Dennis,

    Thanks for letting me know.

    Reply
    Dennis Briggs

    Also if there is no rows in the db it just show a blank page

    Reply
      Vivek Vengala

      Yes Dennis.

      You should follow from User Registration & User Login Script. Otherwise just insert few rows in database.

      Reply
        Dennis Briggs

        I added a row in the db. Row username is somename and pw and marked it active with a 1.
        If you enter a username that is not in the database the script just goes blank.(no checking)

        Reply
          Vivek Vengala

          on line 35 replace the code with this line without quotes “if (empty($to)) {”

          Thanks for pointing the error, I’ll update the code soon.

          Reply
Dennis Briggs

In ASP Script mine look like this
mySQL = “SELECT * FROM table WHERE email = ‘” & strEmail & “‘”

Set objRS = Server.CreateObject(“ADODB.Recordset”)
objRS.Open mySQL, StrCon

IF objRS.EOF THEN
Response.Write “That email address was not found in our database. Please click Back on your browser and enter the email address you registered with.”
ELSE

Reply
Dennis Briggs

This script is very nice on line 14 I added
echo “”; just for looks when checking scipt.
on line 16 I added
$email = $rows[’email’];//FETCHING EMAIL
So the script will echo the email on line 17

Reply
    Vivek Vengala

    Thats Great Dennis,

    Don’t forget to subscribe E-mail Newsletter, Like us on Facebook

    If you want to any other PHP scripts, let me know through the Contact Us page. I’ll do my best.

    Reply
Nitesh

Warning: mail(): SMTP server response: 503 Bad sequence of commands. in C:\xampp\htdocs\quiz\login.php on line 42

Reply
    Vivek Vengala

    Hi Nitesh,

    Use this code in web server, So that emails will be delivered.

    If you are localhost, you can use phpmailer with smtp email server also will work.

    Reply
ahmed

Why did you put
}else{
if ($_POST [’email’] != “”) {
$fmsg = “Not found your email in our database”;
}
}
I think you can just make it like that
}else{
echo “Not found your email in our database”;
}
==================================================
also
//If the message is sent successfully, display sucess message otherwise display an error message.
if($sentmail==1)
{
$smsg = “Your Password Has Been Sent To Your Email Address.”;
}
else
{
if($_POST[’email’]!=””)
$nmsg = “Cannot send password to your e-mail address.Problem with sending mail…”;
}
}

I think you can do it like that
if($sentmail==1)
{
echo “Your Password Has Been Sent To Your Email Address.”;
}
else{
echo “Cannot send password to your e-mail address.Problem with sending mail…”;
}

Reply
    Vivek Vengala

    Hello Ahmed,

    You can do that, but I’m displaying these messages inside body section.
    So that’s why I’m assigning these messages to variables.

    Reply
Rajeev Singh

Hiiii Vivek…

I have created registered and login page successfully. But, when I run this forgot password script in our localhost. Its not working. I am using Ubuntu

Please, reply me soon and let me know what to do

Reply

Leave a Reply: