F
F
freshlemon2020-04-17 09:16:06
PHP
freshlemon, 2020-04-17 09:16:06

Why doesn't $_SESSION live?

I can't figure out why $_SESSION doesn't work for me. I
checked it using isset($_SESSION), the answer is false

session_start(); at the beginning of the index.php document there
is a js function called in index.php

function auth()
{
    let auth_login = document.getElementById('login_auth').value;
    let auth_pass = document.getElementById('pass_auth').value;
        $.ajax(
            {
                type: "POST",
                url:'auth.php',
                data: { auth_login: auth_login,
                        auth_pass: auth_pass},
                success: function(result)
                {
                    switch(result) {
                        case 'empty':  {
                            alert("Ошибка входа!\nЗаполните все поля!");
                            break;
                        }
                        case 'invalid':  {
                            alert("Ошибка входа!\nНеверный логин или пароль.");
                            break;
                        }
                        default: {
                            alert("Удачно!");
                            document.getElementById('authbtn').style.visibility = 'hidden';
                            document.getElementById('auth').style.visibility = 'hidden';
                            
                            document.getElementById("authDel").remove();
                            document.getElementById("regDel").remove();

                            var div = document.createElement("div");

                            div.innerHTML = '<div class="header-right" onClick="exitClick()"><h2 class="a-animated a-psevdo">Выход (' + result + ')</h2></div><div class="header-right" onClick="cartClick()"><h2 class="a-animated a-psevdo">Корзина</h2></div>';

                            document.getElementById("nav").appendChild(div);
                            break;
                        }
                    }
                }
            });
}

This is what auth.php looks like
<?
session_start();

if(empty($_POST['auth_login']) || empty($_POST['auth_pass'])) {
    echo "empty";
} else {
    $login = $_POST['auth_login'];
    $pass = $_POST['auth_pass'];

    $req = "";

    require_once 'funct.php';
    $users = getUsers();
    
    foreach ($users['users'] as $user) {
        if ($login == $user['login'] || $login == $user['mail']) {
            if(password_verify($pass, $user['password'])) {
                $_SESSION['user'] = $user;
                $req = $user['login'];
            }
        }
    }
    if ($req != "") {
        echo $req;
    } else {
        echo "invalid";
    }
}

?>


In general, now the situation is this, I made the assignment specifically of the user's login $_SESSION['user'] = $user['login']; when doing ajax
on a page with index.php right after ajax it doesn't see $_SESSION
Then I reload the page once, var_dump($_SESSION); gives this
array(1) {
["user"]=>
string(5) "lemon"
}

After one more reload it gives this
array(0) {
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-04-17
@FanatPHP

If isset($_SESSION) returns false, it means that the session_start() function was not called before accessing this variable;
Either was called but failed because of the most famous bug. Google error

session_start(); at the beginning of the index.php document there is

"Would you still remember your mother" (c) anecdote
, what does index.php have to do with it? in which file are you checking the session?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question