PHP:从会话获取用户名 [英] PHP: Get username from session

查看:112
本文介绍了PHP:从会话获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太擅长PHP,但有一个小问题.我一直在玩此脚本.

I'm not very good at PHP and I have a little problem. I've been playing around with this script.

我一生都无法弄清楚如何回显已登录用户的用户名. 我试图像这样打印会话的所有信息:

And I can't for the life of me figure out how to echo the username of a logged in user. I tried to print all the information of the session like this:

var_dump($_SESSION)

但是我只是得到了哈希密码和用户级别的整数.

but I just got the hashed password and the userlevel int.

有人可以在这里帮助我吗?我只想能够回应用户名.

Can someone maybe help me here? I just want to be able to echo the username.

推荐答案

您必须将用户名存储在会话中,才能在另一个页面加载时使用,当前脚本仅将这些值存储在会话中;

You have to store the username in the session for it to be available on another page load, currently the script only stores these values in the session;

 $_SESSION['loggedin'] = $row[$this->pass_column];
 $_SESSION['userlevel'] = $row[$this->user_level];

您要做的是将$username添加到传递给login函数的会话中,如下所示;

What you have to do is add the $username to the session that is passed into the login function, like below;

$_SESSION['username'] = $username;

用户名现在将与密钥用户名一起存储在会话中.

The username will now be stored in the session with the key username.

要在其他页面上使用它,请确保在尝试使用它之前,您可以通过调用函数session_start()来启动会话.

To be able to use it on another page, make sure that before attempting to use it you initiate the session by calling the function session_start().

这篇关于PHP:从会话获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆