Print user name on page

Asked

Viewed 96 times

0

I have a login page that when the user logs in is redirected to a page called Minhaconta.php containing the following code:

                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $_SESSION['nome']; ?><span class="caret"></span></a>                      
                    <ul class="dropdown-menu">                          
                        <li><a href="MinhacontaPt-BR.php">&#128102; Perfil</a></li>                         
                        <li><a href="../Conexao/sair.php" class="fa fa-sign-out"> Sair</a></li>                         
                    </ul>

I can take his name and print it on this page, only when I go to the home page of my site, his name doesn’t print.

How can I do that?

When the user has logged in, in the initial part, appears "my account and quit" instead of "login and register". And, above, where my account is, appears the user name.

                     <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">&#128102; Minha conta<span class="caret"></span></a>                     
                    <ul class="dropdown-menu">                          
                    <li><a href="CadastrarPT-BR.php">Registrar-se</a></li>
                    <li><a href="loginPT-BR.php">Entrar</a></li>                        
                    </ul>   
                    </li>

I opened the session only on the page of Minhaconta.php.

  • Kd your home page code?!

  • <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" role="button" Aria-haspopup="true" Aria-Expanded="false">&#128102; My account<span class="Caret"></span></a> <ul class="dropdown-menu"> <li><a href="Register BR.php">Register</a></li> <li><a href="loginPT-BR.php">Login</a></li> </ul> </li>

  • 1

    @Pedrolukas Click on [Edit] and post your code.

  • There are more codes, I just sent this part because this is where I want it when the user is logged in and the name of it appears and instead of registering and logging in, my account appears and exit.

  • Peter, think that to help you, we need as little as possible to reproduce your problem. You explained it well, but we have no way of guessing what your code is like as a whole. You can [Edit] your question and add the code relating to it.

  • Sorry, I already sent the code there, see if you can understand better.

  • Still a little code. It will only generate more questions. Want to see?! Remember to log in to your homepage: session_start()?!

  • I mean, man it’s a lot of code bro, I don’t know how to send it around here and it will embolden you even more, yes, I opened the session only on the page of Minhaconta.php

  • Because it is a shop in this initial part, where there is Carousel, shop windows, etc. so if I send I think it will upset you and every time I try to send Uga k

Show 4 more comments

2 answers

0

I opened the session only on the Minhaconta.php page

According to the PHP documentation, the session should be started or resumed whenever accessing the information stored in it:

session_start() creates a session or summarizes the current session based on a session id passed via GET or POST, or passed via cookie. - PHP: session_start

So, start/summarize the session, too, on your homepage.

It is normal to sign in directly at the root of your project when you have a settings file common to all your pages/scripts. But this can be different for each project...

Another question: the session must be started before any code outputs. The following example will generate an error:

<?php
echo 'Hello world!';
session_start();

The correct is:

<?php
session_start();
echo 'Hello world';

0

You can add a condition on the home page that checks for a logged in user:

<?php if (isset($_SESSION['nome'])) { ?>

    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
        <?php echo $_SESSION['nome']; ?><span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="MinhacontaPt-BR.php">&#128102; Perfil</a></li>
        <li><a href="../Conexao/sair.php" class="fa fa-sign-out"> Sair</a></li>
      </ul>

<?php } else { ?>

    <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">&#128102; Minha conta<span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="CadastrarPT-BR.php">Registrar-se</a></li>
        <li><a href="loginPT-BR.php">Entrar</a></li>
      </ul>
    </li>

<?php } ?>

Don’t forget to start sessions (session_start();) on the necessary pages

  • Vlwww guy helped me!! <3

Browser other questions tagged

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