N
N
Nikolay2015-10-29 12:49:24
PHP
Nikolay, 2015-10-29 12:49:24

Why is the session variable removed?

Controller Admin, action panel:

if(!isset($_SESSION['admin'])) // Тут ее уже не существует. Программа зацикливается.
    {
      session_start();
      header("Location: /admin/authorization/");
      exit;
    }

    if (isset($_POST["exit"]))
    {
      session_unset();
      session_destroy();
      header("Location: /admin/authorization/");
      exit;
    }

Model Admin, action authorization:
public function authorization()
  {
    if (isset($_POST['login'])) {
      if ($this->login == $_POST['login'] && $this->password == md5($_POST['password']))
      {
        $_SESSION['admin'] = 'some';
        header("Location: /admin/panel/");
        exit;
      }
      else {
        header("Location: /admin/authorization/");
        exit;
      }
    }
  }

If the username and password are entered correctly, there is a redirect to the Controller Admin, action panal, where the session variable is no longer there. Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-10-29
@tryvols

The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.
php.net/manual/en/function.session-start.php
In other words, you are trying to use session data before you initialize it.

S
Sergey Zelensky, 2015-10-29
@SergeyZelensky-Rostov

session_start must be called before $_SESSION

session_start();
if(!isset($_SESSION['admin'])) // Тут ее уже не существует. Программа зацикливается.
    {
      header("Location: /admin/authorization/");
      exit;
    }

    if (isset($_POST["exit"]))
    {
      session_unset();
      session_destroy();
      header("Location: /admin/authorization/");
      exit;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question