alpha
alpha

Reputation: 29

How to display a users name using PHP SESSIONS?

I was wondering how would I display a logged in members users name on every page using PHP SESSIONS?

Upvotes: 0

Views: 9256

Answers (2)

Rob
Rob

Reputation: 1885

The user data has to be stored somewhere in the $_SESSION global. Whether or not the name is included, depends on your scripts.

I would do a var_dump($_SESSION) to see exactly what is stored in the session, then use that information to check if the name exists in the session, and if not, query the database for it and store it in the session.

if (!isset($_SESSION['User']['name']) {
   $q = mysql_query("SELECT name FROM users WHERE id = $_SESSION['User']['id']");
   $r = mysql_fetch_row($q);
   $_SESSION['User']['name'] = $r[0];
}

echo 'Hello, '.$_SESSION['User']['name']

Upvotes: 1

the_machinist_
the_machinist_

Reputation: 455

Put the members username into a session variable. I haven't personally touched any PHP in a while but you usually use the $_SESSION[] super global array.

echo $_SESSION['username'];

Upvotes: 2

Related Questions