PHP - setting a session inside a function

received (I think ...) a very simple problem:

I want to establish a session inside a function. Simple situation:

I received a registration form. After highlighting the form, I call the "login" function, which checks whether the user has been authenticated. If yes, the session should be established.

Here is a very simple code:

session_start();

function login() {
  $SESSION['login'] = true;
}

if (isset($_REQUEST['doLogin'])) {
  login();
  // is session is set here, it works...
}

if ($SESSION['login'] === true) echo 'you are logged in';

But that does not work. What for? Many thanks!

+3
source share
1 answer

You use $SESSION, you need to use$_SESSION

+21
source

Source: https://habr.com/ru/post/1758048/


All Articles