2013-11-20 274 views
0

我已經放置了2個控制器登錄和site1.When我登錄並進入會員區查看頁面,如果我複製該URL並放置在另一個tab.it打開。但我的目的是顯示你沒有權限訪問該頁面。登錄身份驗證

控制器:登錄

<?php 

class Login extends CI_Controller { 

    function index() 
    { 
     $data['main_content'] = 'login_form'; 

     $this->load->view('includes/template', $data);  

    } 
    function inactive() 
    { 
    echo"<script>alert('In active user Please contact the administrator');</script>"; 
    $this->load->view('login_form'); 
    } 
    function invalid() 
    { 
    echo"<script>alert('Invalid username or password');</script>"; 
    $this->load->view('login_form'); 
    } 
    function validate_credentials() 
    {  
     $this->load->model('membership_model'); 
     $query = $this->membership_model->validate(); 

     if($query) // if the user's credentials validated... 
     { 
      $data = array(
       'username' => $this->input->post('username'), 
       'is_logged_in' => true 
      ); 
      if($query->num_rows()>0){ 
      $status = $query->row()->account_status;} 
      else { 
      $status = ''; } 
      //Account active 
      if($status == 'active') 
      { 
       $this->session->set_userdata($data); 
       redirect('site1/members_area'); 
      } 
      else if ($status == 'inactive')//Account In active 
      { $this->inactive(); 
       } 
       else // incorrect username or password 
     { 
      $this->invalid(); 
     } 
     } 

    } 

    function signup() 
    { 
     $data['main_content'] = 'signup_form'; 
     $this->load->view('includes/template', $data); 
    } 

    function create_member() 
    { 
     $this->load->library('form_validation'); 

     // field name, error message, validation rules 
     $this->form_validation->set_rules('first_name', 'Name', 'trim|required'); 
     $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); 
     $this->form_validation->set_rules('college_name', 'college_name', 'trim|required'); 
     $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email'); 
     $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]'); 
     $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
     $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 
     $this->load->helper('date'); 

     if($this->form_validation->run() == FALSE) 
     { 
      $this->load->view('signup_form'); 
     } 

     else 
     {   
      $this->load->model('membership_model'); 

      if($query = $this->membership_model->create_member()) 
      { 
       $data['main_content'] = 'signup_successful'; 
       $this->load->view('includes/template', $data); 
      } 
      else 
      { 
       $this->load->view('signup_form');   
      } 
     } 

    } 

    function logout() 
    { 
     $this->session->sess_destroy(); 

     $this->index(); 


    } 

站點1:在PHP

<?php 

class Site1 extends CI_Controller 
{ 


    function members_area() 
    { 
     $this->load->view('index'); 


    } 

    function another_page() // just for sample 
    { 
     echo 'good. you\'re logged in.'; 
    } 

    function is_logged_in() 
    { 
     $is_logged_in = $this->session->userdata('is_logged_in'); 
     if(!isset($is_logged_in) || $is_logged_in != true) 
     { 
      echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';  
      die();  
      //$this->load->view('login_form'); 
     }  
    } 

} 


    } 
+0

請使用具有diff選項卡的相同瀏覽器? –

+0

是的,我使用相同的瀏覽器 – user2991258

+0

嘗試使用diff瀏覽器..即1使用鉻和另一個1使用IE或Firefox ..參考@RobinAbony答案爲解釋.. –

回答

1

會話被標籤之間結轉。服務器並不知道如何在兩個選項卡之間分開。閱讀關於會話的PHP文檔以獲取更多信息http://us2.php.net/manual/en/intro.session.php