-4
echo $_SESSION['username'];

I'm trying to see the current logged in username, but its not showing anything.

Saud
  • 17
  • 1
  • 8
  • Have you started the session? Are those values stored properly to the session? – Sougata Bose Apr 25 '17 at 11:07
  • try `session_start()` just after opening php tag. – Priyesh Kumar Apr 25 '17 at 11:08
  • 1
    `username` is not a core PHP feature. Data won't be there unless you (or your framework) put it there. – Álvaro González Apr 25 '17 at 11:08
  • Please step to [HELP center](http://stackoverflow.com/help), then visit [GET started](https://meta.stackoverflow.com/questions/252149/how-does-a-new-user-get-started-on-stack-overflow), and finally, read [How to Ask Question](http://stackoverflow.com/help/how-to-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). As we're **not** mind-readers (AFAIK), please show us some code... – OldPadawan Apr 25 '17 at 11:08
  • Yes of course I started session at the top – Saud Apr 25 '17 at 11:11
  • @Saud seriously asked poorly, this is not the way to ask a question on SO – lazyCoder Apr 25 '17 at 11:14

3 Answers3

1

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.

<?php
// Starting session
session_start();
?>

The session_start() function first checks for an existing session ID. If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn't, it starts a new session by creating a new session ID.

Storing and Accessing Session Data

You can store all your session data as key-value pairs in the $_SESSION[] superglobal array. The stored data can be accessed during lifetime of a session. Consider the following script, which creates a new session and registers two session variables.

<?php
// Starting session
session_start();

// Storing session data
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>

To access the session data we set on our previous example from any other page on the same web domain — simply recreate the session by calling session_start() and then pass the corresponding key to the $_SESSION associative array.

<?php
// Starting session
session_start();

// Accessing session data
echo 'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"];
?>

The PHP code in the example above produce the following output.

Hi, Peter Parker

Med Dhiia
  • 64
  • 4
0

first start the session and store the user name in the session and then get it for example:

seesion_start();
$_SESSION['user_name'] = 'xyz'; //save username in session
//to get the user name after saving
$userName = $_SESSION['user_name'];
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
jerry
  • 1
  • 2
-1

First, you need to start the session. Before any HTML put:

<?php
session_start(); //With this PHP function you start a session
?>

Then, for example make a HTML Form:

<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
    <input type="text" placeholder="Username" name="username">
    <input type="password" placeholder="Password" name="password">
    <input type="submit" name="submit">
</form>
</body>
</html>

After that set session variables:

<?php
if (isset($_POST["submit"]))
{
    $_SESSION["username"] = $_POST["username"]; //Save the data from the 
    $_SESSION["password"] = $_POST["password"]; //form to the $_SESSION variables
}
?>

And finally the code will look like:

<?php
session_start(); //Session started
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
    <input type="text" placeholder="Username" name="username">
    <input type="password" placeholder="Password" name="password">
    <input type="submit" name="submit">
</form>
<?php
if (isset($_POST["submit"]))
{
    $_SESSION["username"] = $_POST["username"]; //Set username
    $_SESSION["password"] = $_POST["password"]; //Set password
}
echo $_SESSION["username"]; //Output the session username
?>
</body>
</html>

I hope i helped you less or more. :)

XTard
  • 56
  • 8