Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
The script you linked to does't save the username in as a session variable. If you want that, you have to add it. – Barmar May 20 '13 at 20:41
up vote 2 down vote accepted

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().

share|improve this answer
    
Wow, that was a really fast answer. This worked perfectly! Thank you very much. – PatricF May 20 '13 at 20:49
    
@PatricF No problem if it helped you consider accepting the answer as it will help others in the future – GriffLab May 20 '13 at 20:52

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

share|improve this answer
    
That doesn't give me anything.. – PatricF May 20 '13 at 20:42
    
@PatricF Check my answer – GriffLab May 20 '13 at 20:42
    
It it does not give you anything, the you have not assigned a session yet – samayo May 20 '13 at 20:43
1  
@PatricF: when responding to answers, you need to give them something to work with. For example, you could link to an external copy of your script as it now stands. Otherwise, how is this answerer meant to respond? – halfer May 20 '13 at 20:43

You have session_start(); on top ?

share|improve this answer
    
Yes I have that on top. I get the information from the session, there's just no information on the username.. – PatricF May 20 '13 at 20:43
    
so if you do the following: session_start(); $_SESSION['username'] = 'Test'; echo $_SESSION['username']; it will return nothing ? – Nathan van der Werf May 20 '13 at 20:44

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
    }
}
share|improve this answer
<?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();
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.