简体   繁体   English

我如何获取会话开始用户名以回显

[英]How can i get the session start username to echo out

Iam having issues echoing the session username and have no idea were to start any more cause every thing i have tried keeps saying string to array conversion but idk how to get this to correctly work. Iam在回显会话用户名时遇到问题,并且不知道要启动任何原因,因为我尝试过的每件事都不断说字符串到数组的转换,但是idk如何使它正确工作。 here is the code that would check for the loged in saying you logged in would you like to log out. 这是用于检查登录信息的代码,表明您已登录并要注销。

 if(isset($_SESSION['username'])&& !empty($_SESSION['username'])){
    echo "<P id='loged'>your loged in would you like to log out</p>";

    echo'<a href="logout.php">log out</a>';





}else{
    include 'logform.php';
}

您可能想在$ _SESSION变量可用之前调用session_start()

您只想回显用户名?

echo "Hello " . $_SESSION['username];

I suppose the following answers your question... 我想以下回答您的问题...

echo $_SESSION['username'];

...but you will also want to use session_start() first. ...但是您还需要先使用session_start()。

Also, no need to use isset() and !empty(), as empty() checks if it is set first. 另外,不需要使用isset()和!empty(),因为empty()会检查是否首先设置了它。 All you really need is: 您真正需要的是:

if(!empty($_SESSION['username']))

You forgot to mention session_start() in the begining. 您忘了在开始时提到session_start()

session_start(); //add this in the begining of the file.


echo $_SESSION['username'];  //this will display username

Try this: 尝试这个:

call the function session_start() at beginning of the page and then check the $_SESSION array using

echo '<pre>';
print_r($_SESSION);

after that you see all the indexes set in session variable.
  • Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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