Form only works the second time

1

I made a form login and everything worked fine. But it has a bug, only log on the second attempt. It is in MVC pattern:

Controller:

public function index() {
    if(!$this->model->isUserLoggedIn()) {
        if(isset($_POST['login_submit'])) {
            $this->model->dologinWithPostData($_POST['user_name'], $_POST['user_password']);

            foreach ($this->model->errors as $error) {
                echo $error;
            }
        } 

        require APP . 'view/_templates/header.php';
        require APP . 'view/login/index.php';
        require APP . 'view/_templates/footer.php';
    } else {
        header("Location: $url/me");
    }
}

Model

/**
 * log in with post data
 */
public function doLoginWithPostData($user_name, $user_password) {
    // if this user exists
    if($this->userValidate($user_name)) {
        // if the provided password fits
        // the hash of that user's password
        if($this->encryptPassword($user_password) == $this->getUserInfo($user_name, 'password')) {
            // If the user is banned
            if(!$this->isBanned($user_name)) {
                // write user data into PHP SESSION (a file on your server)
                $_SESSION['user_name'] = $user_name;
                $_SESSION['user_login_status'] = 1;

                $this->setUserInfo($this->getUserInfo($user_name, 'id'), 'ip_last', $_SERVER['REMOTE_ADDR']);
                $this->setUserInfo($this->getUserInfo($user_name, 'id'), 'last_online', time());
            } else {
                $this->errors[] = "Sorry, it appears this user is banned. Reason:" . $this->getReason($user_name);
            }
        } else {
            $this->errors[] = "Wrong password. Try again.";
        }   
    } else {
        $this->errors[] = "This user does not exist.";
    }
}

view

<form method="post" name="loginform">

<label for="login_input_username">Username or Email</label>
<input id="login_input_username" class="login_input" type="text" name="user_name" required />

<label for="login_input_password">Password</label>
<input id="login_input_password" class="login_input" type="password" name="user_password" autocomplete="off" required />

<input type="submit"  name="login_submit" value="Log in" />

</form>

<a href="<?php echo URL; ?>register">Register</a>
    
asked by anonymous 25.06.2015 / 01:28

1 answer

1

The problem is not to redirect the user when he can log in within that if :

if(!$this->model->isUserLoggedIn())

And because the user is already logged in the second time on the page, he is redirected successfully because of else .

    
04.07.2015 / 18:07