doubts about the variable session_start ()

0

I put the session_start() method to make the user's name appear on the screen when he logs into the following pages, but I wanted to put his "name" instead of the "email" follow the code that I I did:

Code to validate user input.

<?php 

session_start();

require_once("conexao.php");

$conn = mysqli_connect('localhost', 'root', '') or die ( mysqli_error() );

mysqli_select_db($conn, 'projeto') or die ( mysqli_error() );

$user = $_POST['email'];
$pwd = $_POST['senha'];


$query = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM nutricionista WHERE email = '$user' AND senha ='$pwd'"));

if($query == 1){
    $_SESSION['email'] = $user;
    $_SESSION['senha'] = $pwd;

    header("location: menuNutricionista.php");
} else {
    echo "<script>alert('Dados informados incorretamente!');history.back();</script>";
}

?>

Here is the HTML page where bem-vindo + nome do usuário appears

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <script type="text/javascript" src="../css/script.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
    <link rel="shortcut icon" href="imagens/favicon.png" />  

    <?php  

    session_start();

    if((!isset ($_SESSION['email']) == true) and (!isset ($_SESSION['senha']) == true))
    {
    unset($_SESSION['email']);
    unset($_SESSION['senha']);
    header('location:index.php');
    }

    $logado = $_SESSION['email'];
    ?>

</head>
<body>
    <img id="emblema" src="../imagens/emblema.png">

    <div class="div-logado">
        <?php echo"Bem-vindo </br>$logado "; ?>
    </div>
    
asked by anonymous 08.11.2017 / 03:50

2 answers

1

Well if it's not going to mess up your code, I think you'll just need to create a new query that will bring you an array.

Make:

$query2 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM nutricionista WHERE email = '$user' AND senha ='$pwd'"));

Then define the user's array in SESSION. Example:

$_SESSION['nome'] = $nome

Of course, since there is already a "name" column in your database. Replace with the correct name that is in your table.

In your HTML page you can replace the line that defines the user logged in by: $ logged = $ _SESSION ['name'];

And in the call to show the user name can use the same $ logged.

I do not remember if the mysqli call is correct, if it does not work, let me rewrite it here again.

    
08.11.2017 / 11:41
1

Ignoring all problems, that there are at least four, you must make a new query to get this information, assuming you have the nome information saved in any column of the database, related to the email.

<?php  

session_start();

// Se não houver as informações você encerra a página e redireciona
if(!isset($_SESSION['email'], $_SESSION['senha'])) {
     unset($_SESSION['email'], $_SESSION['senha']);
     session_destroy();

     header('Location: index.php');
     exit();
}

$email = $_SESSION['email'];

// Havendo a sessão você busca o nome
$stmt = mysqli_prepare($conn, 'SELECT nome FROM nutricionista WHERE email = ?');
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $logado);
mysqli_stmt_fetch($stmt);

?>

This has the same principle you did on the other page, the first. We select nome (assuming this is the name of the MySQL column) where email is equal to that of the session, we define the email in mysqli_stmt_bind_param so that it is the value of the first ? of our query.

Then, the result in the name will be set to $logado , as we indicated in mysqli_stmt_bind_result .

Then to display you use:

<div class="div-logado">
    Bem vindo <br>
    <?= htmlentities($logado, ENT_QUOTES | ENT_HTML5, 'UTF-8'); ?>
</div>

In the "security goes with god" method, you can also use:

$result = mysqli_query($conn, 'SELECT nome FROM nutricionista WHERE email = "'. $_SESSION['email'] .'"');

list($logado) = mysqli_fetch_row($result);

That's exactly the same as what you did on the first page.

    
08.11.2017 / 09:53