User Name Recovery—Forgotten User Name Script
This PHP code helps your administrators recover lost user names or passwords. It's important to give them options if they forget something, and this automatic way requires no administrative work at your end, answering emails with "forgot user name" stories of woe.
Below, the configure.php inclusion assures that when the members file on the server is searched for an email address, the correct connections get made that empower checking out this file. The script gets the POSTed email data from the form, except if the form has not been filled in yet, in which case if($email<>"") catches the blank email POST and jumps us to the form, by way of a conditional clause preventing access to the PHP here until the form is used. If the email data is valid (it gets validated via a JavaScript function, below), the members file is searched for the email supplied. If it is not found, a message is displayed and we get to try the form again. If it is found, the record is put in the $check_user_data variable and the $row variable gets the array in the MySQL results variable, and this associative array can now be addressed to find any of the user data, such as passwords and user data.
At this point, we set up the email variables and message and try to send the email. If it works, a message says to expect an email with the info requested. If the email fails due to a system problem, you get a message saying so. The email may not get delivered because it is bogus or their server is down, in which case you'll get a mail delivery problem email about the attempt from mail servers—but that isn't handled by this script, just website mail servers. At the end of the script is the form which carries an onSubmit event that runs a standard regular expressions email validator.
The ".$psbhostemailaddress." email address in the script is YOUR email address and it's supplied by the configure.php file included initially.
<?php
include_once"configure.php";
$email=$_POST['email'];
$email=mysql_real_escape_string($email);
if($email<>""){
$check_user_data = mysql_query("SELECT * FROM members WHERE email = '$email'") or die(mysql_error());
if(mysql_num_rows($check_user_data) == 0)
{echo '<script language="javascript">alert("This email address does not exist. Please try again.")</script>;';unset($email);}
else {$row = mysql_fetch_array($check_user_data);$email=$row['email'];
$to = $email;
$subject = "Here are your login details . . . ";
$message = "This is in response to your request for login details as administrator of your PSB™.\n\nYour User Name is ".$row['username'].".\n\nYour Administrator Password is ".$row['admin_password'].".\n\nYour Users Password is ".$row['users_password'].".\n\nYou may use your administrator password to edit your account settings, add people to your PSB™, edit status code meanings, change your email, or even delete your whole account.\n\nDon't give your admin password to anyone in your group, but do save it somewhere safe.\n\nGive your group members the users password ONLY.\n\nYou and your group may use the users password to log in to your PSB™ and change your status and comments.\n\nEnjoy your PSB™.\n\nRegards,\n\nthe management";
$headers = "From: ".$psbhostemailaddress."\r\nReply-To: ".$email;
if(mail($to, $subject, $message, $headers)){echo "<center><font face='Verdana' size='2'><b><br><br><br><br><br>THANK YOU</b> <br>Your passwords and user name are posted to your email address. Please check your mail soon.</center>";}
else{echo "<center><font face='Verdana' size='2' color=red>There is some system problem in sending login details to your address. <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>User Name Recovery—Forgotten User Name</TITLE>
<meta name="description" content="User Name Recovery—Forgotten User Name">
<meta name="keywords" content="User Name Recovery,forgot User Name,forgotten User Name,php,javascript, dhtml, DHTML">
<style type="text/css">
BODY {margin-left:0; margin-right:0; margin-top:0;text-align:left;background-color:#bbb}
p, li {font:13px Verdana; color:black;text-align:left}
h1 {font:bold 28px Verdana; color:black;text-align:center}
h2 {font:bold 24px Verdana;text-align:center}
h3 {font:bold 15px Verdana;}
.main {position:absolute;width:700px;top:150px;left:150px;padding:30px;text-align:left;border:8px groove blue;background-color:#ddd}
</style>
<div class='main'>
<p><center><B><h2>User Name Recovery—Forgotten User Name</h2></B></center></p>
<form name="MyForm" method="POST" onsubmit="return validateemail()" action="forgot-user-name.php">
<label for='email'><b>Email address: </b><input type="text" name="email" value="" size="30" maxlength='30'></label><br><br>
<center><input type="submit" value="Recover Forgotten User Name"><br><br></center>
<center><input type="reset" value="Reset"><br><br></center>
<center><input type="button" value="Go to Administrator Page" onClick="window.location='administrator-page.php'"><br><br></center>
</form>
</div>
<script language="javascript">
function validateemail(){
var ck_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
if (document.MyForm.email.value.search(ck_email)==-1)
{alert("That email address is not valid.");return false}
return true}
</script>
</body></html>