How to return the id of the user in the session

Asked

Viewed 1,269 times

-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 the id on another page to make a insert at the bank

session_start();
  print_r($_SESSION);
if(!$_SESSION) {
    header("Location: .././index.php");
    exit;
  }
  • who would that id?

  • The id of the user table, which I pulled for the session. On another page I want to link the user to make the Insert in the database

  • would be the value of userid?

  • That’s right, I want the other page to receive this id so that the Insert will be linked to this user

  • You already possess his value, $_SESSION['userid'] put this in a variable on the page of the Insert $variavel = $_SESSION['userid'] don’t forget the session_start();

  • Note that $_SESSION shows all session variable values for a user session

Show 1 more comment

1 answer

1

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

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

If you do:

echo $_SESSION['userid']

You’ll find you have access to the data userid 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 to ensure that it has not been destroyed.

  • 1

    I’ll see, you’ll see, he’ll see.

  • On this page works, I want to do on another page understand

Browser other questions tagged

You are not signed in. Login or sign up in order to post.