Error with Session in PHP

Asked

Viewed 516 times

1

I am making a registration page using PHP. I have a validation to show the user if the registration has been done or not, but is always returning Notice: Undefined index: status_cadastro in C:\xampp\htdocs\login-php-usb\cadastro.php on line 25.

HTML:

            <?php
            if($_SESSION['status_cadastro']):
            ?>
              <div class="notification is-success">
                 <p>Cadastro efetuado!</p>
                 <p>Faça login informando o seu usuário e senha <a href="login.php">aqui</a></p>
               </div>
            <?php
            endif;
            unset($_SESSION['status_cadastro']);
            ?>

Filing cabinet register.php with the Sesssions:

<?php
session_start();
include("conexao.php");

$nome = mysqli_real_escape_string($conexao, trim($_POST['nome']));
$usuario = mysqli_real_escape_string($conexao, trim($_POST['usuario']));
$senha = mysqli_real_escape_string($conexao, trim(md5($_POST['senha'])));

$sql = "select count(*) as total from usuario_web where usuario = '$usuario'";
$result = mysqli_query($conexao, $sql);
$row = mysqli_fetch_assoc($result);

if($row['total'] == 1) {
    $_SESSION['usuario_existe'] = true;
    header('Location: cadastro.php');
    exit;
}

$sql = "INSERT INTO usuario_web (nome, usuario, senha, data_cadastro) VALUES ('$nome', '$usuario', '$senha', NOW())";

if($conexao->query($sql) === TRUE) {
    $_SESSION['status_cadastro'] = true;
}

$conexao->close();

header('Location: cadastro.php');
exit;
?>

I think the mistake is with Septssion, but I couldn’t identify, anyone would have any idea?

  • You even started Session in this file that contains html ?

  • @Thiagopetherson: <?php&#xA;session_start();&#xA;?>&#xA;<!DOCTYPE html>&#xA;<html>&#xA;

  • The line 25 being mentioned in the error message is that of unset or IF ?

  • @Thiagopetherson would do if. https://ibb.co/m8L5ffH

  • @Algeujunior, you know what’s going on. I believe you’re trying to access a Session attribute before it’s created. Type. You are rendering HTML before you started Session . with this attribute. type $_SESSION['status_registration'] = false; if($connected->query($sql) === TRUE) { $_SESSION['status_registration'] = true; }

1 answer

0


You are trying to access a Session attribute that only $_SESSION['status_register'] will only be created if you enter an if($connected->query($sql) === TRUE):

Filing cabinet register.php with the Sesssions:

<?php
session_start();
include("conexao.php");

$nome = mysqli_real_escape_string($conexao, trim($_POST['nome']));
$usuario = mysqli_real_escape_string($conexao, trim($_POST['usuario']));
$senha = mysqli_real_escape_string($conexao, trim(md5($_POST['senha'])));

$sql = "select count(*) as total from usuario_web where usuario = '$usuario'";
$result = mysqli_query($conexao, $sql);
$row = mysqli_fetch_assoc($result);

if($row['total'] == 1) {
    $_SESSION['usuario_existe'] = true;
    header('Location: cadastro.php');
    exit;
}

$sql = "INSERT INTO usuario_web (nome, usuario, senha, data_cadastro) VALUES ('$nome', '$usuario', '$senha', NOW())";
//Seta $_SESSION['status_cadastro'] antes da validação 
$_SESSION['status_cadastro'] = false;

if($conexao->query($sql) === TRUE) {
    $_SESSION['status_cadastro'] = true;
}

$conexao->close();

header('Location: cadastro.php');
exit;
?>

I hope I’ve helped;

Browser other questions tagged

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