简体   繁体   English

如何使用PHP SESSIONS显示用户名?

[英]How to display a users name using PHP SESSIONS?

我想知道如何使用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. 我有一段时间没有亲自接触过任何PHP,但是您通常使用$ _SESSION []超级全局数组。

echo $_SESSION['username']; echo $ _SESSION ['username'];

The user data has to be stored somewhere in the $_SESSION global. 用户数据必须存储在$ _SESSION全局变量中的某个位置。 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. 我将执行var_dump($_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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM