Jump to content

Lost password form....probably simple, trying to figure out what I'm doing wrong


Recommended Posts

Can someone take a look at this and see if there's anything wrong with it?  I'm just not seeing a problem, but it gives the error below everytime!

 


//Lost password
if (isset($_POST['lostEmail']) && isset($_POST['lostSubmit'])) {
    $lostEmail = trim($_POST['lostEmail']);

    if (!filter_var($lostEmail, FILTER_VALIDATE_EMAIL))
        $errors['invalidEmail'] = 'Your email address is invalid.';

    $query = 'SELECT * FROM users WHERE email = "' . mysql_real_escape_string($lostEmail) . '" LIMIT 1';
    $result = mysql_query($query);
    if (mysql_num_rows($result) <> 1)
        $errors['noEmail'] = 'This email address does not exist.';

// Probably this if statement....
    if (!errors) {
        $newPassword = "newtestpassword";
        $subject = "Lost Password Request";
        $txt = "Someone (hopefully you) requested a new password on your hehalf \r \n
                Login: " . $lostEmail . "\n
                Password: " . $newPassword . " \r \n
                    Thank you!";
        $headers = "From: admin@mysite.com";
        if (mail($lostEmail, $subject, $txt, $headers)) {
            $success['passwordSent'] = "Mail sent successfully";
        } else {
            $errors['passwordSent'] = "Mail failed to send";
        }
    } else {
        echo "failed<br>";
        echo $lostEmail . $subject . $txt . $headers . "<br>";
    }
}
?>

<form class="box400" name="lostPassword"  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <h2>Lost Password</h2>
            <?php if ($errors['invalidEmail']) print '<div class="invalid">' . $errors['invalidEmail'] . '</div>'; ?>
            <?php if ($errors['noEmail']) print '<div class="invalid">' . $errors['noEmail'] . '</div>'; ?>
            <label for="lostEmail">Email Address</label>
            <input type="text" name="lostEmail" value="" />
            <input type="submit" name="lostSubmit" value="Lost Password" />
            <?php if ($success['passwordSent']) print '<div class="valid">' . $success['passwordSent'] . '</div>'; ?>
            <?php if ($errors['passwordSent']) print '<div class="invalid">' . $errors['passwordSent'] . '</div>'; ?>
        </form>

 

The only return I get is:

failed

myemailaddress@host.com

$errors is returning TRUE. There are only 2 reasons in that code for that to happen. What have you done to troubleshoot it? I'd start by seeing exactly what is in the $errors array.

Yeah, sorry - should have been more clear...in the last else:

 

    } else {
        echo "failed<br>";
        print_r($errors);
        var_dump($errors);
        echo $lostEmail . $subject . $txt . $headers . "<br>";
    }

An empty array should evaluate to TRUE when you do if(!$array), but for grins let's try it the right way, and change that to if( empty($errors) ) instead and see if that makes any difference.

 

Where is the code that initializes $errors as an empty array?

He's not checking against $errors, he's checking against errors

 

You should start programming with error reporting turned on. It'll help you find your typos.

 

Get ready for a potential huge wave of notices though :)

I have at the top of the file:

// Reset errors and success messages

$errors = array();

$success = array();

 

ok......I changed my if statement to if( empty($errors) )and I'm returning $success['passwordSent'].

 

I have two very similar pieces above this, for creating the user and for logging in....so I'm not sure why this issue is even happening.

 

Anyway, now I can move onto the next problem. :)

Thanks!

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.