douan7529
douan7529
2014-08-25 20:11
浏览 25
已采纳

Codeigniter注销问题

When I try to logout of the session I created I keep getting modify header errors.. Here is my code

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

Here are the errors:

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

Any ideas what would cause this?

This is my entire controller for the the code.

<?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');
 }

}

?>

I basically just log the user into a view and then there is a logout link on that page which when they click it they then get logged out of the session which then runs the logout function. Any ideas?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drzb7969753
    drzb7969753 2014-08-25 20:44
    已采纳

    This answer will most likely resolve your issue.

    However, as I cannot comment, I'd like to point out that if you are going to destroy the session you don't need to unset any part of it first. Therefore can change the following

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

    to this:

     $this->session->sess_destroy();
    
    点赞 评论
  • dongsu7049
    dongsu7049 2014-08-25 21:36

    Also to add, use

    $this->session->sess_destroy();
    

    instead of session_destroy().

    点赞 评论

相关推荐