I want to assign the user name to a SESSION variable, yet this is not working.

$_SESSION['loveleytest'] = $user;

$_SESSION['thelovelytest2'] = $user->name;

What should I do?

closed as off-topic by kiamlaluno Nov 28 '15 at 10:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I do not see a point in assigning global varaibles to session. You have access to global $user already at every page load. – arpitr Jun 3 '15 at 21:05

For assigning user_name to a session variable you would directly do something like

global $user;
$_SESSION['thelovelytest2'] = $user->name;

I think the issue with your code may be:

  1. It is not able to retrieve the global $user variable.
  2. You might not have defined the global $user variable.
  3. Because you did $_SESSION['loveleytest'] = $user, thats why the issue is there.

I am also curious to know which was the exact issue.

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