1

Hi Currently Im still Learning 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

my problem is I get this error 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

<!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

 <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

   <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

<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

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at site/application/controllers/home.php:2)

Filename: libraries/Session.php

Line Number: 675

2
  • What is ASSETS_PATH ? Use base_url() Commented Jan 9, 2014 at 13:45
  • oh. its define('ASSETS_PATH','http://localhost/site/assets/'); Commented Jan 9, 2014 at 13:48

3 Answers 3

1

You probably have a space before the opening <?php tag in your Home controller which is causing output.

// a space here is enough to cause output
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
Sign up to request clarification or add additional context in comments.

1 Comment

ooh God What have I done..well you where right Mudshark. Thanks.
0

Use this line on your config file or autoload file or add in index.php file may be resolved your problem.

ob_start()

Comments

0

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"> 

4 Comments

well. thanks about the base_url(). I tried editing my site but still i get the error
Remove the session from autoload and try again. It is probably the session that is causing the error.
yup. when i add the session it cause the error. But I need the session library.
Add the encryption key in the config.php. In CI, sessions are store in tables so you will need to create a table in your database to save the sessions

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.