1
I made a form
log in and it all worked out. But you have a bug, only log in on the second try. It’s in MVC standard:
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>
It seems that you are not redirecting the user when finishing the login, because of your
else
it works the second time... If he successfully logs in he should redirect, no?– Felipe Avelar
Solved, comment there for me to give like and solved .
– misakie