[Solved] Displaying username with $_SESSION[‘username’] [closed]


Your code checks, if value of key username of superglobal array SESSION isn’t empty, and if so, then it saves it’s value into variable $username.
If you want to display it’s value, you can use

echo($_SESSION["username"]);

to directly get it from SESSION or, when setting username from session to variable $username (as in your code) you can also use

echo($username);

//EDIT

As other people mentioned, you have to start session at start of each file you want to use it with function session_start();

Then you can save data or read data from it, as from any other array.
For more information about arrays (if you don’t know, how they work) you can visit W3Schools – PHP arrays

For example:

session_start();

//setting value "Ben" into session array to key "username"
$_SESSION["username"] = "Ben";

//displaying value of key "username" from session
echo($_SESSION["username"]);
    //output: Ben

For more information about session you can visit W3Schools – PHP session

4

solved Displaying username with $_SESSION[‘username’] [closed]