Question

   <?php 
session_start();
if(isset($_SESSION['login']))
{

 include_once('includes/header.php'); ?>
 <!DOCTYPE html>
<html>
<body>
    <div id="mainframe">
        <img src="img/header.png">
        <div id="menu">
            <?php include_once('includes/navbar.php'); ?>
        </div>
        <div id="content">
            <h3>Shopping Cart</h3>
        </div>
    </div>
    <?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
    else
    {
    header('location: login.php');
    }
?>

Here is my small PhP code I've got at the moment, my login session is $_SESSION['login']. And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out. Does anyone know a simple method / solution for this?

Was it helpful?

Solution 2

U need to pass username using SESSION variable for the same

write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.

   $query = "SELECT name FROM users WHERE password='$password'";
   $username = mysql_result(mysql_query($query),0);

   $_SESSION['username'] = $username;

than proceed as you are doing

<?php 
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}

OTHER TIPS

Put this somewhere in your if statement. It will show Logged in as User at right top corner of page

<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top