PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Send forgotten password to mail in login form using php and mysql

13 Jul 2014

Send forgotten password to mail in login form using php and mysql

                       Already you know about validate login form with remember me and signout concepts using php. You want to know Login with remember me and signout using php and mysql
                       Now see about how to send forgotten password to mail using php?. First your table contains users mail id. Just imagine this is your table:


Mysql login table

Login form using php and mysql:

         
          Consider the following example:
login.php:

<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }
span{
  color:lightgreen;

 }
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td> <span><a href="forgot.php">Forgot password</a></span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>

</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
    header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new')  or die(mysql_error());
 $name=$_POST['name'];
 $pwd=$_POST['pwd'];
 if($name!=''&&$pwd!='')
 {
   $query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ")  or die(mysql_error());
   $res=mysql_fetch_row($query);
   if($res)
   {
    if(isset($_POST['remember']))
{
  setcookie('name',$name, time() + (60*60*24*1));
  setcookie('pwd',$pwd, time() + (60*60*24*1));
}
    $_SESSION['name']=$name;
    header('location:welcome.php');
   }
   else
   {
    echo'You entered username or password is incorrect';
   }
 }
 else
 {
  echo'Enter both username and password';
 }
}
?>
</body>
</html>


             While you run the above file, you'll get output like this:


Login form using php

Fogot password in php:


                 The php script for send forgotten password to mail is:
forgot.php:


<html>
<head>
<style type="text/css">
 input{
 border:1px solid olive;
 border-radius:5px;
 }
 h1{
  color:darkgreen;
  font-size:22px;
  text-align:center;
 }

</style>
</head>
<body>
<h1>Forgot Password<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>Email id:</td><td><input type='text' name='mail'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
if(isset($_POST['submit']))

 mysql_connect('localhost','root','') or die(mysql_error());
 mysql_select_db('new') or die(mysql_error());
 $mail=$_POST['mail'];
 $q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
 $p=mysql_affected_rows();
 if($p!=0) 
 {
  $res=mysql_fetch_array($q);
  $to=$res['mail'];
  $subject='Remind password';
  $message='Your password : '.$res['password']; 
  $headers='From:guruparthiban19@gmail.com';
  $m=mail($to,$subject,$message,$headers);
  if($m)
  {
    echo'Check your inbox in mail';
  }
  else
  {
   echo'mail is not send';
  }
 }
 else
 {
  echo'You entered mail id is not present';
 }
}
?>
</body>
</html>

         Where,
                 mysql_query("select * from login where mail='".$mail."' "); is select the values from mysql table.
                 mysql_affected_rows() return the number of rows selected. If it returns 0, then there is no values is present in mysql table.
                 mysql_fetch_array() return the results an associated array. You want to know mysql_fetch_array().
                  Mail() is used to send password to mail. Yow want to know mail() in php.

              When you click the 'forgot password' in login page, you'll get output like this:

Forgot password in php

                You entered your email id in text box and click submit button. Now you can get message 'Check your inbox in mail' if the mail is send. Otherwise, you'll get 'Mail is not send' message. If you enetered mail id which is not present is mysql table, then you will get 'You entered mail is is not present' message.

57 comments:

  1. nice post...
    thanx for sharing..

    ReplyDelete
  2. this type in programming resource(4) of type (mysql link) you entered username and password is incorrect

    ReplyDelete
  3. thanks boss reduce my work this code

    ReplyDelete
  4. Code is very nice but it's not showing in my mail

    ReplyDelete
    Replies
    1. Just you run this code in server, you can receive mail.

      Delete
    2. sir i have sent the problem and uptil now i did not recieve the reply... zakirkhan90089

      Delete
  5. Thanks it's work! appreciate it so much!

    ReplyDelete
  6. Thanks very much this code just save me

    ReplyDelete
  7. I tried this code but i received this error..Warning: mail() [function.mail]: Failed to connect to mailserver at "server.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set(). How can i fix this?

    ReplyDelete
    Replies
    1. You can send mail only from server.

      Delete
    2. sir i have paste the same code in my project but cause some error

      Delete
    3. sir the message is shown on page but email is not sent. i m confused in code like $from:"emailaddress of whom";
      $to =" to whom";

      Delete
  8. hi bro code is working fine but when i click submit button the mail is not sent to mail address . i m using xammp server i run this code on localhost. but mail is sent . how can i get the mail from that code contaning user password

    ReplyDelete
    Replies
    1. You can't send mail from local. So you can test this feature at server.

      Delete
    2. can you sent me email on my email
      zakirkhan90089@gmail.com

      Delete
  9. Thank you so much for this my friend, just remember guys, your site must be online and hosted for the mail function to work.

    Worked like a charm for me. :)

    ReplyDelete
  10. When I try this (it's hosted on my webserver) I do not get an email. The funny thing is if I set the "to" field to an email address with the same domain as the site it works, like for example if I try sending to someone@mydomain.com or someotheraddress@mydomain.com (and the "from" is coming from mydomain) it will go through. When I try sending to someone@gmail.com or someone@somewhereelse.com it does not go through.


    I get the following error: Warning: mail(): SMTP server response: 530 SMTP authentication is required. in E:\HostingSpaces\graffix\myserverpath

    It seems like it will only send emails to my domain from someone (my contact form works fine, the user is the "from" address and my email is the "to") but not when I'm trying to send someone else an email from my domain.

    Any ideas?

    ReplyDelete
  11. Fatal error: Call to undefined function email() in /home/sos/public_html/smtp/forgot.php on line 40
    ??

    ReplyDelete
    Replies
    1. You can send mail only from your server not from local

      Delete
  12. gud work...................

    ReplyDelete
  13. what do u mean by server????

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Check your inbox in mail, is on browser but no mail recieved

    ReplyDelete
    Replies
    1. You can send mail only from your server. Not from local system. If u want to send mail from your local system, then you need to setup SMTP port php.ini file and also you need a net connections.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. How i set up my smtp port in my php.ini file can you tell me.

      Delete
  16. hello sir this worked for me thankyou verymuch sir ji I am beginner here.. sir i want your little help... i want to send multiple checkbox value in mail but i am getting an ERROR:- "Warning: trim() expects parameter 1 to be string, array given" PLEASE HELP me sir.. thank you

    ReplyDelete
    Replies
    1. We can use trim() only for trim space or any values in a string. Please check your array whether it have values or not? Then pass array values into trim().

      Delete
  17. sir is it working on localhost or not...i m getting aal the specified messeges but not getting password in my mail...
    plz help me sir...
    thanks in advance

    ReplyDelete
    Replies
    1. Did you confirm you received mail from your localhost? If yes, then please check your MySQL part to get password.

      Delete
  18. Replies
    1. Please refer this http://www.phponwebsites.com/2014/05/php-session.html

      Delete
  19. I have everything setup including smtp configuration but still when I submit I get "mail not send". I submit the correct email address. I am using wampserver and I have put it Online. What could be the problem? please help. Otherwise the codes are good, thank you

    ReplyDelete
  20. cool its working successfully . ty

    ReplyDelete
    Replies
    1. plz tell how to work this code.mail is display in mail server.

      Delete
  21. how can we reset password for above code??
    can you share the link or code?

    ReplyDelete
    Replies
    1. In this tutorial, the above code snippet only send password to corresponding mail address. So the user can see his password in his inbox.

      If you want to reset password, first get password from user and then update that password to login table. The MySQL Query is:

      "update login set password='PASSWORD' where email='MAIL' and name='NAME';"

      Delete
  22. hi i get this error on server..
    Warning: mail() has been disabled for security reasons in D:\Inetpub\vhosts\anandindustries1985.com\d.digitalvichar.com\forget_pwd.php on line 39
    mail is not send

    ReplyDelete
  23. Hi sir The code was successfully work i use it in my server but when i got the check in your mail box there was no an Message. Help

    ReplyDelete
  24. ( ! ) Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\blood\resetpwd.php ............show this eror plz solve it

    ReplyDelete
  25. hi,everyone

    i am using this code and work successfully but i got warning():530 authentication is required..

    please any help me..?

    ReplyDelete
  26. nice code ,this code very usedfull for me..

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  27. Sir i have uploaded my page on server ..
    it is successfully running the code but when i check mail in mail server (gmail.com) , it does not display any mail. help me sir

    ReplyDelete
  28. Hi All,
    I've the following error messages:

    Notice: Undefined index: email in ..\forgot.php on line 29

    Fatal error: Call to undefined function email() in ..\forgot.php on line 39

    ReplyDelete
  29. HI Boss, this code has been helpful. However, it worked for a few times then it went off. Never send email, never display any error, just blank white page that appears and thats it. Any help will be very much appreciated.
    Thanks.
    Zollyzo

    ReplyDelete
  30. hii...nice code working properly...

    Thank you bro.....

    ReplyDelete
  31. it's just printing email not sent y s it so?please helpme

    ReplyDelete
  32. sir apka code web server run nhi kr rha hai submit pr page blank ho jaah rha hai

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. Before selecting a mailing house make sure to do some serious research: The relationship a direct mailer develops with their direct news house is crucial to the success of the company's direct mail shots so proceed with care. Here are some of the things which will help you when looking for the right mailing services company.

    ReplyDelete
  35. Keep in touch whilst functioning from your own home office with out all of the hassle of purchasing or procurment costly office equipment. Debtors are allowed to apply with their a bad credit score background whenever. Autonomous

    ReplyDelete
  36. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\onlineblood\ForgotPassword.php on line 75

    ReplyDelete