How to return the id of the user who is in the session

-1
require_once('conexao.php');
@$email = $_POST['email'];
@$senha = md5($_POST['senha']);
// $email = "[email protected]";
// $senha = "12346";

$pdo = $dbconn->prepare("SELECT userid, nome, nivel FROM usuario WHERE email=:email and senha=:senha");
$pdo->bindParam(":email", $email);
$pdo->bindParam(":senha", $senha);
$pdo->execute();
// print_r ($pdo);

$users = $pdo->fetchAll(PDO::FETCH_ASSOC);

if (count($users) <= 0)
{
    echo "<script>alert('Email ou senha errados');
                top.location.href='./index.php';
                </script>";
    exit;
}

// pega o primeiro usuário
$user = $users[0];

session_start();
$_SESSION['logged_in'] = true;
$_SESSION['userid'] = $user['userid'];
$_SESSION['username'] = $user['nome'];
$_SESSION['usernivel'] = $user['nivel'];
// print_r ($_SESSION);
// echo session_id();

header('Location: ./_link/link.php');

I want to return id on another page to make a insert in the bank

session_start();
  print_r($_SESSION);
if(!$_SESSION) {
    header("Location: .././index.php");
    exit;
  }
    
asked by anonymous 17.03.2018 / 23:13

1 answer

1

To access session variables created, use the keys you used to put data in the session:

  • $_SESSION['logged_in']
  • $_SESSION['userid']
  • $_SESSION['username']
  • $_SESSION['usernivel']

If you do:

echo $_SESSION['userid']

You'll see that you have access to the userid data that you put in the session. However, it is necessary to ensure that the first block of code is executed before calling the session variable and ensuring that it has not been destroyed.

    
18.03.2018 / 03:09