PHP Forget Password Script
<?php
include('dbconnect.php');
if(isset($_POST[forgot'']))
{
send_forgot_password($_POST);
}
?>
<?php
function check_email_exists($txtval)
{
$query = "SELECT * FROM users WHERE email ='$txtval'";
$qry = mysqli_query($GLOBALS['db'],$query) or die ("error 6".mysqli_error($GLOBALS['db']));
$num = mysqli_num_rows($qry);
$rec = mysqli_fetch_array($qry);
return $rec;
}
function update_password($uid)
{
$pass= mt_rand(100000, 999999);
$password = md5($pass);
$sql="UPDATE users set password='$password' WHERE id='$uid'";
$query=mysqli_query($GLOBALS['db'],$sql);
return $pass;
}
function send_Email($to, $from, $subject, $msg)
{
// the message
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg, 70);
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html; charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <'.$from.'>' . "\r\n";
// $headers .= 'Cc:
[email protected]' . "\r\n";
$a = mail($to, $subject, $msg, $headers);
}
function send_forgot_password($data)
{
$is_email = check_email_exists($data['email']);
if($is_email)
{
$new_pass = update_password($is_email['id']);
$email_msg = 'New Password: '.$new_pass;
send_Email($to=$is_email['email'], $from='
[email protected]', $subject='Notification: Password Reset', $email_msg);
echo '';
echo '';
}
else
{
echo '';
}
// return $return_data;
}
?>