2016-04-01 184 views
1

我想銷燬會話數據,但不能這樣做。無法銷燬會話

當我點擊註銷按鈕時,它總是顯示出現一些問題,var_dump()的值顯示NULL。

顯示錯誤,例如消息:session_destroy():試圖破壞未初始化會話

控制器:Users.php

class Users extends CI_Controller 
{ 

    public function logout() 
    { 
     $result = $this->session->sess_destroy(); 
     var_dump($result = $this->session->sess_destroy()); 
     if($result) 
     { 
      echo "You are logged Out!!"; 
     } 
     else 
     { 
      echo "Some problem has been occured"; 
     } 
    } 

public function login() 
    { 
     $this->form_validation->set_rules('customer-id', 'custid', 'trim|required'); 
     if ($this->form_validation->run() == FALSE) 
     { 
      echo "Please Enter Customer ID"; 
     } 
     else 
     { 


      $this->form_validation->set_rules('pass', 'password', 'trim|required'); 
      if ($this->form_validation->run() == FALSE) 
      { 
       echo "Please Enter Password"; 
      } 
      else 
      { 
       $custid = $this->input->post("customer-id"); 
       $password = $this->__encrip_password($this->input->post('pass')); 

       if('loginuser' == TRUE) 
       { 


        $data = $this->user_model->get_username($custid); 
        //$data['custid'] = $custid; 
        $this->session->set_flashdata('data',$data); 
        $this->session->keep_flashdata('data'); 
        var_dump($this->session->flashdata('data')); 
        //echo $data; 
        redirect('users/clientview'); 
        //$this->load->view('clientview', $data); 

       } 
       else 
       { 
        $result = $this->user_model->login($custid, $password); 
        if ($result) 
        { 

         $sessiondata = array('customer-id' => $custid, 
           'loginuser' => TRUE); 

         $this->session->set_userdata($sessiondata); 
         $data['custid'] = $custid; 
         echo "You have successfully logged-in :)"; 

        } 
        else 
        { 
         echo $custid; 
         echo $password; 
         echo "Invalid Username or Password"; 
        } 
       } 
      } 
     } 
    } 
} 
+0

你需要session_start();之前你可以銷燬它 也header(「Location ...」)正在發送標題,所以你不能關閉會話之後。做關閉會話後的重定向 – Raj

+0

@Raj請參閱標籤codeigniter – RiggsFolly

+0

爲什麼你會假設一個問題發生了什麼?我不知道'sess_destroy()'應該返回什麼,但是手冊沒有提到任何返回值,所以我不會測試它或者依賴它。請注意,php警告可能是由事實上,你調用該方法兩次。 – jeroen

回答

4

您正在嘗試兩次破壞會話。

$result = $this->session->sess_destroy(); 
var_dump($result = $this->session->sess_destroy()); 

做到這一點,而不是作爲一線破壞會議上,第二次將拋出一個錯誤有關的未初始化會話,因爲你試圖摧毀它在第二行第二次。

$result = $this->session->sess_destroy(); 
var_dump($result); 

我也不能肯定是否sess_destroy()實際返回什麼東西可以測試?

+0

我猜想第二次使用'var_dump()'來引入調試問題。所以我認爲真正的問題是e OP正在測試'sess_destroy()'的返回值,該值不存在或至少未被分配,就我所見。 – jeroen

+0

@ jeroen我認爲你可能是對的,但我的代碼字有點生疏。 – RiggsFolly

+0

同樣在這裏,我不得不查看手冊剛剛:-) – jeroen