简体   繁体   中英

Conditions not being met in my IF statement

I have the below code, which allows a member to recover their password. However I cannot spot the mistake that is made and my conditions aren't being met. When you visit recover.php you are redirected to index.php, so the last else statement is being executed (this should only happen when a user is logged in - meaning they can't recover a password if they are logged in).

<?php
include 'storescripts/init.php';
$msg = "";
if(isset($_GET['success']) === true && empty($_GET['success']) === true)
{
    $msg = "Thanks, we've emailed you.";
}
else
{
    $mode_allowed = array('mem_password');
    if (isset($_GET['mode']) === true && in_array($_GET['mode'],  $mode_allowed) === true)
    {
        if(isset($_POST['mem_email']) === true && empty($_POST['mem_email']) ===  false)
        {
            if (email_exists($_POST['mem_email']) === true)
            {
                recover($_GET['mode'], $_POST['mem_email']);
                header('Location: recover.php?success');
                exit();
            }
            else
            {
                $msg = "<p>Oops, we couldn\'t find that email in the system</p>";
            }
        }
    }
else
    {
        header('Location: index.php');
        exit();
    }
}
logged_in_redirect();
include 'includes/overall/head.php';
include 'includes/overall/template_header.php';
?>
<div id="mainDivShort">
    <h1>Recover</h1>
    <div id="divBreak"></div>
    <?php include ("includes/overall/column_left.php");?>
    <div id="middleContent">
        <?php echo $msg; ?>
        <form action="" method="post">
            <ul>
                <li>Please enter your email address:<br> <input type="text" name="mem_email"></li>
                <li><input type="submit" value="Recover"></li>
            </ul>
        </form>
    </div>
    <?php include ("includes/overall/column_right.php");?>
</div>
<?php include ("includes/overall/template_footer.php");?>

As I am visiting recover.php when I am logged out, I cannot work out what condition is wrong to execute the last else statement.

First of all:

This is not going to work:

if(isset($_GET['success']) === true && empty($_GET['success']) === true)

It should be:

if(isset($_GET['success']) === true && empty($_GET['success']) === false)

And

header('Location: recover.php?success');

should be (else $_GET['success'] will not be set):

header('Location: recover.php?success=1');

And:

<form action="" method="post">

should be:

<form action="recover.php?mode=mem_password" method="post">

For the rest, you code is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM