简体   繁体   English

CodeIgniter无法修改标头信息

[英]CodeIgniter Cannot modify header information

Hi Currently Im still Learning CodeIgniter. 您好,目前我仍在学习CodeIgniter。 Im trying to use its session and enabled the session in autoload file 我正在尝试使用其会话并在自动加载文件中启用该会话

First the file structure of files is like this/ 首先,文件的文件结构是这样的/

I structure my files like this. 我这样构造我的文件。 all templates will go in views/index.php 所有模板都将进入views / index.php

my problem is I get this error Cannot modify header information 我的问题是我收到此错误Cannot modify header information

I have a controller Home 我有一个控制器主页

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Home extends CI_Controller{  

        public function index()
        {     
            $view['title'] = 'Welcome';

            $data['data'] = $view;
            $data['content'] = 'home';
            $this->load->view('index',$data);
        }

    } 

My views is something like this 我的看法是这样的

views/index.php 意见/ index.php文件

<!DOCTYPE html> 
<html lang="en"> 
    <?php     
        //echo ("Welcome User: " . $datas["user_id"]); 
        $this->load->view('header',$data);
        $this->load->view('template/' . $content);
        $this->load->view('footer',$data);
    ?>
</html> 

views/header.php 意见/ header.php文件

 <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content=""> 
    <title><?=$title?></title>  
      <link href="<?=ASSETS_PATH?>css/bootstrap.css" rel="stylesheet"> 
    <link href="<?=ASSETS_PATH?>css/custom.css" rel="stylesheet">  
  </head> 
  <body oncontextmenu="return false">

views/footer.php 意见/ footer.php

   <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="<?=ASSETS_PATH?>js/bootstrap.min.js"></script>
    <script src="<?=ASSETS_PATH?>js/javascript.js"></script> 
  </body>

then my content 然后我的内容

views/template/home.php 视图/模板/ home.php

<div class="jumbotron mod-jumbotron"> 
    <div class="jm-button" style="text-align:center;">
           <h1>Welcome</h1>
    </div>

</div> 

I don't know why do i get that error session. 我不知道为什么会收到该错误会话。 Please hope you could help me. 请希望能帮助我。 I didn't even set a session. 我什至没有安排会议。 I have just added it in auto load and don't know why i get that header problem. 我刚刚在自动加载中添加了它,不知道为什么会出现标题问题。 Is my file structure wrong? 我的文件结构不正确吗?

A PHP Error was encountered 遇到PHP错误

Severity: Warning 严重程度:警告

Message: Cannot modify header information - headers already sent by (output started at site/application/controllers/home.php:2) 消息:无法修改标题信息-已发送的标题(输出从site / application / controllers / home.php:2开始)

Filename: libraries/Session.php 文件名:libraries / Session.php

Line Number: 675 行号:675

You probably have a space before the opening <?php tag in your Home controller which is causing output. 在Home控制器中打开<?php标记之前,您可能有一个空格,这会导致输出。

// a space here is enough to cause output
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

在配置文件或自动加载文件上使用此行,或在index.php文件中添加此行可能会解决您的问题。

ob_start()

You will have to put the loading of the pages in the controller and not in the views. 您将不得不将页面的加载放置在控制器中而不是视图中。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{  

    public function index()
    {     
        $data['title'] = 'Welcome';
        $this->load->view('header',$data);
        $this->load->view('index',$data);
        $this->load->view('footer',$data);
    }

} 

The view is for display, and you control it using the controller. 该视图用于显示,您可以使用控制器对其进行控制。

<script src="<?= base_url(). 'js/javascript.js' ?>"></script> 
<link href="<?=base_url(). 'css/custom.css'?>" rel="stylesheet"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM