Log In

Email:
Password:
Keep me logged in

Forgot Password

Email:




Archives
2013
March (7)
February (13)
January (14)
2012
December (12)
November (12)
October (12)
September (6)
August (12)
July (6)
June (12)
May (6)
April (12)
March (6)
February (12)
January (12)
2011
December (11)
November (14)
October (11)
September (4)
August (16)
July (8)
Categories
CSS (28)
jQuery (116)
PHP (52)
.htaccess (5)
iPod (1)
iPhone (16)
Sponsored Links


Send Forgotten Password
PHP Ajax jQuery Javascript MySQL
Browse Category | Wednesday, June 6, 2012 |

Description: Sends the password of a user via email. Includes the MySQL instructions, the email form, and the PHP/Ajax file that sends the password.







MySQL:
Make a MySQL table for the users.
CREATE TABLE `users` (
`username` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 25 ) NOT NULL ,
`password` VARCHAR( 25 ) NOT NULL
) ENGINE = MYISAM;
INSERT INTO users VALUES ('user', 'you@example.com', 'abc123');
PHP:
settings.php, file where the important MySQL information is located.
<?php

//CONNECTS TO YOUR DATABASE (MODIFY TO YOUR OWN SETTINGS)
$c mysql_connect("server""username""password");
$db mysql_select_db("your_database"$c);

$table 'users';

?>
Code:
form.php, file with the form to submit the email address where the password would be sent.
<html>
<head>
<script src="http://wcetdesigns.com/assets/javascript/jquery.js"></script>
<script>
function fp(){
var email = $("#email").val();
var data = "email="+email;

$.ajax({
type: "POST",
url: "send-password.php",
data: data,
success: function(e){
$("#message").html(e);
}
});
}
</script>
</head>
<body>
<table>
<tr><td align="right">
Email: </td><td align="left"><input id="email"> Type an email that you regularly check for the demo but it must match the one in the database. Change <b>"you@example.com"</b> in your database.</td></tr>
<tr><td align="right"></td><td align="left"><input onClick="fp()" type="button" value="Get Password"></td></tr>
<tr><td align="right"></td><td align="left"><span id="message"></span></td></tr>
</table>
</body>
</html>
send-password.php, AJAX file which checks whether email is valid and if it exists, if so password will be sent to that address.
<?php

$email 
$_POST["email"];
$email_test eregi("^([A-Za-z0-9_-]+)(\.[A-Za-z0-9_-]+)*@([A-Za-z0-9_-]\.)*([A-Za-z0-9_-]+)\.[A-Za-z]{2,}$"$email);

if(
$email_test){
    include(
"settings.php"); //FILE WITH THE IMPORTANT MySQL INFORMATION
    
    
$server_un 'you'//USERNAME OF YOUR WEBSITE EMAIL ADDRESS (CHANGE TO YOUR OWN) Ex: admin
    
$domain $_SERVER["HTTP_HOST"]; //DOMAIN NAME WITH EXTENSION; Ex: (wcetdesigns.com)
    
$server_email $server_un.'@'.$domain//YOUR WEBSITE'S EMAIL ADDRESS
    
    
$q mysql_query("SELECT * FROM $table WHERE email='".$email."'");
    
$n mysql_num_rows($q);
    
$r mysql_fetch_assoc($q);

    if(
$n&&$r["email"]){
        
$to $email;
        
$subject "Forgot Password";
        
$headers "From: Your Site <".$server_email.">\r\n";
        
$headers .= "Content-type: text/html\r\n";
        
$message "You have requested that you forgot your password.<br>
                    Password: <b>"
.$r["password"]."<b>";
        
        
mail($to$subject$message$headers);
    
        echo 
'Password sent to <b>'.$email.'</b>';
    }
} else {
    echo 
'Invalid Email';
}

?>
 13    10
Views: 3,427 Downloads: 596




(Optional in case you want a reply)



10 Comments
ada
ghdfhdfhfdhgfdhfdhfdh

Saturday, April 27, 2013 at 2:55:08 PM
Guest
it doen't showing any error but my mail is not sending ...im using local host...need any change in coding?pls any one help me at ishajohn890@gmail.com

Tuesday, Febuary 26, 2013 at 2:24:16 PM
vignesh
i have a error in send-password.php Deprecated: Function eregi() is deprecated in D:\wamp\www\send-forgotten-password1360203085\send-password.php on line 12Waing: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\wamp\www\send-forgotten-password1360203085\send-password.php on line 16so please help me..............

Thursday, Febuary 7, 2013 at 11:08:17 AM
guest
i have a error in send.password coding

Thursday, Febuary 7, 2013 at 11:04:11 AM
Guest
tgjhgfjgchmghmghj

Wednesday, Febuary 6, 2013 at 5:18:55 PM
1 2 Next Last