Why not live $_SESSION?

Can't understand why I don't live $_SESSION
Checked via isset($_SESSION), the answer is false

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

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("login failed!\will zapolnit all fields!");
break;
}
 case 'invalid': {
 alert("login failed!\nevernyi username or password.");
break;
}
 default: {
alert("Successfully!");
 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">Logout (' + result + ')</h2></div><div class="header-right" onClick="cartClick()"><h2 class="a-animated a-psevdo">shopping Cart</h2></div>';

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

Here is auth.php
<?
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, did the assignment specifically user login $_SESSION['user'] = $user['login']; when executing ajax
on the page index.php immediately after ajax, he does not see $_SESSION
Then I reload the page once, var_dump($_SESSION); gives this
array(1) {
["user"]=>
string(5) "lemon"
}

After another reboot, it throws this
array(0) {
}
April 19th 20 at 12:28
1 answer
April 19th 20 at 12:30
If isset($_SESSION), corresponds to false, it means that before you can access this variable were not caused by the function session_start();
Either was called, but it has not worked out for most known bugs. Error to Google

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

"You would have mom remembered" (C) anecdote

what does index.php? in what file you check the session?
You're not adequate ? He is also in auth session_start(); there. - cortez_Roh commented on April 19th 20 at 12:33
This is to ensure that session_start(); in both files there (But I don't know how it should be actually, study session, and that does not work)

A funny anecdote, thanks - heaven_Pollich1 commented on April 19th 20 at 12:36
@heaven_Pollich1, in that file where do isset($_SESSION), there is no session_start();
what is not clear? - Maxime.Mann41 commented on April 19th 20 at 12:39
@Maxime.Mann41,
<?php
session_start();
define('FL_KEY', true);

include "funct.php";
?>
<!DOCTYPE HTML>
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
the <head>
 <title>lab #4</title>
 <link href="http://allfont.ru/allfont.css?fonts=arsenal-regular" rel="stylesheet" type="text/css" />
 <link href="style.css" rel="stylesheet" type="text/css">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="viewport" content="width=1110">
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</head>
the <body>
 <div class="content">
 <div class="screen">
 <header class="header">
 <div class="container">
<nav>
 <div class="header-left">
 <h2><a class="a-animated" href="index.php">Products</a></h2>
</div>
</nav>
 <div class="logo">
 <a href="index.php"><img src="img/logo.svg" width=150px alt="download Error"></a>
</div>
 <nav id="nav">

<?php
 if (isset($_SESSION['user'])) {
 echo '<div class="header-right" onClick="exitClick()">
 <h2 class="a-animated a-psevdo">Logout (' . $_SESSION['user']['login'] . ')</h2>
</div>
 <div class="header-right" onClick="cartClick()">
 <h2 class="a-animated a-psevdo">shopping Cart</h2>
</div>';
 } else {
 echo '<div class="header-right" onClick="authClick()" id="authDel">
 <h2 class="a-animated a-psevdo">Authorization</h2>
</div>
 <div class="header-right" onClick="regClick()" id="regDel">
 <h2 class="a-animated a-psevdo">Registration</h2>
</div>';
}

?>

</nav>
</div>
</header>
 <div id="cap" class="cap">
 <div id="cart-list" class="cart-list">
 <div id="cart" class="cart-bg">
 <h2>In the cart until no products</h2>
<tr><th>Naimenovanie</th><th>Firma</th><th>Categoria</th><th>Cena</th><th>Opisanie</th><th>Kolichestvo</th></tr>
</div>
</div>
</div>
<main>
 <div class="prod-list">
 <div class="prod-bg"></div>
 <div class="production">
 <ul class="products clearfix">

<?php

 $prods = createProds("goods.csv");

 foreach ($prods as $prod) {
 echo '<li class="product">
 <div class="prod-img">
 <img src="img/item' . $prod['id'] . '.png">
</div>
 <div class="prod-head">
 <p><b>' . $prod['name'] . '</b></p>
</div>
 <p><b>Manufacturer:</b> ' . $prod['firm'] . '</p>
 <p><b>Type:</b> ' . $prod['type'] . '</p>
 <p>' . $prod['desc'] . '</p>
 <p><b>Price:</b> ' . $prod['cost'] . '</p>
 <div class="in-cart" value="' . $prod['id'] . '" onClick="Cart(this)">
 <p class="cart-text">In cart</p>
</div>
</li>';

}

?>

</ul>
</div>
</div>
 <div id="reg" class="reg">
 <div class="reg-area">
 <div class="middle">
<h2>Registration</h2>
 <div id="fancy-inputs">
 <label class="input">
 <input type="text" class="registration" id="login">
<span><span>Username</span></span>
</label>

 <label class="input">
 <input type="text" class="email registration" id="email">
<span><span>E-mail</span></span>
</label>

 <label class="input">
 <input type="password" class="registration" id="pass">
<span><span>Password</span></span>
</label>
</div>
 <p class="btn" id="regbtn" onClick="registration()">Register</p>
</div>
</div>
</div>
 <div id="auth" class="reg">
 <div class="reg-area">
 <div class="middle">
<h2>Authorization</h2>
 <div id="fancy-inputs">
 <label class="input">
 <input type="text" class="auth" id="login_auth">
<span><span>Username</span></span>
</label>

 <label class="input">
 <input type="password" class="auth" id="pass_auth">
<span><span>Password</span></span>
</label>
</div>
 <p class="btn" id="authbtn" onClick="auth()">Login</p>
</div>
</div>
</div>
</main>

<footer>
 <div class="copyright">
 <p>© 2020 https:/campin.gg</p>
</div>
</footer>
</div>
</div>


 <script language="JavaScript">

</script>
</body>
</html>


Here is the file without the js script - heaven_Pollich1 commented on April 19th 20 at 12:42
so session_start(); is not working and you have crushed the error output and PHP wants to tell you this, but can't.
When you get the error, run with her here, and Google - Maxime.Mann41 commented on April 19th 20 at 12:45

Find more questions by tags PHP