How to print the user name after logging into the site, only on other pages

Asked

Viewed 45 times

-1

So I want to do the following, I want when a user on my site accesses his account, his name appears on a particular site of the site, that I know how to do, I take his session and print, like this: $_Session['name'];. Only that I want to do the same in other parts of my site, how can I do?

  • Tried to use session_start() on the pages you want that data to be available? This function will make available on the global $_SESSION the domain session data on the current page.

  • Yes, but I do this and gave, only I want his name to appear on another page, ex: when the user enters he goes to a specific page for him, I want when he goes back to the home page of the site appear his name, got?

1 answer

2

Hello, You just need to start the section on the page and give the print command in the desired location as in the example:

 <?php

if (session_status() !== PHP_SESSION_ACTIVE) {
  session_start();
}//inicia a seção

$_SESSION['name'] = $nome_do_usuario;

?>

    <!DOCTYPE html>

    <html>
    <body>
    <div> <?php echo $_SESSION['name'] ?> </div>
    </body>
    </html>

This way you can access any value saved in your $_SESSION !

  • So buddy, I tried this and it’s not going to, like, I think you got it wrong, look at this, when the user logs into his account he’s redirected to a page called Minhaconta and on this page takes his session and shows up his good name on the page, only when I go back to the home page, I can’t get his name, and I put this index error code because of the name.

  • 1

    Are you saving his name in $_SESSION? If you are, you can go to any other page.

  • Yeah, I got what I wanted ;)

Browser other questions tagged

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