Show logged in user name

0

Good afternoon, I'm a beginner in php and would need a help.

I have this file (index.php) where I want to display the user name logged on the screen after login, but this gives an indefinite variable error ($ _SESSION).

Note: disregard the last div = main line

        

            $usuario   = $_SESSION['usuarioSession'];
            $senha     = $_SESSION['senhaSession'];

                $sql = mysql_query("SELECT * FROM syslogin WHERE usuario = '$usuario' AND senha = '$senha'");

                while($linha = mysql_fetch_array($sql)){
                    $usuario = $linha['usuario'];
                    $senha   = $linha['senha'];
                    }
     ?>
    <div class="bemvindo">Bem Vindo! <strong><?php echo $usuario;?></strong> | Hoje é: <?php echo date('d/m/Y');?></div><!--Bem Vindo-->

    <div id="principal">
                <h3>Bem Vindo(a).</h3><br /><br />
                <img src="css/img/logo1.jpg" alt="logo" />
    </div> <!-- Fim da div#principal -->

    <?php include('includes/fimerodape.php'); ?>

Please, where am I going wrong?

    
asked by anonymous 26.07.2017 / 21:52

1 answer

1

This is because you did not log in, do this with session_start() :

session_start();
$usuario   = $_SESSION['usuarioSession'];
$senha     = $_SESSION['senhaSession'];

I also recommend that you make a if to check if the user has the session.

if(isset($usuario)){
    echo "<div class='bemvindo'>Bem Vindo! <strong>$usuario</strong> | Hoje é: ".date('d/m/Y')."</div>";
}
else{
    echo "Você não está logado!";
}
    
26.07.2017 / 22:05