How do I carry in the Location header only the user name

Asked

Viewed 57 times

0

My login screen will lead to login_proc.php that starts the session. I want to pass to the reservation.php screen the user name. How to do this by passing the header or otherwise. Also, how do I get the value you pass in the.php booking.?

<?php
// Iniciar a $_SESSION
session_start();

require_once "conexao.php";


$usuario = filter_input(INPUT_POST,'usuario_nome', FILTER_SANITIZE_SPECIAL_CHARS);
$usuario = mysqli_real_escape_string($banco, $usuario);
$senha = filter_input(INPUT_POST,'senha', FILTER_SANITIZE_SPECIAL_CHARS);
$senha = password_hash($senha, PASSWORD_DEFAULT);
$sql = "SELECT id_usuario, usuario_nome, senha FROM usuario WHERE usuario_nome = '$usuario'";

$resultado = mysqli_query($banco, $sql);
$registro = mysqli_fetch_assoc($resultado);


@header ("location:reserva.php?usuario=$usuario");
  • I don’t understand what you want, and if it’s right, but to do what you want, try it this way: @header("location:reserva.php?usuario=" . $usuario); or @header("location:reserva.php?usuario={$usuario}");

2 answers

0


Goes through the $_SESSION global variable. It is responsible for storing session information.

$resultado = mysqli_query($banco, $sql);
$registro = mysqli_fetch_assoc($resultado);

$_SESSION['userId'] = $registro['id_usuario'];
$_SESSION['userName'] = $registro['usuario_nome'];


@header ("location:reserva.php");

Then simply access $_SESSION and do what you want. It is accessible anywhere in the application:

$userName = $_SESSION['userName'];
  • Why use the @ in @header?

  • @Woss only the AP will be able to answer :P, the Vaz only adjusted for sessions.

  • Exactly, I just copied the code and added the session. But it’s an interesting point, I hadn’t even noticed

-1

If "reserve.php" is for business rule only you can use $_SESSION to store the content you want, if it is the direct return of a page that will have HTML rendered you can do:

echo json_encode(['usuario' =>  $usuario]);

The solution depends on how the return page is implemented.

Browser other questions tagged

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