I was wondering how would I display a logged in members users name on every page using PHP SESSIONS?
2 Answers
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'];
1 Comment
the_machinist_
I wouldn't var_dump() anything unless you are having a ghost of a problem with the session. Nothing is going to magically appear in there. You may want to reference the PHP manual on this one by the way: php.net/manual/en/session.examples.basic.php
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']