简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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