Hello everyone:
I'm a bit frustrated because I'm getting an error on one website but not on another one for the same web page. This tells me there's something different about the PHP settings that is different, but I'm not sure where to begin. This is the error I get:
Warning: Cannot modify header information - headers already sent by
Based on my reading online, it is supposedly common for this error to occur when there is extra white space showing up in the code. I'm not sure that this is my problem because, like I've already mentioned, on one site the code works and the other it doesn't. Here is the code being referenced in the error:

if(isset($_COOKIE[session_name()])) {
			setcookie(session_name(), ' ', time()-42000, '/');
		}
session_destroy();
redirect_to("index_bi.php?order=$name");

What I'm trying to do is after the order to borrow books has been placed and an email sent telling of the order, I want the session to be destroyed so that there is nothing left in the person's order list. Any ideas what's wrong? Thank you.

You have to make sure, that you definately don't have any html, echo or print before you set the session. Thats because session sends a cookie to the client via the HTTP-header. As soon you have any output, PHP sends the header and the stuff you want the browser to show.

The reason why you don't have the problem on the one server could be, that you have the error-reporting turned off.

Make sure that you have no html or other text written with echo before that or any html or text before the <?php tag. This could be something as simple as a space:

<?php //Notice the space here
header("Location: thiswillnotwork.html");
?>

And can easily be fixed by removing the extra space or characters:

<?php //No spaces so the header call should work along with the use of session vars 
header("Location: thiswillwork.html");
?>

I also find it to be a good practice to begin the session right after the <?php tag if I am using it in that script.

Thanks for the quick responses. I checked the display errors setting and it's on. And error_reporting is set for 6135. I don't know what this means, but that's the setting for it. And I checked for extra spaces and I don't think there are any. I placed the following on a separate page and I still get the error. It has to be in this script, but I can't see it:

if(isset($_COOKIE[session_name()])) {
			[B]setcookie(session_name(), '', time()-42000,'/');[/B]
		}
		
		// 4. Destroy the session
		session_destroy();
		[B]redirect_to("index_bi.php?order=$name");
[/B]

Especially the lines in bold are getting errors.

Yeah, it was started first thing. Well, I got it to work, but I'm not sure why. There was some conflict between the lines of code I showed you and code in the header. Here is the header code:

<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
<script language="javascript">
function printpage()
 {
  window.print();
 }
</script>
<script language="javascript">
<!--
function confirmPost() {
var agree=confirm("Are you sure you want to delete this student?");
if (agree)
return true;
else
return false;
}
//-->
</script>
<style>
<!--
A{text-decoration:none}
-->
</style>
</head>
	<div id="header">
    	<h1>Bohemia Institut</h1>
    </div>
    <div id="main">

I just cut this header code out of the next web page and it worked fine. But can anyone see the problem here? I would prefer to keep it the way I had it, but will live with it the way it is now if no one sees the problem. Thanks.

I'm a bit confused here, is the code above all that you have at the start of your page? No DTD? Do you have 'html' and 'body' tags? The page looks poorly formed (sorry, no offence). Could you print the page, showing the whole code from the beginning to '<div id="header">'.

Hello everyone:
I'm a bit frustrated because I'm getting an error on one website but not on another one for the same web page. This tells me there's something different about the PHP settings that is different, but I'm not sure where to begin. This is the error I get:
Warning: Cannot modify header information - headers already sent by
Based on my reading online, it is supposedly common for this error to occur when there is extra white space showing up in the code. I'm not sure that this is my problem because, like I've already mentioned, on one site the code works and the other it doesn't. Here is the code being referenced in the error:

if(isset($_COOKIE[session_name()])) {
			setcookie(session_name(), ' ', time()-42000, '/');
		}
session_destroy();
redirect_to("index_bi.php?order=$name");

What I'm trying to do is after the order to borrow books has been placed and an email sent telling of the order, I want the session to be destroyed so that there is nothing left in the person's order list. Any ideas what's wrong? Thank you.

I was having a similar issue the other day, but my situation was a little different. have you tried ob_start(); and ob_end_flush();?

Try putting ob_start(); athe the start of the page and ob_end_flush(); just after the header("Location: URL")

it will not send anything to the browser until it gets to the on_end_flush so it should work

This is how my typical web page starts:

<?php session_start(); 

	function logged_in() {
		return (isset($_SESSION['user_id']));
	}

	function confirm_logged_in() {
		if (!logged_in()) {
			redirect_to("index.php");
		}
	}
?>
<?php
	require("constants.php");
	$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
	if (!$connection) {
		die("Database connection failed: " . mysql_error());
	}

	$db_select = mysql_select_db(DB_NAME,$connection);
	if (!$db_select) {
		die("Database selection failed: " . mysql_error());
	}
?>
<?php require_once("includes/functions.php"); ?>
<head>
<title>Name of Organization</title>
<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
<script language="javascript">
function printpage()
 {
  window.print();
 }
</script>
<script language="javascript">
<!--
function confirmPost() {
var agree=confirm("Are you sure you want to delete this student?");
if (agree)
return true;
else
return false;
}
//-->
</script>
<style>
<!--
A{text-decoration:none}
-->
</style>
</head>
	<div id="header">
    	<h1>Name of Organization</h1>
    </div>
    <div id="main">

The first two sections, the session and connection, are in their own include files. I guess this isn't very good, but I've always worried about the PHP on the page and not the other stuff. Could you enlighten me a bit? Thanks.

This is how my typical web page starts:

<?php session_start(); 

	function logged_in() {
		return (isset($_SESSION['user_id']));
	}

	function confirm_logged_in() {
		if (!logged_in()) {
			redirect_to("index.php");
		}
	}
?>
<?php
	require("constants.php");
	$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
	if (!$connection) {
		die("Database connection failed: " . mysql_error());
	}

	$db_select = mysql_select_db(DB_NAME,$connection);
	if (!$db_select) {
		die("Database selection failed: " . mysql_error());
	}
?>
<?php require_once("includes/functions.php"); ?>
<head>
<title>Name of Organization</title>
<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
<script language="javascript">
function printpage()
 {
  window.print();
 }
</script>
<script language="javascript">
<!--
function confirmPost() {
var agree=confirm("Are you sure you want to delete this student?");
if (agree)
return true;
else
return false;
}
//-->
</script>
<style>
<!--
A{text-decoration:none}
-->
</style>
</head>
	<div id="header">
    	<h1>Name of Organization</h1>
    </div>
    <div id="main">

The first two sections, the session and connection, are in their own include files. I guess this isn't very good, but I've always worried about the PHP on the page and not the other stuff. Could you enlighten me a bit? Thanks.

I am a little confused? This code is different to your origional question? did you solve that issue?

I solved it in the respect that I deleted something and it works now. I do want to try ob_start() and see what happens. I haven't had a chance to look at what you said, Leviathan, but I will. I posted the beginning of my pages for ARDAV to see.

Thank you, Leviathon. The ob_start() works like a charm.

This question has already been answered. Start a new discussion instead.