PatricF
PatricF

Reputation: 419

PHP: Get username from session

I'm not very good at PHP and I have a little problem. I've been playing around with this script.

And I can't for the life of me figure out how to echo the username of a logged in user. I tried to print all the information of the session like this:

var_dump($_SESSION)

but I just got the hashed password and the userlevel int.

Can someone maybe help me here? I just want to be able to echo the username.

Upvotes: 1

Views: 66471

Answers (5)

GriffLab
GriffLab

Reputation: 2166

You have to store the username in the session for it to be available on another page load, currently the script only stores these values in the session;

 $_SESSION['loggedin'] = $row[$this->pass_column];
 $_SESSION['userlevel'] = $row[$this->user_level];

What you have to do is add the $username to the session that is passed into the login function, like below;

$_SESSION['username'] = $username;

The username will now be stored in the session with the key username.

To be able to use it on another page, make sure that before attempting to use it you initiate the session by calling the function session_start().

Upvotes: 5

Vijaya Subramaniyan
Vijaya Subramaniyan

Reputation: 1

<?php
include('db.php');
session_start();
$name=$_POST['name'];
$password=$_POST['password'];
echo $sql="SELECT * FROM register WHERE (name='$name' OR email='$name') AND password='$password'";
$result=mysqli_query($conn,$sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0)
 {
    $_SESSION['user']=mysqli_fetch_assoc($result);
    $row = $_SESSION['user'];
    $role = $row['role'];

            if($role == 1)
            {
                header('location:usermanagement.php');
            }
            else{
                header('location:user.php');
            }

}
else
 {

    echo "Wrong Username or Password";
    header('location:login.php');
}
$conn->close();
?>

Upvotes: 0

Ben Branyon
Ben Branyon

Reputation: 172

In the login function you should write the username to the session after a successful login.

//instantiate if needed
include("class.login.php");
$log = new logmein();
$log->encrypt = true; //set encryption
if($_REQUEST['action'] == "login"){
    if($log->login("logon", $_REQUEST['username'], $_REQUEST['password']) == true){
         //do something on successful login
         $_SESSION['username'] = $_REQUEST['username'];
    }else{
         //do something on FAILED login
    }
}

Upvotes: 0

samayo
samayo

Reputation: 16495

Basically, just write it inside like

session_start();
echo $_SESSION['username']; 

or

 echo $_SESSION['password'];

A brief explanation of how sessions work.

first you start the session and assign any value to a session ex:

session_start();
$_SESSION['username'] = 'john';

then echoing works like:

echo $_SESSION['username']; // will echo out 'jonh'

note session_start() must be shared in-between the pages you want to use the session

Upvotes: 1

Nathan van der Werf
Nathan van der Werf

Reputation: 332

You have session_start(); on top ?

Upvotes: 0

Related Questions