1

When I want to divide my code in multiple controllers to create some structure I always get this session error. I tried a lot of things I found on google and stackoverflow but that didn't solve it.. This is the error:

ERROR :

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php:100)

Filename: Session/Session.php

Line Number: 140

Backtrace:

File: /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php
Line: 11
Function: __construct

File: /Applications/MAMP/htdocs/BT_dashboard/index.php
Line: 292
Function: require_once

I've got my 'main' controller User. And I want to add a controller 'Project'. When I call the project controller I get the error. Here is the user controlelr :

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * User class.
 * 
 * @extends CI_Controller
 */
class User extends CI_Controller {

    public function __construct() {

        parent::__construct();
        //$this->load->library(array('session'));
        $this->load->helper(array('url'));
        $this->load->model('user_model');
        $this->load->model('employee_model');
        $this->load->model('customer_model');
        $this->load->model('project_model');
    }

public function index() {
    if (!$this->session->userdata('userlevel')) {
        $this->load->view('dashboard_login');
    } else {
        $data['userdata'] = $this->session->userdata;
        $data['employeetotal'] = $this->user_model->get_amount_employees();
        $data['customertotal'] = $this->user_model->get_amount_customers();
        $data['projectstotal'] = $this->user_model->get_amount_projects();

        $this->load->view('header', $data);
        $this->load->view('dashboard_index', $data);
        $this->load->view('wrapper', $data);
    }
}
public function login() {
        $loggedin = $this->session->userdata('logged_in');
        if (!$loggedin) {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('user_mail', 'user mail', 'required');
            $this->form_validation->set_rules('user_password', 'user password', 'required');

            if ($this->form_validation->run() == false) {
                $data = new stdClass();
                $data->error = 'Check your user and password';
                $this->load->view('dashboard_login', $data);
            } else {
                $usermail = $this->input->post('user_mail');
                $password = $this->input->post('user_password');

                if ($this->user_model->resolve_user_login($usermail, $password)) {

                    $user_id = $this->user_model->get_user_id_from_mail($usermail);
                    $user = $this->user_model->get_user($user_id);

                    $this->session->set_userdata('user_id', $user_id);
                    $this->session->set_userdata('user_name', (string) $user->user_name);
                    $this->session->set_userdata('user_gsm', (string) $user->user_gsm);
                    $this->session->set_userdata('user_address', (string) $user->user_address);
                    $this->session->set_userdata('user_city', (string) $user->user_city);
                    $this->session->set_userdata('userlevel', $this->user_model->get_user_level((int) $user->user_id));
                    $this->session->set_userdata('user_mail', $usermail);

                    /*
                      $_SESSION['user_id'] = $user_id;
                      $_SESSION['user_name'] = (string) $user->user_name;
                      $_SESSION['user_gsm'] = (string) $user->user_gsm;
                      $_SESSION['user_address'] = (string) $user->user_address;
                      $_SESSION['user_city'] = (string) $user->user_city;
                      $_SESSION['userlevel'] = $this->user_model->get_user_level((int) $user->user_id);
                      $_SESSION['user_mail'] = $usermail; */
                    $data['userdata'] = $this->session->userdata;

                    if ($this->session->userdata('userlevel') == "3") {
                        $data['employeetotal'] = $this->user_model->get_amount_employees();
                        $data['customertotal'] = $this->user_model->get_amount_customers();
                        $data['projectstotal'] = $this->user_model->get_amount_projects();
                    }
                    $this->load->view('header', $data);
                    $this->load->view('dashboard_index', $data);
                    $this->load->view('wrapper', $data);
                } else {
                    $data = new stdClass();
                    // login failed
                    $data->error = 'Wrong username or password.';
                    // send error to the view
                    $this->load->view('dashboard_login', $data);
                }
            }
        } else {
            $data['userdata'] = $this->session->userdata;
            $data['employeetotal'] = $this->user_model->get_amount_employees();
            $data['customertotal'] = $this->user_model->get_amount_customers();
            $data['projectstotal'] = $this->user_model->get_amount_projects();
            $this->load->view('header', $data);
            $this->load->view('dashboard_index', $data);
            $this->load->view('wrapper', $data);
        }
    }

My project controller looks like :

<?php

defined('BASEPATH') OR exit('No direct script access allowed');
/*
 * File Name: employee.php
 */

class Project extends CI_Controller {

    public function __construct() {
        parent::__construct();
        //$this->load->library(array('session'));
        $this->load->helper(array('url'));
        $this->load->model('user_model');
        $this->load->model('employee_model');
        $this->load->model('customer_model');

    }

    public function createProject() {
        if ($this->session->userdata('userlevel')) {
            if ($this->session->userdata('userlevel') < 3) {
                $userid = $this->uri->segment(3);
            } else {
                $userid = $this->input->post('userproject');
            }
            $this->load->helper('form');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('project_name', 'project name', 'trim|required|min_length[2]|callback_is_project_name_unique[' . $this->input->post('project_name') . ']');
            $this->form_validation->set_rules('project_address', 'project address', 'trim|required|min_length[2]');
            $this->form_validation->set_rules('project_description', 'project description', 'trim|required|min_length[2]');
            $this->form_validation->set_rules('project_city', 'project city', 'trim|required|min_length[2]');

            if ($this->form_validation->run() == FALSE) {

                $data['userdata'] = $this->session->userdata;
                $this->load->view('header', $data);
                if ($this->session->userdata('userlevel') < 3) {
                    $this->load->view('dashboard_add_project', $data);
                } else {
                    $data['userslist'] = $this->user_model->get_users_list();
                    $this->load->view('dashboard_add_project_admin', $data);
                }
                $this->load->view('wrapper', $data);
            } else {

                $Address = urlencode($this->input->post('project_address'));
                $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $Address . "&sensor=true";
                $xml = simplexml_load_file($request_url) or die("url not loading");
                $status = $xml->status;
                if ($status == "OK") {
                    $Lat = $xml->result->geometry->location->lat;
                    $Lon = $xml->result->geometry->location->lng;
                    $LatLng = "$Lat,$Lon";
                }

//pass validation
                $data = array(
                    'project_name' => $this->input->post('project_name'),
                    'project_address' => $this->input->post('project_address'),
                    'project_description' => $this->input->post('project_description'),
                    'project_city' => $this->input->post('project_city'),
                    'project_finished' => $this->input->post('project_finished'),
                    'lat' => $Lat,
                    'lng' => $Lon,
                );

//$this->db->insert('tbl_user', $data);
                if ($this->user_model->create_project($data, $userid, $this->input->post('project_name'))) {

                    if ($this->session->userdata('userlevel') > 1) {
                        $data['projectlist'] = $this->user_model->get_project_list();
                        $data['uncompleted_projects'] = $this->user_model->get_uncompleted_projects();
                        $data['completed_projects'] = $this->user_model->get_completed_projects();
                    } else {
                        $data['projectlist'] = $this->user_model->get_project_list_userid($userid);
                    }
                    $data['userdata'] = $this->session->userdata;

                    $this->load->view('header', $data);
                    $this->load->view('dashboard_projects', $data);
                    $this->load->view('wrapper', $data);
                } else {
                    $data->error = 'There was a problem creating your new employee. Please try again.';
                    $data['userdata'] = $this->session->userdata;

                    // send error to the view
                    $this->load->view('header', $data);
                    $this->load->view('dashboard_add_project', $data);
                    $this->load->view('wrapper', $data);
                }
            }
        }
    }

}
?>

My config/autoload.php got following codeline :

$autoload['libraries'] = array('database','session');
5
  • The error message says something about line 100 in your Project.php, which line is that? Commented Dec 24, 2015 at 15:29
  • 1
    That's the end of the controller. (php file) Commented Dec 24, 2015 at 15:31
  • I think I heared something about not ending with ?> in php controllers in codeigniter, try that. I dont see you doing that in User either, but that might be because you didnt copy the entire thing Commented Dec 24, 2015 at 15:33
  • That's it , thankyou!!! Commented Dec 24, 2015 at 15:36
  • Lemme farm that rep tho googogo Commented Dec 24, 2015 at 15:36

1 Answer 1

2

In codeigniter I think the framework is appending php after your controller, by exiting your controller with ?>, you make anything that is appended look like hmtl that is suppsoed to be echoed.

Since you cant just send html after you send a return to the client (this->load->view()), you're getting this error.

Don't exit your controller with ?>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.