A
A
Andrey Vasilev2020-02-12 01:29:18
PHP
Andrey Vasilev, 2020-02-12 01:29:18

PHP login not working?

Hello,
for some reason, after authorization, the avatar, login and personal account do not appear in the header, but the window just closes and nothing happens. Moreover, during registration, everything is pushed into the database, and during authorization, it checks and if the data is correct, it displays "ok", but nothing happens.

if ($_POST['cmd'] == 'login') {
    if (!isset($_POST['email']) || !isset($_POST['pass'])) {
        return;
    }
    $msg = checkData();
    if ($msg != '') {
        echo error($msg);
    } else {
        include(__DIR__ . '/db_config.php');
        $answ = DBConnect();
        if ($answ['result'] != 'ok') {
            echo json_encode($answ);
        } else {
            $q = $DB->query(
                "SELECT * FROM cs_users " .
                "WHERE email=\"{$_POST['email']}\" " .
                "and pass=\"" . md5($_POST['pass'] . SALT) .'"'
            );
            if ($q->num_rows !== 0) {
                $_SESSION['authorized'] = true;
                $i = $q->fetch_assoc();
                $_SESSION['userID'] = $i['userID'];
                $_SESSION['name'] = $i['name'];
                $_SESSION['email'] = $i['email'];
                $_SESSION['date'] = $i['date'];
                $_SESSION['avatar'] = $i['avatar'];
                echo json_encode([
                    'result' => 'ok'
                ]);
            } else {
                echo error("Неверные имя и/или пароль.\n");
            }
            $q->free_result();
        }
    }
}


<?php
    if (isset($_SESSION['authorized']) && $_SESSION['authorized'] === true) {
?>
                <div class="header-auth">
                    <img class="header-auth__photo" src="<?=$_SESSION['avatar'];?>" alt="Пользователь">
                    <div class="header-auth-ab">
                        <span class="header-auth-ab__name"><?=$_SESSION['name'];?></span>
                        <img src="img/lk/out.svg" alt="">
                        <a href="profile.php" class="header-auth-ab__lk">Личный кабинет</a>
                    </div>
                </div>
<?php
} else {
?>

                <div class="header-auth">
                    <div class="header-auth__login">Вход</div>
                    <div class="header-auth__signup">Регистрация</div>
                </div>
<?php
}
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Vasilev, 2020-02-12
@Nolis

The problem was an extra space at the very beginning of login.php
Try to remove all existing spaces and save the file in UTF-8 without BOM.

A
AUser0, 2020-02-12
@AUser0

In the browser in Web Developer Tools , look on the Network tab , what exactly is passed from the login form to the server, and what arrives from the server in response. Apparently there arrives absolutely _NOT_ 'ok' in JSON format.

P
Pavel, 2020-02-12
@PavelFokeev

Did you start the session? session_start();
Is there a BOM character before starting the session ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question