简体   繁体   中英

php login behaving strangely

The login works as a typical login should, the problem is that you can ONLY put in the username and it will log you in. Pretty much if the username is correct then the password doesnt even matter.

php

<?php
include 'password.php'; 

session_start();
if (!isset($_SESSION['loggedIn'])) {
    $_SESSION['loggedIn'] = false;
}

if (isset($_POST['password']) && isset($_POST['user'])) {
    if (sha1($_POST['password'] == $password) && sha1($_POST['user']) == $user) {
        $_SESSION['loggedIn'] = true;
    } else {
        $error_msg = "Incorrect Login!";
    }
} 

if (!$_SESSION['loggedIn']): ?>

the mistake is at this line

    if (sha1($_POST['password'] == $password) && sha1($_POST['user']) == $user) {

you put the parentheses to the wrong place (the one after password), the following should be fine.

    if (sha1($_POST['password']) == $password && sha1($_POST['user']) == $user) {

Besides the fact what marvin explained, I would add a line into your code.

} else {
        $error_msg = "Incorrect Login!";
        $_SESSION['loggedIn'] = false;   // Add this line
}

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