刷新页面后会话启动头文件更改时

时间:2017-06-13 07:21:51

标签: php codeigniter

Codeigniter的新手。

这是我的头文件。登录后,需要更改头文件,但刷新页面后它会更改。当我退出时也是如此。请帮助。

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
</head>

<body>
  <nav class="navbar navbar-inverse">
    <div class="container-fluid">
      <ul class="nav navbar-nav">
        <li><a href=<?php echo base_url( 'form/home')?>>HOME</a></li>
        <li><a href=<?php echo base_url( 'form/about')?>>ABOUT</a> </li>
        <li><a href=<?php echo base_url( 'form/contact')?>>CONTACT</a></li>
      </ul>
      <?php
                        if(isset($_SESSION["name"])){
      ?>
        <ul class="nav navbar-nav navbar-right">
          <li><a href=<?php echo base_url( 'form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
          <li><a href=<?php echo base_url( 'form/logout')?>>Logout<span class="glyphicon glyphicon-log-out"></span></a></li>
        </ul>
        <?php
                        }else{
        ?>
          <ul class="nav navbar-nav navbar-right">
            <li><a href=<?php echo base_url( 'form/register')?>>REGISTER<span class="glyphicon glyphicon-user"></span></a></li>
            <li><a href=<?php echo base_url( 'form/login')?>>LOGIN<span class="glyphicon glyphicon-log-in"></span> </a></li>
          </ul>
          <?php
                        }
          ?>
    </div>
  </nav>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

更改if条件以创建具有名称的会话。

if($query -> num_rows() == 1) 
{ 
  $row = $query->row(); 
  $data = array('name' => $row->name); 
  $this->session->set_userdata('loggin_session',$data); // given a name to session(i.e loggin_session). 
  return true;
}    

现在,您的视图更改如下:

<?php 
     $session_data = $this->session->userdata('loggin_session');
     if(isset($session_data['name'])){ 
?> 
     <ul class="nav navbar-nav navbar-right">                  
            <li><a href=<?php echo  base_url('form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
            <li><a href=<?php echo  base_url('form/logout')?>>Logout<span class="glyphicon glyphicon-log-out"></span></a></li>
     </ul>
<?php }else{ ?> 
     <ul class="nav navbar-nav navbar-right">
           <li><a href=<?php echo  base_url('form/register')?>>REGISTER<span class="glyphicon glyphicon-user"></span></a></li>
           <li><a href=<?php echo  base_url('form/login')?>>LOGIN<span class="glyphicon glyphicon-log-in"></span> </a></li>
     </ul>
<?php } ?>