-2

Possible Duplicate:
Headers already sent by PHP

I have recently shifted to storing sessions in a database. I am trying to set ac cookie in write_function(), but it results in a PHP error Cannot modify header information - headers already sent. Here's the code:

function write_session($sid, $data) 
{
  if(empty($data)){ return; }else if(!isset($_COOKIE['crs'])){ setcookie('crs','xrs',0,'/'); }
}

session_set_save_handler('open_session', 'close_session','read_session', 'write_session','destroy_session','clean_session'); 

How do i correct this?

Community
  • 1
  • 1
sanchitkhanna26
  • 2,143
  • 8
  • 28
  • 42
  • and you are sure you are not sending any headers somewhere in code ? – v0d1ch Jan 09 '13 at 00:13
  • yes i am sure that i am not sending any headers. – sanchitkhanna26 Jan 09 '13 at 00:14
  • 1
    'headers' also include any blank lines in front of php, or php errors, notices, messages. – djot Jan 09 '13 at 00:14
  • There are a total of 2 cookies i wanna create. One "session cookie" and the other i mentioned in the question namely "crs" – sanchitkhanna26 Jan 09 '13 at 00:15
  • @mario thankx, i followed the question in ur link. But what should i do now. I have to set a cookie ater session_start() function is envoked. Is there a workaround? – sanchitkhanna26 Jan 09 '13 at 00:17
  • 1
    Cookies are sent in the headers.... if there has been any output prior to the point in your script where you're setting the cookies, you'll get this message.... the actual message tells you exactly where the output was sent – Mark Baker Jan 09 '13 at 00:18
  • 1
    `session_start()` does not prevent you from calling `setcookie()` afterwards - it is some textual output (most likely whitespace), and the exact file and line is given in the error message. – Sven Jan 09 '13 at 00:20

1 Answers1

0

Something is output before you call that function, check especially for blank non-PHP lines. Without more info or code, that's all I can suggest.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • @showmz this is the begining of the script – sanchitkhanna26 Jan 09 '13 at 00:19
  • Check also for errors/warnings/notices, make sure you set error_reporting properly in order to see those. Also, that looks like an include script, make sure you check the main script(s) as well. – Shomz Jan 09 '13 at 00:20