使用多个控制器会导致会话错误代码符号 [英] Using multiple controllers cause session error codeigniter

查看:89
本文介绍了使用多个控制器会导致会话错误代码符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的应用程序中只使用一个控制器时,用户,一切工作正常。当我尝试使用2或3来划分代码并创建一些结构时,我的应用程序总是给出以下错误:

When I use only 1 controller in my application, user, everything works fine. When I try to use 2 or 3 to divide the code and create some structure my application always give the following 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:99)

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

Googled它尝试了很多东西,但找不到答案。我使用codeigniter 3.我的用户控制器看起来像:

Googled it and tried many things but can't find an answer. I'm using codeigniter 3. My User controller looks like :

<?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->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 createProject() {
        if ($_SESSION['userlevel']) {
            if ($_SESSION['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 ($_SESSION['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 ($_SESSION['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);
                }
            }
        }
    }



第二控制器,项目控制器。这是我想使用的控制器知道粘贴createproject代码,因此这给了更多的结构。但是当我把它粘贴在这里,适应我的意见,它给出的错误。这里是我的project_controller代码:

My second controller, the project controller. This is the controller I want to use know to paste the createproject code so this gives more structure. But when I paste it here and adapt my views it gives the error. here's my project_controller code:

<?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->helper(array('url'));
        $this->load->model('user_model');
        $this->load->model('employee_model');
        $this->load->model('customer_model');

    }

    public function createProject() {
//same as usercontroller

我的用户/用户控制器中的登录代码:

My User/login code in User controller :

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);

                    $_SESSION['user_id'] = $user_id;
                    $_SESSION['user_name'] = (string) $user->user_name;
                    $_SESSION['logged_in'] = (bool) true;
                    $_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 ($_SESSION['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);
        }
    }



在配置/自动加载我有以下: / p>

In config/autoload I've got following:

$autoload['libraries'] = array('database','session');


推荐答案

这是一个常见的问题, 在您的控制器 Project.php (您应该只有76行,如果我是正确的)上的行99 。我敢打赌你的控制器没有99行...删除额外的行,错误将消失。
或如果您在第99行有回声

This is a common problem , notice that the error is on line 99 on your controller Project.php (you should have only 76 lines if I am correct). I bet your controller doesn't have 99 lines ... delete the extra lines and the error will vanish. or if you have an echo on line 99 comment it

希望有帮助!

这篇关于使用多个控制器会导致会话错误代码符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆