如果用户已登录,是否可以使用PDO或Smarty更改导航栏?

时间:2019-12-27 21:58:38

标签: php html pdo smarty

我正在使用Smarty和PDO创建一个网站,这是我的头文件,并且我不确定如何根据用户是否登录来更改它?

当用户未登录时,应显示这些选项(周五,周六,周日,注册,登录) 但是,当他们登录时,我希望“注册”和“登录”更改为“我的帐户”和“退出”。以下是我目前拥有的代码。

//If user is logged in

<div id="navigation"> <ul class="nav" id="navigation">
      <nav class="navbar navbar-expand-lg">
  
  <button class="navbar-toggler" id="hideshow" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"><i class="far fa-caret-square-down"></i></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=main">Home <i class="far fa-arrow-alt-circle-right"></i><span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=friday">Friday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=saturday">Saturday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=sunday">Sunday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>

  <li class="nav-item">
    <a class="nav-link" href="index.php?p=logout" id="Logout" />Logout <i class="fas fa-sign-out-alt"></i></a>
  </li>

<li class="nav-item">
    <a class="nav-link" href="index.php?p=account">My account <i class="far fa-arrow-alt-circle-right"></i></a>
  </li>

//If user isn't signed in


  <div id="navigation"> <ul class="nav" id="navigation">
      <nav class="navbar navbar-expand-lg ">
  
  <button class="navbar-toggler" id="hideshow" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"><i class="far fa-caret-square-down"></i></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=main">Home <i class="far fa-arrow-alt-circle-right"></i><span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=friday">Friday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=saturday">Saturday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="index.php?p=sunday">Sunday <i class="far fa-arrow-alt-circle-right"></i></a>
      </li>

  <li class="nav-item">
    <a class="nav-link" href="index.php?p=signup" id="signup" />Signup <i class="fas fa-user-plus"></i></a>
  </li>


<li class="nav-item">
    <a href="index.php?p=login" id="Login"/>Login <i class="fas fa-sign-in-alt"></i></a>
</li></ul>
  {/if}
</div>
      
      
  </nav></div>

2 个答案:

答案 0 :(得分:0)

首先,将Controller中的变量分配给您的视图,例如:

$view->assign('USER_ID', $this->auth->getUserId());

然后在模板中:

    {if $USER_ID > 0}
    {* html or template when user is logged in *}

    {* if differences are big, better is create new sub-template: *}
    {include file='./header-logged-in.tpl'}

    {else}
    {* html or template when user is not logged in *}
    {include file='./header-logged-out.tpl'}
    {/if}

答案 1 :(得分:0)

if(isset($_SESSION['user_id'])) {
// User Logged In Navigation
} else {
// User Logged Out Navigation
}

当然,您没有提供足够的信息,这是假设您的用户会话设置为$_SESSION['user_id'] <-更改为您的会话另存为。