Codeigniter注销问题

时间:2014-08-25 20:11:08

标签: php codeigniter session

当我尝试注销我创建的会话时,我不断收到修改标题错误。这是我的代码

   function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

以下是错误:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Users/Rob/stage/dashboard.php:36)

Filename: libraries/Session.php

Line Number: 688

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Users/Rob/stage/dashboard.php:36)

Filename: helpers/url_helper.php

Line Number: 540

任何想法会导致什么?

这是我代码的整个控制器。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class dashboard extends CI_Controller {

 function __construct()
 {
   parent::__construct();
 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['username'];
     $this->load->view('dashboard', $data);
   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

}

?>

我基本上只是将用户登录到一个视图中,然后在该页面上有一个注销链接,当他们点击它们时,他们会退出会话,然后运行注销功能。有任何想法吗?

2 个答案:

答案 0 :(得分:1)

This answer很可能会解决您的问题。

但是,由于我无法发表评论,我想指出,如果你要破坏会话,你不需要先取消它的任何部分。因此可以改变以下

 $this->session->unset_userdata('logged_in');
 session_destroy();

到此:

 $this->session->sess_destroy();

答案 1 :(得分:1)

还要添加,请使用

$this->session->sess_destroy();

而不是session_destroy()