By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,647 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,647 IT Pros & Developers. It's quick & easy.

Warning: headers already sent

P: n/a
Hello, I am a total newbie to PHP and programming in general. I am playing
around with a PHP / MySQL shopping cart script which I found at
http://www.macromedia.com/devnet/mx/.../php_cart.html. When
I try to start a session or create a cookie, I get the following errors.

Warning: Cannot send session cache limiter - headers already sent (output
started at /wrapper_head2.php:27) in /cart.php on line 13

Warning: Cannot add header information - headers already sent by (output
started at /wrapper_head2.php:27) in /cart.php on line 14

Warning: Cannot add header information - headers already sent by (output
started at /wrapper_head2.php:27) in /cart.php on line 14

Warning: Cannot add header information - headers already sent by (output
started at /wrapper_head2.php:27) in /cart.php on line 14

Warning: Cannot add header information - headers already sent by (output
started at /wrapper_head2.php:27) in /cart.php on line 14
Here is the beginning of my PHP script for the shopping cart. I haven't
changed much from the original tutorial.

<?php

// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc.

$dbServer = "blanked";
$dbUser = "blanked";
$dbPass = "blanked";
$dbName = "blanked";

function ConnectToDb($server, $user, $pass, $database)
{
// Connect to the database and return
// true/false depending on whether or
// not a connection could be made.

$s = @mysql_connect($server, $user, $pass);
$d = @mysql_select_db($database, $s);

if(!$s || !$d)
return false;
else
return true;
}

function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

//call the page header
include("wrapper_head2.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{

ShowCart();
}
}

function AddItem($itemId, $qty)
{ ... the functions in the case are defined from here on.

Here are the contents of the wrapper_head2.php page, which it tells me is
causing the problem.

<?

echo "

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">

<html>
<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<title>Title</title>

<link rel=stylesheet type=\"text/css\" href=\"blue.css\">

</head>

<body>

<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td>
<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\"
cellspacing=\"0\">
<tr>
<td>

";

//call header page
include("header.html");

echo "

</td>
</tr>
<tr>
<td>

";

//call horizontal menu
include("strip.html");

echo "

</td>
</tr>
</table>
</td>
</tr>

<tr>
<td>
<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">

<tr>
<td background=\"images/stripsha.gif\" height=\"4\" width=\"120\"
bgcolor=\"#D7DEEC\"></td>
<td background=\"images/stripsha2.gif\" height=\"4\"></td>
<td background=\"images/stripsha3.gif\" height=\"4\" width=\"140\"
bgcolor=\"#ECF3FF\"></td>
</tr>

<tr>
<td width=\"120\" valign=\"top\" bgcolor=\"#D7DEEC\">

";

//call vertical menu
include("menu.html");

echo "

</td>
<td valign=\"top\">
<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\"
cellspacing=\"0\">
<tr>
<td valign=\"top\" height=\"400\">
<div align=\"center\">
";

?>

I am guessing that my problem is with the order of the code in my cart.php
page. Several online forums said to check for blank lines and white space
before and after the PHP <? ?> tags, because session_start has to be called
before anything else. I checked with Notepad, and can't find any space
outside of the PHP tags in cart.php, wrapper_head2.php, or any of the html
files called by wrapper_head2.php. Can someone please give me some
suggestions as to what I am doing wrong?

Thank you!
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
While the city slept, None (ln*****@sypmatico.ca) feverishly typed...
Hello, I am a total newbie to PHP and programming in general. I am
playing around with a PHP / MySQL shopping cart script which I found
at
http://www.macromedia.com/devnet/mx/.../php_cart.html.
When I try to start a session or create a cookie, I get the following
errors.

Warning: Cannot send session cache limiter - headers already sent
(output started at /wrapper_head2.php:27) in /cart.php on line 13

[etc]

As answered yesterday (or possibly the day before) and several times over
the last couple of weeks...

You cannot have ANY output AT ALL before you set sessions, write cookies or
send headers. That includes any HTML output. Move your session code to
before any output is sent and it should work, or at least, you shouldn't be
getting the errors you do at the moment! ;-)

Cheers,
Nige

--
Nigel Moss
http://www.nigenet.org.uk
Mail address not valid. ni***@DOG.nigenet.org.uk, take the DOG. out!
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.