S
S
supercoder6662020-05-27 18:43:30
PHP
supercoder666, 2020-05-27 18:43:30

Why is login not output from MySQL database?

Hello. I'm making an authorization and registration script. Registration and authorization work flawlessly. But after authorization, the user's login is not displayed, and when dumped, it shows that it is NULL. Very strange. I can not understand what is passed in the session of this variable.

<?php 
  session_start();
  require_once 'connect.php';
  require_once 'config.php';

  if (isset($signInBtn)) 
  {
    $erLog = array();
    $user = R::findOne('users', 'login = ?', array($login));
      if ($user) 
      {
        // логин есть, проверка пароля
        if (sha1($password) == $user->password) 
        {
          // пароль есть, залогинивание
          $_SESSION['signined'] = $user;
          header('Location: ../index.php');
        } 
        else 
        {
          $erLog[] = "Пароль введен неверно";
        }
      } 
      else 
      {
        $erLog[] = "Пользователя с таким именем не существует";
      }
      if (!empty($erLog)) {
        $_SESSION['message'] = array_shift($erLog);
      }

  }
 ?>

<?php
session_start();
require_once 'connect.php'; 
?>
<?php 

if (isset($_SESSION['signined'])) 
{
  echo "Вы авторизованы, ";
  echo $_SESSION['signined']->login;
  echo '<a href="logout.php"> Выйти</a><br><br>';
} 
else
{
  $_SESSION['message'] = 'Для доступа к этой странице необходимо авторизоваться';
  header('Location: ../login.php');
}
 ?>

UPD. And only now I saw that logout does not work either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2020-05-27
@ThunderCat

session stores only elementary types represented as strings. Which is logical, because this is the essence of a file, or a similar storage. In order for such magic to work, you need to:
a) remove from the object all references to resources that will interfere with serialization, if any
b) serialize the object and write to the session
and in reverse order for reading:
a) Load the object class that you will receive (otherwise the magic will not work)
b) Deserialize from the session and add to the object all references to the necessary resources, for example, a reference to the database object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question