php php登录成功后如何显示用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27218773/
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
How can I display a user's username after successful php login?
提问by Will
So I cannot seem to get this to work following other peoples answers. So I have my files set up like this:
因此,我似乎无法按照其他人的答案进行操作。所以我的文件设置如下:
- Index
- Register
- Config
- Home
- Logout
- 指数
- 登记
- 配置
- 家
- 登出
config.php
配置文件
<?php
// These variables define the connection information for your MySQL database
$username = "XXX";
$password = "XXX";
$host = "XXX";
$dbname = "XXX";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
$db = new PDO("mysql:host={$host}; dbname={$dbname}; charset=utf8",
$username, $password, $options);
} catch (PDOException $ex){
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
header('Content-Type: text/html; charset=utf-8');
session_start();
?>
And in my home.php
, which is the page it will redirect you to after a successful login I want it to show the username, like, "Hello, {username}"... However I am having some trouble achieving this.
在 my 中home.php
,这是成功登录后将您重定向到的页面,我希望它显示用户名,例如“您好,{用户名}”……但是我在实现这一点时遇到了一些麻烦。
home.phpwere I want the username to show:
home.php我希望用户名显示:
<ul class="nav navbar-nav navbar-right">
<li class="divider-vertical"></li>
<li><a href="#">Hello, {USERNAME HERE!!}</a></li>
<li><a href="logout.php">Log Out</a></li>
</ul>
Where '{USERNAME HERE!!}' would be some php code that would allow the username to be shown there.
其中 '{USERNAME HERE!!}' 将是一些允许在那里显示用户名的 php 代码。
I just cannot for the life of me figure out how to get it to show the username of the person who signed up.
我一生都无法弄清楚如何让它显示注册人的用户名。
采纳答案by mrahmat
on your config.php page:
在您的 config.php 页面上:
$_SESSION["username"] = "xxx";
on your home.php start a session as
在你的 home.php 上开始一个会话
session_start();
and wherever you want to put the username:
以及您想要放置用户名的任何位置:
<?php echo $_SESSION["username"]; ?>
in your case most probably will be :
在你的情况下很可能是:
<li><a href="#">Hello, <?php echo $_SESSION["username"]; ?></a></li>
回答by Friso van Dijk
To expand on my comment on your post:
You want to start the session on the top of each page using session_start()
at the top of each page.
If on another page you set the variable $_SESSION['username']
, you can call that.
扩展我对您的帖子的评论:您想在每个页面session_start()
的顶部使用在每个页面的顶部开始会话。如果在另一个页面上设置了变量$_SESSION['username']
,则可以调用它。
The following code will show a login form if the user is logged in, else it'll show other stuff:
如果用户已登录,以下代码将显示登录表单,否则将显示其他内容:
<?php session_start() ?>
<ul class="nav navbar-nav navbar-right">
<li class="divider-vertical"></li>
<?php
if(isset($_SESSION['username'])) {
echo '<li><a href="#">Hello,'. $_SESSION["username"] . '</a></li>';
echo '<li><a href="logout.php">Log Out</a></li>';
} else {
//put login form or include here.
}
?>
</ul>
I also saw the suggestion of $_GET[x], but that isn't the best option. $_GET[x] is used to get variables from link (i.e. index.php?username=Foo), where $_GET['username'] gives Foo. This is useful if you want to sent some information from 1 page to another, but not great for usernames, as they are persistent across the session and the links can be edited.
我也看到了 $_GET[x] 的建议,但这不是最好的选择。$_GET[x] 用于从链接获取变量(即 index.php?username=Foo),其中 $_GET['username'] 给出 Foo。如果您想将一些信息从一个页面发送到另一个页面,这很有用,但对于用户名来说不是很好,因为它们在整个会话中是持久的,并且可以编辑链接。
Also I would like to say that my example isn't very great unless you unset the username when the user logs out. It's better to have a boolean logged_in or anything like it keep track of whether the user is logged in and then proceed to check if the session has a username.
另外我想说的是,除非您在用户注销时取消设置用户名,否则我的示例并不是很好。最好有一个布尔值 login_in 或类似的东西来跟踪用户是否登录,然后继续检查会话是否有用户名。
回答by Will
I ended up resolving my own question with a very simple solution. I both love and hate when this happens...
我最终用一个非常简单的解决方案解决了我自己的问题。当这种情况发生时,我又爱又恨……
All i did was this:
我所做的就是这样:
<?php session_start(); ?>
<ul class="nav navbar-nav navbar-right">
<li class="divider-vertical"></li>
<li><a href="#">Hello, <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?></a></li>
<li><a href="logout.php">Log Out</a></li>
</ul>
So all I really did was add:
所以我真正做的就是添加:
<?php session_start(); ?>
and
和
<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>