2015-01-09 53 views
-1

我想檢查會話是否設置。而不是在所有的頁面檢查,我想其中包含的所有文件我如何檢查會話是否設置或不是從標題視圖

一個觀點叫的header.php檢查這個

我的頭視圖代碼是

<nav class="navbar navbar-default navbar-fixed-top"> 
<div class="container-fluid" style="background: #1A7A7B"> 
    <div class="navbar-header float-left"> 
     <a href="http://www.rafeeque.zaliyo.in/smp1/index.php/admin/admin_home"><h2 align="center" style="float:left;">School Management System</h2></a> 

     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".user-collapse"> 
      <span class="glyphicon glyphicon-user"></span> 

    </button> 
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".menu-collapse"> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 

    </button> 
    </div> 
    <div class="collapse navbar-collapse user-collapse"> 
     <ul class="nav navbar-nav navbar-right navbar-collapse"> 
      <li><a href="#"><span class="glyphicon glyphicon-user ">Profile</span></a></li> 
      <li><a href="#"><span class="glyphicon glyphicon-off">Logout</span></a></li> 
     </ul> 
    </div> 
</div> 
</nav> 

如何做到這一點。在此先感謝

+2

'if(isset($ _ SESSION [...]))' –

+0

這可能有助於: http://stackoverflow.com/questions/18286972/check-if-session-doesnt-exist-php –

回答

0

創建新的文件,名爲checkSession.php。寫這段代碼(修改您的需求)

<?php 
if (isset($_SESSION['foo'])) { 
    $foo = $_SESSION['foo']; 
} else { 
    $foo = null; 
} 

在每一個頁面現在你需要包括這個文件:

<!DOCTYPE html> 
<html> 
<?php include('checkSession.php'); ?> 
... 
0

如果你想設置驗證目的的會話,你可以使用這個

<?php 
if ($this->session->flashdata('notice')) 
{ 
    $data = $this->session->flashdata('notice'); 
    $class_str = $data['code'] === 0 
     ? 'class = "alert alert-error"' 
     : 'class = "alert alert-success"'; 
?> 
    <div <?=$class_str?>> 
     <button type="button" class="close" data-dismiss="alert">&times;</button> 
     <?=$data['message'];?> 
    </div> 
<?php 
    } 
?> 

或以其它方式爲用戶數據:

if ($this->session->userdata('data')) 
{ 
    //code 
}