3

I am getting php session error on my live developed using codeigniter

A PHP Error was encountered

Severity: Warning

Message: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/userts4m/public_html/development/relatioweb/admin/index.php:1)

Filename: Session/Session.php

Line Number: 140

Backtrace:

File: /home/userts4m/public_html/development/relatioweb/application/admin/controllers/User.php
Line: 9
Function: __construct

File: /home/userts4m/public_html/development/relatioweb/admin/index.php
Line: 293
Function: require_once

Working at localhost fine. But when i uploded at server we are facing this issue.

What should i do ...

Ashish Chaturvedi
  • 1,352
  • 3
  • 13
  • 37

10 Answers10

14

There are two solutions to fix this issue.

Some times this error occurred due to the wrong configuration of the framework, go to the folder applications/config and open up the config.php write this in the starting of file like this

<?php

  if (!defined('BASEPATH')) exit('No direct script access allowed');
    ob_start();
    /* Remaining Part Of your file
     .........................................
   */

Another solution is to write ob_start() in the constructor of the class like this:

<?php

 if (!defined('BASEPATH')) exit('No direct script access allowed');
   class Your_Controller extends CI_Controller {
     public function __construct() {
       parent::__construct();
       ob_start();
     }
   }
Muhammad Usman
  • 1,403
  • 13
  • 24
4

the problem is well explained here How to fix "Headers already sent" error in PHP

In order to solve this I needed to implement the concept of output-buffering (Output buffering is a mechanism in which instead of sending a response immediately to browser we buffer it somewhere so that we can send it at once when whole content is ready ). So as the error indicate the problem occurs in index.php
what I did I inserted

ob_start();

at the beginning of index.php

Community
  • 1
  • 1
Yahia Bat
  • 564
  • 5
  • 12
3

As per @kunal and @dale suggestion there was a space in /home/userts4m/public_html/development/relatioweb/admin/index.php

Ashish Chaturvedi
  • 1,352
  • 3
  • 13
  • 37
2

This usually means that somewhere you've already got a session_start() and a second one will throw this error. Remove the redundant one.

bucketman
  • 463
  • 6
  • 15
2

In this file may have blank space before start PHP tag.

vpgodara
  • 329
  • 2
  • 2
1

Try using @session_start(); It will bypass the error and session will be start. Please note @session_start() is your first line of the code. Do not execute or echo anything before it.

0

Check the controller file, if there are spaces before the php tag remove it.

Keale
  • 3,924
  • 3
  • 29
  • 46
0

check your files for extra spaces or enter, in my case, I have an extra enter in application/config/autoload.php before php tag (Iam using codeigniter)

Andy
  • 436
  • 4
  • 7
0
 ob_start ([ callable $output_callback = NULL [, int $chunk_size = 0 [, int $flags = 
 PHP_OUTPUT_HANDLER_STDFLAGS ]]] ) : bool

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

0

I had to go to php.ini and remove comment from always_populate_raw_post_data = -1 line.