PHP:从会话中获取用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16657968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Get username from session
提问by PatricF
I'm not very good at PHP and I have a little problem. I've been playing around with this script.
我不是很擅长 PHP,我有一个小问题。我一直在玩这个脚本。
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.
但我刚刚得到了散列密码和用户级别 int。
Can someone maybe help me here? I just want to be able to echo the username.
有人可以在这里帮助我吗?我只想能够回显用户名。
回答by GriffLab
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];
What you have to do is add the $username
to the session that is passed into the login
function, like below;
您需要做的是将$username
传递给login
函数的会话添加到会话中,如下所示;
$_SESSION['username'] = $username;
The username will now be stored in the session with the key username.
用户名现在将与密钥用户名一起存储在会话中。
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()
.
为了能够在另一个页面上使用它,请确保在尝试使用它之前通过调用函数来启动会话session_start()
。
回答by samayo
Basically, just write it inside like
基本上,只要把它写在里面
session_start();
echo $_SESSION['username'];
or
或者
echo $_SESSION['password'];
A brief explanation of how sessions work.
会话如何工作的简要说明。
firstyou start the session and assign any value to a session ex:
首先,您启动会话并为会话分配任何值,例如:
session_start();
$_SESSION['username'] = 'john';
then echoing works like:
然后回声的工作方式如下:
echo $_SESSION['username']; // will echo out 'jonh'
notesession_start()
must be shared in-between the pages you want to use the session
注意session_start()
必须在您要使用会话的页面之间共享
回答by Nathan van der Werf
You have session_start(); on top ?
你有 session_start(); 在上面 ?
回答by Ben Branyon
In the login function you should write the username to the session after a successful login.
在登录功能中,您应该在成功登录后将用户名写入会话。
//instantiate if needed
include("class.login.php");
$log = new logmein();
$log->encrypt = true; //set encryption
if($_REQUEST['action'] == "login"){
if($log->login("logon", $_REQUEST['username'], $_REQUEST['password']) == true){
//do something on successful login
$_SESSION['username'] = $_REQUEST['username'];
}else{
//do something on FAILED login
}
}
回答by Vijaya Subramaniyan
<?php
include('db.php');
session_start();
$name=$_POST['name'];
$password=$_POST['password'];
echo $sql="SELECT * FROM register WHERE (name='$name' OR email='$name') AND password='$password'";
$result=mysqli_query($conn,$sql);
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0)
{
$_SESSION['user']=mysqli_fetch_assoc($result);
$row = $_SESSION['user'];
$role = $row['role'];
if($role == 1)
{
header('location:usermanagement.php');
}
else{
header('location:user.php');
}
}
else
{
echo "Wrong Username or Password";
header('location:login.php');
}
$conn->close();
?>