Cannot send session cookie/ session cache error

hi

This is my config file

<?php $conn=mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("db_supply",$conn);
session_start();
?>

This is email.php

<?php require_once("../../config.php");
$subject= "newsletter";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:info@site.com' . "\r\n";
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

This is the error that is getting logged in log file

PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send 

session cookie - headers already sent in /home/public_html/config.php on line 3

PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send 

session cache limiter - headers already sent in /home/public_html/config.php on line 3

I have removed all white spaces, but still i m getting this error, dont know why ??

vineet

Try this:

// config.php

<?php
$conn=mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("db_supply",$conn);

// test to see if session_start() has already been called:
  if(!isset($_SESSION)) {
     session_start();
 }

Hi John

Do i need to start $conn from the second line instead of first line ??

vineet

No. White space and line-feeds are ignored but do make your script more readable.

It looks as though there is an error in your script and it is not showing.

Try this:
// email.php

<?php 
error_reporting(-1); // maximum errors and warnings, check the PHP Manual online
ini_set('display_errors', true);

echo '<br >' . __LINE__; die; // move this line down until you see an error or warning

require_once("../../config.php");
$subject= "newsletter";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:info@site.com' . "\r\n";
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

Hi John

Email.php file echoes “5” on the page

nothing else is echo.

on view source of page its “
5”

vineet

Try moving the line down to after the require statement.

Hi John

again it echoes “5”

<?php require_once("../../config.php");
error_reporting(-1); // maximum errors and warnings, check the PHP Manual online
ini_set('display_errors', true);

echo '<br >' . __LINE__; die; // move this line down until you see an error or warning
$subject= "Newsletter";
?>

vineet

It looks like you didn’t move it down.

If you aren’t seeing any error messages all that echo is saying is “I made it to here OK”

1 Like

That script is not the same as Post #4

I think that error_reporting() and display errors are not set to reveal any sensitive information to a user who experiences either warnings or errors.

The ideas is to temporarily set the configuration so that you can find and eliminate the errors and warnings.

Your PHP Warning: states that session_start() must be called before any text is output to the screen.

It looks as though require_once “…/…/config.php” is producing an error which is output to the screen BEFORE session_start() is called.

Try this:

//email.php

<?php
// DEBUG: set maximum errors and warnings, check PHP Manual online
   error_reporting(-1); 
   ini_set('display_errors', true); // output to screen


// DEBUG: move this line down until you see an error or warning
// DEBUG: First Test here:
   echo '<br >line: ' . __LINE__ .'<br>file: ' . __FILE__; 
   die; 
// after test remove above four line and also this line on


// test to see if session_start() has already been called:
   if(!isset($_SESSION)) {
     session_start();
   }

// DEBUG: Second Test here:
   echo '<br >line: ' . __LINE__ .'<br>file: ' . __FILE__; 
   die;
// after test remove above three lines and also this line on


// try to connect to the database
   require_once("../../config.php");

// DEBUG: Third Test here:
   echo '<br >line: ' . __LINE__ .'<br>file: ' . __FILE__; 
   die; 
// after test remove above three lines and also this line on

$subject= "newsletter";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:info@site.com' . "\r\n";
$body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

1 Like

What editor are you using? Is your files in the editor encoded with UTF-8 without BOM? If they are not try it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.