Welcome Guest, Not a member yet? Register   Sign In
Loading session class triggers header error
#1

[eluser]Unknown[/eluser]
Hi all,
I'm new to this forum and this is my first post, it is a really simple problem. Each time I attempt to load the session class in any of my controllers and then load a view, I get a "cannot modify header information error" in this view.
Example
Code:
<?php
class Example extends CI_Controller {
        public function Example()
        {
           parent::__construct();
           $this->load->library('session');
        }


    public function index()
    {
           $this->session->set_userdata('user','examplename');
           $user = $this->session->get_userdata('user');
           $this->load->view("example_view", array('name'=>$user));
    }

}
?>
Now if I were to run this script, I will get an error similar to
Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\...\example.php:6)
Filename: libraries/Session.php

Line Number: 686
The view loads, but I would really want to remove this message from it. If you have any idea about its cause please let me know

Thanks
#2

[eluser]cideveloper[/eluser]
Try this. Remove everything after the closing bracket from the Example class (no newline after the closing tag either). You don't need the closing php tag at the bottom.

Also as a suggestion, change

Code:
public function Example()
to
Code:
public function __construct()

just to keep with the current ways of doing it.

Controller Constructors
#3

[eluser]hermes1986[/eluser]
Hello, i'm new to this forum, and i am new to codeigniter..i have the same problem, and i got this message appear

Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\noodletrial\application\controllers\noodlehome_ctrl.php:34)

Filename: libraries/Session.php

Line Number: 671

the message appear after i put a some session syntax in my model file, here is my model file
Code:
<?php
class Users_Model extends CI_Model
{
    function _required($required,$data)
    {
        foreach ($required as $fields)
            if(!isset($data[$fields])) return false;
            
            return true;
    }
    
    function _default($default,$options)
    {
        return array_merge($default,$options);
    }

    function AddUsers ($options=array())
    {
        if (!$this->_required(array('UserId','UserName','UserEmail','UserPassword','UserStatus'),$options))
        return false;
        
        $options=$this->_default(array('UserStatus','A'),$options);
        
        $this->db->insert('users',$options);
        
        return $this->db->insert_id();
    }
    
    function UpdateUsers($options=array())
    {
        if (!$this->_required(array('UserId'),$options))
        return false;
        
        if (isset($options['UserName']))
            $this->db->set('UserName',$options['UserName']);
            
        if (isset($options['UserEmail']))
            $this->db->set('UserEmail',$options['UserEmail']);
            
        if (isset($options['UserPassword']))
            $this->db->set('UserPassword',md5($options['UserPassword']));
            
        if (isset($options['UserStatus']))
        $this->db->set('UserStatus',$options['UserStatus']);
            
        return $this->db->affected_rows();
    }
    
    function GetUsers($options=array())
    {
        if(isset($options['UserId']))
            $this->db->where('UserId',$options['UserId']);
            
        if(isset($options['UserName']))
            $this->db->where('UserName',$options['UserName']);
            
        if(isset($options['UserEmail']))
            $this->db->where('UserEmail',$options['UserEmail']);
            
        if(isset($options['UserPassword']))
            $this->db->where('UserPassword',$options['UserPassword']);
            
        if(isset($options['UserStatus']))
            $this->db->where('UserStatus',$options['UserStatus']);
            
        if(isset($options['limit'])&& isset($options['offset']))
            $this->db->limit($options['limit'],$options['offset']);
        else if(isset($options['limit']))
            $this->db->limit($options['limit']);
            
        if(isset($options['sortBy'])&& isset($options['sortDir']))
            $this->db->order_by($options['sortBy'],$options['sortDir']);
            
        $query=$this->db->get("users");
        
        if(isset($options['UserId'])||isset($options['UserEmail']))
            return $query->row(0);
        
        return $query->result();
        
    }
    
    function Login($options=array())
    {
    
        if (!$this->_required(array('UserEmail','UserPassword'),$options))
        return false;
        
        $user=$this->GetUsers(array('UserEmail'=>$options['UserEmail'],'UserPassword'=>md5($options['UserPassword'])));
        
        if(!$user) return false;
      
        $this->session->set_userdata('UserEmail',$user->UserEmail);
        $this->session->set_userdata('UserId',$user->UserId);
      
        return true;
            
    }
}

and here is my controller file which calls up to the model
Code:
<?php
class NoodleHome_Ctrl extends CI_Controller {


    function __construct()
    {
        parent::__construct();
        $this->load->model('users_model');
    }
    
    function Login()
    {
    
        $this->form_validation->set_rules('UserEmail','email','trim|required|valid_email|callback__check_login');
        $this->form_validation->set_rules('UserPassword','password','trim|required');
        
        if ($this->form_validation->run())
        {
            echo "You have successfully logged in..";
                   if($this->users_model->Login(array('UserEmail'=>$this->input->post('UserEmail'),'UserPassword'=>$this->input->post('UserPassword'))))
            {
                redirect('dashboard');
            }
            else
            {
                redirect('noodlehome_ctrl/login');
            }
            
            $this->load->view('noodlehome/login_view');
        }
    }
    
    function index()
    {
        $this->load->view('noodlehome/login_view');
    }
    
    function _check_login($userEmail)
    {
        if($this->input->post('UserPassword'))
        {
            $user=$this->users_model->GetUsers(array('UserEmail'=>$userEmail,'UserPassword'=>md5($this->input->post('UserPassword'))));
            if($user) return true;
        }
        $this->form_validation->set_message('_check_login','Your Username and Password combination is invalid');
        return false;
    }
    
}

and i found the problem in the model is :
$this->session->set_userdata('UserEmail',$user->UserEmail);
$this->session->set_userdata('UserId',$user->UserId);

and the problem in the controller is :
redirect('dashboard');

please give any help...and thx before

regards,

hermes
#4

[eluser]hermes1986[/eluser]
sorry my bad, i've already found the real problem, which is : i write an echo before redirect...thx alot guys..and my problem is solved




Theme © iAndrew 2016 - Forum software by © MyBB