PHP Example
Screen shot
Screen shot
Screen shot
Index.php

<html>
<head>
<title>Forget Password</title>
</head>
<body>
<form name="frm" action="" method="post">
<table border="1">
<tr><td align="center" colspan="2"><h3>User Login</h3></td></tr>
<tr>
<td>Enter user_id</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>Enter password</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btn" value="Login" /></td>
</tr>
<tr>
<td align="right" colspan="2"><a href="forgot.php">Forgot password</a></td>
</tr>
</table>
</form>
</body>
</html>

forgot.php

<?php
session_start();

// Mysql connectivity and database connectivity
$conn=mysql_connect('localhost','root','') or die('Mysql not connected');
$database=mysql_select_db('test',$conn) or die('Database Not connected');

// To Get New Password Mail New password to user and store new password into database
if(isset($_REQUEST['btn-forget'])){ 

$user_name=$_POST['email'];
$query1="select * from tbl_user where user_name='$user_name'";
$result1=mysql_query($query1);
$rows1=mysql_num_rows($result1);

if($rows1>=1){
$x=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v",
"w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");
   
$user_password="";
   
for($i=0;$i<=8;$i++){
$user_password=$user_password.$x[rand(0,count($x)-1)];
}
   
$query2="update tbl_user set user_password='$user_password' where user_name='$user_name'";
mysql_query($query2);
   
$to=$_REQUEST['email'];
$subject="New password";
$message="Your New Password is : $user_password";
$from='admin@ittrainingonline.co.in';
$headers = "From: $from";
   
$result=mail($to,$subject,$message,$from);
   
if($result){
$_SESSION['msg']="Your password has been sent to your email";
}
             
}
else{
$_SESSION['msg']="Your Email ID does not exist our database";
}
}
?>
<html>
<head>
<title>Forget Password</title>
</head>
<body>
<form name="frm" action="" method="post">
<table width="362" border="0">
<tr>
<td align="center" colspan="2">
<?php if(isset($_SESSION['msg'])) {
echo "<font color='green' size='+1'>".$_SESSION['msg']."</font>";
unset($_SESSION['msg']);
}
?>
</td></tr>
<tr>
<td align="center" colspan="2">Please enter Email ID to receive password</td>
</tr>
<tr><td height="24" colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td>Enter Email ID</td>
<td><input type="text" name="email"  style="width:200px;" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="btn-forget" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

You can download source code here