S
S
Space Around2019-05-16 13:13:25
PHP
Space Around, 2019-05-16 13:13:25

Why doesn't the value of the $_SESSION variable change immediately, only after the page is reloaded?

Why doesn't the value of the variable change immediately ? In index.php, when the button is pressed, an ajax request is sent (no data is sent) to the file enter_controller.php, then the value of the variable is assigned to enter_controller.php and returned back to index.php, where after a successful request the value of the variable is displayed . The problem is that it does not change immediately, but only after reloading the index.php page . If you do not reload the page, console.log will return guest, and if you reload, the value of the variable will be equal to Hello$_SESSION['login']
$_SESSION['login'] //изначально равное "guest"$_SESSION['login']


$_SESSION['login']

$.ajax({
                type: 'POST',
                async: true,
                url: 'php/enter_controller.php',
                success: function(data){
                    console.log("1: "+"<?php echo $_SESSION['login'];?>");
                }
            });


enter_controller.php
require "session.php";
$_SESSION['login'] = "Hello";


session.php
session_start();

if (!isset($_SESSION['login'])) {
    $_SESSION['login'] = "guest";
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SagePtr, 2019-05-16
@SagePtr

phpfaq.ru/newbie/na_tanke

A
Alex-1917, 2019-05-22
@alex-1917

Well, at least in his signature he indicated how things really are))
You have a mistake. Systemic. Not in the code ... Here it is: For
some reason, however, like most beginners, you stumbled into the depths, before that you had to google properly ... Climbing deep into the theory is, of course, good, but what does your attempts to crawl out of the maze have to do with it and Toaster? If you adequately asked the question - you need to get such and such data and display it there (and this is the essence of your problem), you would have been answered long ago. You googled up to the very tomatoes, climbed into the basics, didn’t fully realize them, didn’t understand anything, then essentially ran into a dead end and ... ran to the Toaster ...
At the same time, the original task was left somewhere far behind.
This is called a system error.
You deserve all these answers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question