S
S
SaiGe2017-08-30 10:52:48
PHP
SaiGe, 2017-08-30 10:52:48

I create session (for registration). In the authorization file, the array with sessions exists and works, but in the main file, the $_SESSION array is empty. What's wrong?

Here is the code for the authorization file:

<?php session_start(); require "phpStart.php" ?>
<?php 

if(isset($_POST['doLogin'])){
  $data=$_POST;
  $errors=array();
  $user=R::findOne('users','login=?',array($data['login']));
  if($user){
    if(password_verify($data['password'],$user->password)){
      $_SESSION['logged_user']=$user;
      echo '<div style="color:green">'.'Здравствуйте '.$_SESSION['logged_user']->login.', вы авторизованны.</div><form action="/"><button>Назад</button></form>';
      print_r($_SESSION['logged_user']);
    }else{
      echo '<div class="errors er2" style="color:red">Вы ввели не правильный пароль!</div>';
    }
  }else{
    echo '<div class="errors er2" style="color:red">Пользователь не найден!</div>';
  }
}
 ?>

<form action="/log.php" method="POST">
  <input type="text" placeholder="Логин" name="login" value="<?php echo $_POST['login']; ?>">
  <input type="password" placeholder="Пароль" name="password" value="<?php echo $_POST['password']; ?>">
  <button name="doLogin">Войти</button>
</form>

Everything works here and the user's login is displayed.
Here is the main file code:
<?php
session_start();
require "phpStart.php";
?>
<?php print_r($_SESSION); if(isset($_SESSION['logged_user'])){ ?>

test
<?php }else{ ?>
<a href="reg.php">Регистрация</a>
<a href="log.php">Войти</a>

<?php } ?>
What's wrong? Where is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Malyarov, 2017-08-30
@Konstantin18ko

session_start();
In the main block, drag to the second block.

D
Denis A., 2017-08-31
@myphpscript

you start 2 different sessions.
from the docks:

The session_start() function creates a session, or resumes an existing one, based on a session ID passed in a GET or POST request, or passed in a cookie.

it is better to do that all actions are performed through one input index.php file, in which the session starts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question