How to display a users name using PHP SESSIONS?
I was wondering how would I display a logged in m开发者_运维知识库embers users name on every page using PHP SESSIONS?
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'];
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']
精彩评论