我需要显示登录用户的名字。但是我做了一个问题
如此。
"You have been successfully logged in as <?php echo $_SESSION['username']; ?>. "
但用户名似乎显得空白..我只是以。登录。
答案 0 :(得分:2)
你有没有......
session_start()
?如果是,您可能需要向我们提供更多代码。
答案 1 :(得分:0)
"You have been successfully logged in as <?php echo $_SESSION['username']; ?>. "
更改为(试一试)
echo "You have been successfully logged in as
$_SESSION['username']  "
+++更多
首先,您需要使用session_start()
函数启动会话。请注意,此函数should be called before any output is generated!
此函数初始化$ _SESSION超全局数组,您可以在其中存储数据。例如,让我们在会话中存储用户名。你可以这样做:
session_start();
$_SESSION['username'] = 'ineedhelp';
"You have been successfully logged in as <?php echo $_SESSION['username']; ?>. "
您必须确保您的会话不是clean or destroy
,而是阻止您的代码
您是否已使用此行销毁
//...more your code
unset($_SESSION['username']);//Check if you clean before output
"You have been successfully logged in as <?php echo $_SESSION['username']; ?>. "
//...more your code
Chekc your phpinfo()
如果您在输出中向下滚动,您会找到一个包含会话设置的完整部分:
session
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
答案 2 :(得分:0)
您是否试图查看HTML页面源代码? ; - )
你试图把PHP代码放在字符串内容中 显然它不起作用。
echo "You have been successfully logged in as ".htmlspecialchars($_SESSION['username']).". "
会起作用。
或者你可以这样做
?>
You have been successfully logged in as
<?php echo htmlspecialchars($_SESSION['username']); ?>. 
<?
答案 3 :(得分:0)
嗯,您必须在这两个页面上都有session_start()
,即您设置或获取$_SESSION['username']
之前设置或获取它们。
并尝试print_r($_SESSION)
(它将有助于调试)检查username
是否设置session_start
如果有警告消息也检查日志。