如何显示访客和用户帐户名称

时间:2017-06-20 07:18:16

标签: php html

我目前正在开发本网站的登录系统。我想要发生的是,您可以作为客户访问该网站您也可以登录帐户。 (管理员作为我的例子)

如果您访问过该网站,我想在页面右上角显示 Guest 。但是,如果您从帐户登录,则也会从右上角显示用户名。 (我已经可以显示用户名了。)我使用了switch语句。

Guest 在那里,但我想隐藏这些通知消息。 我只想隐藏这些通知 enter image description here enter image description here

(^用户名显示) 这是代码:     

if(isset($_SESSION['started']) && $_SESSION['started']) {

}
?>

<li role="presentation" class="dropdown pull-right">
<a class="dropdown-toggle"  title="Profile" data-toggle="dropdown" href="" 
role="button" aria-haspopup="true" aria-expanded="true">
  <span class="glyphicon glyphicon-user" style="cursor: pointer;" aria-
hidden="true"> <span class="caret"></span>
</a>
<ul class="dropdown-menu">
  <li role="presentation"><a href="">Settings</a></li>
  <li role="presentation"><a href="logout.php">Log out</a></li>
</ul>
 </li>

  <li class="pull-right"><a href="profile.php" title="Profile"><?php

   //echo  "";
switch ($_SESSION['user']) 
{
  case ' "'.$_SESSION['user'].'" ' =="":  //To display Guest
echo "Guest"; 

break;

case ' "'.$_SESSION['user'].'" ' != "":  //To display your username
echo $_SESSION['user'];
break;

default:
echo "INVALID";
break;
}

?></a></li>

3 个答案:

答案 0 :(得分:1)

试试这个

if(isset($_SESSION['user'])){
echo 'Hello, '.$_SESSION['user'];
}else{
echo 'Hello, Guest';
}

答案 1 :(得分:0)

您应该检查会话是否已设置。

if(isset($_SESSION['user'])){
    switch ($_SESSION['user'])  {
        case ' "'.$_SESSION['user'].'" ' =="":  //To display Guest
            echo "Guest"; 
            break;

        case ' "'.$_SESSION['user'].'" ' != "":  //To display your username
            echo $_SESSION['user'];
            break;

        default:
            echo "INVALID";
            break;
    }
}

答案 2 :(得分:0)

首先检查用户会话集。

尝试以下,

 "timetable": {
    "teachers": {
        "762": "sfsdfsdf",

    },
    "hours": {
        "1": "09:00",
    },
    "rooms": {
        "439": "sdfsdfsdf",
    },
    "subjects": {
        "738": "sdfsdfdsf",
    },
    "entries": {
        "10": [
            {
                "week": "1",
                "day": "6",
                "date": "2017-03-01",
                "hour": "1",
                "type": "0",
                "course": "3844",
                "teacher": "59502",
                "room": "640",
                "p": "-1",
                "table": "1447"
            },
            {
                "week": "1",
                "day": "6",
                "date": "2017-03-01",
                "hour": "2",
                "type": "0",
                "course": "4047",
                "teacher": "50792",
                "room": "799",
                "p": "-1",
                "table": "1447"
            }
        ]
    }

您也可以从PHP中删除通知。

<?php
session_start();
if(isset($_SESSION['user'])){
    switch ($_SESSION['user']) 
    {
        case $_SESSION['user'] == "":  //To display Guest
            echo "Guest"; 
            break;
        case $_SESSION['user'] != "":  //To display your username
            echo $_SESSION['user'];
        break;
        default:
            echo "INVALID";
        break;
    }
}
else{
    echo "INVALID";
}
?>