- passive income

Reset Password via Email in PHP

Reset Password via Email

Password reset or forgot password are the most important feature for our web or any application which you want to develop . Some developer create it with reset password link via email , somebody creates that they first validate user email and if validated then they generate a new password and send these new password to user email . So today i am going to show how to change password via email in php. After completing this forgot password tutorial i am assuring you that you can set this system to your application for  reset password .

To do forgot password we have to follow those following terms

  • 1=> Check given input email whether it is valid or not .
  • 2=> Check given input email whether it is in our Database or not .
  • 3=> If matched then fetch user information .
  • 4=> Then assign userId in a variable .
  • 5=> Create a new password for that assigned userId .
  • 6=> Then finally sends these reset password link to user with new password .


Let’s start our tutorial . First we have to connect our Database .

Connect Database

To connect database first create a config.php file. Write those following code to this file 

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "your database name");

Then create a file name will be Database.php  and write those following code to this file .

Class Database{
    public $host   = DB_HOST;
    public $user   = DB_USER;
    public $pass   = DB_PASS;
    public $dbname = DB_NAME;
    public $link;
    public $error;
    public function __construct(){
    private function connectDB(){
    $this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        $this->error ="Connection fail".$this->link->connect_error;
        return false;
   public function select($query){
        $result = $this->link->query($query) or die($this->link->error.__LINE__);
        if($result->num_rows > 0){
            return $result;
        } else {
            return false;
    public function update($query){
    $update_row = $this->link->query($query) or die($this->link->error.__LINE__);
        return $update_row;
    } else {
        return false;

Now we are going to follow our above steps to complete our reset password system . It is very very easy to understand and complete this forgot password task. Have look on below code

reset password


       $db = new Database(); / Creating Database Class object;   

       if ($_SERVER['REQUEST_METHOD'] == "POST"){

        $email = $_POST['email'];
        $email = strip_tags(mysqli_real_escape_string($db->link,trim($email)));       
                  echo " Enter your email address ";
             elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)){ //Validate Email 
                     echo " invalid email address ";
        $checkmail = "SELECT * FROM your_table_name WHERE email ='$email' LIMIT 1"; //Fetching data for existing email
        $result = $db->select($checkmail); //accessing database class method select() 
                 if ($result == true) { //If email exists then enter next condition
                  while($value = $result->fetch_assoc()){
                      $userId = $value['id']; // Assign current userId 
                      $userName = $value['username']; // Assign current name
                    $text = substr($email, 0, 3); //taking first three letter from email for creating new password
                    $random = rand(10000, 99999); // Generating random number 
                    $newpassword = "$text$random"; //Merging first three letter from email  & random number 
                    $updatePassword= md5($newpassword); //Encrypt password. Here you can use Hashing or whichever do you want.

                    $sql = "UPDATE  your_table_name  //old password is replaced by $newpassword
                                    SET password = '$updatePassword'
                                    WHERE id = '$userId'"; 

                    $Passwordupdated = $db->update($sql); 
                    $to    = "$email";
                    $from  = ""; //Put your send address here .
                    $headers = "From: $from\n";

                $headers .= 'MIME-Version: 1.0'."\r\n"; 
                $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";

                    $subject = "Your Recover Password";
                    $message = "Your user name is ".$userName." and Password is ".$newpassword." please 
                                  visit 'your login url' to login";

                    $sendmail = mail($to, $subject, $message, $headers); //Finally send email with new password

                    if ($sendmail) {
                        echo " Check your email for login, Check your spam message also";
                        echo " Mail not sent ";

                     echo " Email doesn't exists ";

    <form action="" method="post">
     <input type="text" name="email" placeholder="Enter your email">
     <input type="submit" name="submit" value="Update">

Keep in mind that for this procedure you have to make such system that user email should be unique . Hope you will understand all these  procedure of reset password or forgot password. If you like this tutorial please leave a comments and share with your friends. If you find any bugs of this tutorial , please share this with me. Best wishes & happy coding . 

Leave a comments


Recent Tweets