Display username logged in

0

Example in code file index.php :

<header>

        <input type="checkbox" id="btn-menu">
        <label for="btn-menu" class="icon-menu"></label>
            <nav class="menu">
                <ul>
                    <li><a href="index.php"><span class="icon-home"></span>Home</a></li>
                    <li><a href="#"><span class="icon-info"></span>Sobre</a></li>
                    <li><a href="#"><span class="icon-calendar"></span>Agendamento</a></li>
                </ul>
            </nav>

                <button type="button" class="cadastro btn-login"><a href="login-cadastro.php">Login</a></button>
                <button type="button" class="cadastro btn-cadastro"><a href="login-cadastro.php">Cadastre-se</a></button>
        </header>

When the user logs in, he wants the Login and Sign-up options to disappear, and instead of those options, the name of the user is displayed.

I was thinking of replicating the index.php code to the index-logado.php (example), and opening a session in that new code, putting his name there.

Example in code index-logado.php :

session_start();
    <header>

            <input type="checkbox" id="btn-menu">
            <label for="btn-menu" class="icon-menu"></label>
                <nav class="menu">
                    <ul>
                        <li><a href="index-logado.php"><span class="icon-home"></span>Home</a></li>
                        <li><a href="#"><span class="icon-info"></span>Sobre</a></li>
                        <li><a href="#"><span class="icon-calendar"></span>Agendamento</a></li>
                    </ul>
                </nav>

            echo "BEM-VINDO". $_SESSION['nome-usuario'];

            </header>

I do not know if this is a correct way, it's the only one I thought, and I know so far.

If I had a condition that changed directly in the index.php would be the most practical solution to my view, but if it exists, I do not know it yet.

TRY N ° 1:

<?php session_start(); 
require_once("Valida.php");
?>

<header>

        <input type="checkbox" id="btn-menu">
        <label for="btn-menu" class="icon-menu"></label>
            <nav class="menu">
                <ul>
                    <li><a href="index.php"><span class="icon-home"></span>Home</a></li>
                    <li><a href="#"><span class="icon-info"></span>Sobre</a></li>
                    <li><a href="#"><span class="icon-calendar"></span>Agendamento</a></li>
                </ul>
            </nav>
                <?php 
                    if ($v->getValida()) {
                ?>
                    <span><?php echo "Bem-vindo ".$_SESSION['email'];  ?></span>


                <?php}else{?>


                <button type="button" class="cadastro btn-login"><a href="login-cadastro.php">Login</a></button>
                <button type="button" class="cadastro btn-cadastro"><a href="login-cadastro.php">Cadastre-se</a></button>

                <?php } ?>


        </header>   

Validate.php file

<?php 

 class Valida{


    private $valida;

    function getValida(){
        return $this->valida;
    }

    function setValida($valida){
        $this->valida = $valida;
    }
}

 ?>
    
asked by anonymous 05.04.2017 / 15:57

3 answers

1

What you can do is create two different types of "menu" one for when you are connected and another for when you are an unconnected visitor.

For example:

session_start();

if(isset($_SESSION['nome-usuario'])){
  include('conectado/menu.php');
}else{
  include('desconectado/menu.php');
}
    
05.04.2017 / 22:41
1

Well an easy way would be to "IFar" once you are using PHP, here is the suggestion: (change vars and tags for yours)

<header>
  <input type="checkbox" id="btn-menu">
  <label for="btn-menu" class="icon-menu"></label>
  <nav class="menu">
    <ul>
      <li><a href="index.php"><span class="icon-home"></span>Home</a></li>
      <li><a href="#"><span class="icon-info"></span>Sobre</a></li>
      <li><a href="#"><span class="icon-calendar"></span>Agendamento</a></li>
    </ul>
  </nav>
  <?php if ($usuario_status == 'LOGADO') { ?>
    <span><?php echo $usuario_nome;?></span>
  <?php } else { ?>
    <button type="button" class="cadastro btn-login"><a href="login-cadastro.php">Login</a></button>
    <button type="button" class="cadastro btn-cadastro"><a href="login-cadastro.php">Cadastre-se</a></button>
   <?php } ?>
</header>
    
05.04.2017 / 16:15
0

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

Note: disregard the last line div=principal

</head>
<body>
        <?php 
            include('includes/topoemenu.php');
            require_once('includes/db.php');

                $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'); ?>

</body>
</html>

Please, where am I going wrong?

    
26.07.2017 / 02:54