Answer the question
In order to leave comments, you need to log in
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;
}
}
}
});
}
<?
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";
}
}
?>
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question