Display logged in user name

Asked

Viewed 7,670 times

0

Example in file code 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 Register options to disappear, and instead of these options appear the name of who logged in.

I was thinking of replicating the code of index php. to the index-logated.php(example), and open a session in that new code by placing its name there.

Example in file code index-logated.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 don’t know if this is the right way, it’s the only way I thought, and I know so far.

if I had a condition that would change directly in index.php would be the most practical solution in my view, but if it exists, I don’t have the knowledge of it yet.

ATTEMPT 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>   

File Valida.php

<?php 

 class Valida{


    private $valida;

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

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

 ?>
  • You can do it through if and Else to check that the $_SESSION['user-name'] is empty or set and then display the user name if the $_Session is true, you can do it all on the same page without the need for you to replicate the code

  • Saul da Silva Rolim, the user has already been authenticated. ’ll take step by step 1° The user logs in.php 2° Authenticate in.php 3° Redirect from Authenticate.php to index.php if there is a condition that changes the index.php file to place the session, or if it is done the way I did above, and if you give to change the file, how it is done.

  • I understood what you meant Saul da Silva Rolim, was not seeing this possibility of condition next to html, Thank you.

  • you managed to make?

  • not yet, but I know where the problem is, just trying to figure out how to fix it

  • I’m not able to do it yet, I edited the post, I put the files I’m trying. getValida() arrives in the validation file, displays, but in the header file it displays nothing. /// Something else, besides not reaching, my condition displays the 2, both the buttons and the welcome, if I reverse the condition it removes the 2(button is welcome)

  • A session will only cease to exist if you have destroyed it or it expires. Then just check if the session variable is set and display the information (assuming you stored the user name in the session)

  • I solved the problem, my Else was next to the <?php}Else tag{?>

Show 3 more comments

3 answers

1

What you can do is create two different types of "menu" one for when you’re logged in and the other for when you’re an uninlogged visitor.

For example:

session_start();

if(isset($_SESSION['nome-usuario'])){
  include('conectado/menu.php');
}else{
  include('desconectado/menu.php');
}
  • my validation file already does the redirect, the problem is that when it redirects to index.php appears the name of the logged in user, and the login and registration buttons, I want when it logs in, the buttons come out.

  • I edited the answer.

1

Good an easy way would be "Ifar" since you are using PHP, follows below the suggestion : (exchange vars and tags for your)

<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>
  • Interesting, I did not think of this solution, I will test here and give the feedback, worth carlosgaddini. ^^

  • Well the Phpurists may not like my suggestion, but it is easy to understand that it was my goal. Anything ask again.

  • I’m having difficulty to make the value of validation, get in my IF condition, I edited the post and put what I’m doing, for some reason the value I pass is not getting in my IF condition //// Something else, besides not getting, my condition displays the 2, both the buttons and the welcome, if I reverse the condition she removes the 2(button is welcome)

  • I solved the problem.

0

I have this file (index.php) where I want to display the name of the logged-in user on the screen after their login, but this giving an undefined variable error ( $_SESSION).

Observing: 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 I’m going wrong?

  • 1

    Welcome, consider taking a [tour] to get the most out of the site. You used the answer field to ask... Good Luck

Browser other questions tagged

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