Problem passing variable from one screen to another

0

is as follows. I'm having a problem using $ _SESSION in PHP. I'm starting now, so I'm sorry if I did not login in the right way.

I have a Login screen that basically runs this code:

        <?php

session_start();

include_once('classes/Database.php');

if (isset($_SESSION['logged_in'])) {

    // Abre a página system.php - Outra tela, onde o nome deve aparecer.

} else {
    if (isset($_POST['form_email'], $_POST['senha'])) {
        $email = $_POST['form_email'];
        $password = md5($_POST['senha']);
        $nome = "";

        if (empty($email) or empty ($password)) {
            $error = "Todos os campos são obrigatórios!";
        } else {
            $query = $pdo->prepare("SELECT * FROM usuarios WHERE u_email = ? AND u_senha = ?");
            $query->bindValue(1, $email);
            $query->bindValue(2, $password);

            $query->execute();

            $num = $query->rowCount();
            if ($num == 1) {
                // Pega o nome do usuário que logou no sistema

                $stmt = $pdo->prepare("SELECT u_nome AS nome FROM usuarios WHERE u_id = '$num'");
                $stmt->bindValue(1, $nome);
                $stmt->bindValue(1, $num);
                $stmt->execute();

                $linha = $stmt->fetch(PDO::FETCH_ASSOC);

                $nome = $linha['nome'];
                $_SESSION['nome'] = $nome;
                $_SESSION['logged_in'] = true;
                echo $_SESSION['nome'];

                header('Location: system.php');
                exit();
            } else {
                $error = 'Email ou senha incorretos!';
            }
        }

    }

    ?>

     // Aqui mostra o código html da página de login

<?php }

Following the structure of the code, you notice that if you found the user in the database I get the variable num, step as the ID and select the name of the user, if I take out header('Location: ...') I will be on the screen and show the user name, works perfectly. See:

Showthecorrectnameof$_SESSION['nome'].Butstartingatthepageofsystem.phpthesessionsimplyseemstobedeletedandgivesvariableerrornotdefined.

System.php Code

<?php

session_start();

include_once('classes/Database.php');

echo $_SESSION['nome']; // Aponta erro aqui, lógico

require_once './page_structure.php';

?>

// Aqui mostra o código html da página system.php

If you can help me, the idea is to show a type Welcome followed by the username.

Note: $_SESSION['logged-in'] shows normal in system.php, with value = 1.

    
asked by anonymous 08.04.2018 / 19:22

2 answers

1

Error resolution:

Login Page Code:

<?php

session_start();

include_once('classes/Database.php');

if (isset($_SESSION['logged_in'])) {

    // Abre a página system.php
    header('Location: pages/system.php');

} else {
    if (isset($_POST['form_email'], $_POST['senha'])) {
        $email = $_POST['form_email'];
        $password = md5($_POST['senha']);
        $nome = "";

        if (empty($email) or empty ($password)) {
            $error = "Todos os campos são obrigatórios!";
        } else {
            $query = $pdo->prepare("SELECT * FROM cms_usuarios WHERE u_email = ? AND u_senha = ?");
            $query->bindValue(1, $email);
            $query->bindValue(2, $password);

            $query->execute();

            $num = $query->rowCount();
            if ($num == 1) {
                // Pega o nome do usuário que logou no sistema

                $stmt = $pdo->prepare("SELECT u_nome AS nome FROM cms_usuarios WHERE u_email = '$email'");
                $stmt->execute();

                $linha = $stmt->fetch(PDO::FETCH_ASSOC);

                $nome = $linha['nome'];
                $_SESSION['nome'] = $nome;
                $_SESSION['logged_in'] = true;
                echo $_SESSION['nome'];

                header('Location: pages/system.php');
                exit();

            } else {
                $error = 'Email ou senha incorretos!';
            }
        }

    }

    ?>

The system.php page remained the same, with no changes.

    
08.04.2018 / 22:13
2

// Get the name of the user who logged into the system

Wrong

$stmt = $pdo->prepare("SELECT u_nome AS nome FROM usuarios WHERE u_id = '$num'");
$stmt->bindValue(1, $nome);
$stmt->bindValue(1, $num);

Correct:

$stmt = $pdo->prepare("SELECT u_nome AS nome FROM usuarios WHERE u_id = ?");
$stmt->bindValue(1, $id);

$ stmt-> bindValue (1, $ name);

$ stmt-> bindValue (1, $ name);

Since the variable $id you will have to return in the first select

$row = $query->fetch(PDO::FETCH_ASSOC);

$id = $row['id'];
  

$num = $query->rowCount(); returns 1 if there is any user with the data entered, so if $query was running it would always return the user name with u_id=1

I do not see the need for two queries to achieve the desired one, just one.

<?php

session_start();

include_once('classes/Database.php');

if (isset($_SESSION['logged_in'])) {

    // Abre a página system.php - Outra tela, onde o nome deve aparecer.

} else {
        if (isset($_POST['form_email'], $_POST['senha'])) {
            $email = $_POST['form_email'];
            $password = $_POST['senha'];
            $nome = "";

            if (empty($email) or empty ($password)) {
                $error = "Todos os campos são obrigatórios!";
            } else {
                $query = $pdo->prepare("SELECT * FROM usuarios WHERE u_email = ? AND u_senha = ?");
                $query->bindValue(1, $email);
                $query->bindValue(2, $password);

                $query->execute();

                $row = $query->fetch(PDO::FETCH_ASSOC);

                $num = $query->rowCount();

                if ($num == 1) {
                    // Pega o nome do usuário que logou no sistema

                    $nome = $row['nome'];
                    $_SESSION['nome'] = $nome;
                    $_SESSION['logged_in'] = true;

                    header('Location: system.php');
                    exit();
                } else {
                    $error = 'Email ou senha incorretos!';
                }
            }
       }

}

echo $error;
?>
    
08.04.2018 / 21:28