echo $_SESSION['username'];
I'm trying to see the current logged in username, but its not showing anything.
echo $_SESSION['username'];
I'm trying to see the current logged in username, but its not showing anything.
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
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'];
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. :)