tags:

views:

66

answers:

5

for some r.eason I cant display a logged in users name when they are logged in? the code is below

<?php
if (isset($_SESSION['user_id'])) {

echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>'; 
if ($_SESSION['user_level'] == 1) {
 echo 'something else';
}
 } else { echo 'something';
}
?>

Thanks every one but i solved it.

+1  A: 

Ack! Just look at your code. Do you know what this line is doing?

echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';

That's so wrong I don't even know where to begin. Just try

echo $_SESSION['first_name'];

And see if that gets you closer to what you want ;)

Mark
No it didn't work at all.
Then print_r($_SESSION) and see if the username is even set.
Mark
A: 
NawaMan
+1  A: 

Make sure you're also calling session_start() before trying to access the variables.

cshaiku
+1  A: 

Change your code to:

<?php
session_start();
if (isset($_SESSION['user_id'])) {
  if (isset($_SESSION['first_name'])) {
    echo ", " . $_SESSION['first_name']} . '!';    
  if ($_SESSION['user_level'] == 1) {
    echo 'something else';
  }
} else {
  echo 'something';
}
?>
cletus
A: 

Here are the list of possibilities of the mistakes and make sure that you have corrected them

1) have you set the cookie "first_name" using setcookie method...?

2) Then have u called the *session_start()* function so that the session variables can be called in that page??

3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Nikhil Ah
`first_name` is not a cookie. It's a session variable. If you're nesting variables with single quotes in a double quoted string, you *should* indeed wrap them in "flower" brackets, ie, echo "x = {$_SESSION['x']}";
Mark