Jump to content

Password Reset


NLT

Recommended Posts

<?php
include('global.php');
//This code runs if the form has been submitted
if (isset($_POST['submit'])) 
{

// check for valid email address
$email = $_POST['remail'];
$pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
  $error[] = 'Please enter a valid email address';
} 

// checks if the username is in use
$check = mysql_query("SELECT mail FROM users WHERE mail = '$email'")or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 == 0) {
$error[] = 'Sorry, we cannot find your account details please try another email address.';
}

// if no errors then carry on
if (!$error) {

$query = mysql_query("SELECT username FROM users WHERE email = '$email' ")or die (mysql_error());
$r = mysql_fetch_object($query);

//create a new random password

$password = substr(md5(uniqid(rand(),1)),3,10);
$pass = md5($password); //encrypted version for database entry

//send email
$to = "$email";
$subject = "Account Details Recovery";
$body = "Hi $r->username, \n\n you or someone else have requested your account details. \n\n Here is your account information please keep this as you may need this at a later stage. \n\nYour username is $r->username \n\n your password is $password \n\n Your password has been reset please login and change your password to something more rememberable.\n\n Regards Site Admin";
$additionalheaders = "From: <user@domain.com>\r\n";
$additionalheaders .= "Reply-To: noprely@domain.com";
mail($to, $subject, $body, $additionalheaders);

//update database
$sql = mysql_query("UPDATE users SET password='$pass' WHERE email = '$email'")or die (mysql_error());
$rsent = true;


}// close errors
}// close if form sent

//show any errors
if (!empty($error))
{
	$i = 0;
	while ($i < count($error)){
	echo "<div class=\"msg-error\">".$error[$i]."</div>";
	$i ++;}
}// close if empty errors


if ($rsent == true){
echo "<p>You have been sent an email with your account details to $email</p>\n";
} else {
echo "<p>Please enter your e-mail address. You will receive a new password via e-mail.</p>\n";
}

?>

<form action="" method="post">
<p>Email Address: <input type="text" name="remail" size="50" maxlength="255">
<input type="submit" name="submit" value="Get New Password"></p>
</form>

 

I know I need to replace my emails, but I need to select "mail" from the users table, and then username from the user table. From the code I've got, when I type in the email, I get an error

Unknown column 'email' in 'where clause'

 

I'm not sure whether it's connecting or not, what could I add to make it connect to the database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.