0

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

2 Answers 2

2

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'];

Sign up to request clarification or add additional context in comments.

1 Comment

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
1

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.