hello,
i've been working on the same issue for the last few days and i just cannot seem to get it working properly. the code that i am posting is where it stands now, but it has gone back and forth many times with different variations. my goal is to create a forgot password link on my login page that will ask the user to type in their registered email address. if it matches, then email them a random pwd. else, send them an error message. to me, it seems simple enough, but i'm having one heck of a time. i've read many other posts on various forums to try and figure this out on my own, but i either don't get anywhere or i can only get partial results.
as it stands with the code i have below... the error arrays seem to work okay as i'm unable to submit an email without @ or . and i cannot leave the field empty. however, as long as i include the @ and . then the page accepts it, and sends me to the home page as if everything is okay. i have left out the mail processing script on this one because i'm trying to first figure out how to distinguish whether the email submitted by a user is in the database or not and only then should the user be taken to the home page.
any help would be great, as i mentioned i'm pretty much going in circles.
thank you!
CODE
<?php require_once('Connections/connUser.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$colname_getEmail = "-1";
if (isset($_GET['email'])) {
$colname_getEmail = $_GET['email'];
}
mysql_select_db($database_connUser, $connUser);
$query_getEmail = sprintf("SELECT user_id, username, password, email FROM userTable WHERE email = %s", GetSQLValueString($colname_getEmail, "text"));
$getEmail = mysql_query($query_getEmail, $connUser) or die(mysql_error());
$row_getEmail = mysql_fetch_assoc($getEmail);
$totalRows_getEmail = mysql_num_rows($getEmail);
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
// Initialize array for error messages
$error = array();
$_POST['email'] = trim($_POST['email']);
if (empty($_POST['email'])) {
$error['email'] = 'Please make sure you have filled in an email address.';
}
if (!stristr($_POST['email'],"@") OR !stristr($_POST['email'],".")) {
$error['emailFormat'] = 'Please make sure your email address is valid';
}
function makeRandomPassword() {
$salt = "abchefghjkmnpqrstuvwxyz012345678923456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 7) {
$num = rand() % 41;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random_password = makeRandomPassword();
$db_password = sha1($random_password);
if (!$error) {
$updateSQL = sprintf("UPDATE userTable SET password='$db_password' WHERE email='$email'",
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['user_id'], "int"));
mysql_select_db($database_connUser, $connUser);
$Result1 = mysql_query($updateSQL, $connUser) or die(mysql_error());
$updateGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forgot Password and/or Username</title>
</head>
<body>
<style type="text/css">
<!--
.warning {
color: #F00;
}
-->
</style>
<p>Forgot Password and/or Username</p>
<p><?php
if (isset($error)) {
echo '<ul>';
foreach ($error as $alert) {
echo "<li class='warning'>$alert</li>\n";
}
echo '</ul>';
}
?> </p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Email:</td>
<td><input type="text" name="email" value="<?php echo htmlentities($row_getEmail['email'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Update record" /></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form2" />
<input type="hidden" name="user_id" value="<?php echo $row_getEmail['user_id']; ?>" />
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($getEmail);
?>