Topic: Shopping cart tutorial quantity won't go up start session problem

Hi everyone,

I'm am following Jon Lebensold's shopping cart tutorial, I am using Dreameaver CS3 instead of Eclipse. I'm am at the end of video 7, I've finished the $quantity code and started the session in my functions file.
However when I try to refresh my page in the browser the quantity does not go up at all.

I've looked at my php error logs for apache, they state:

[06-Oct-2009 11:30:16] PHP Warning:  session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/mywebsite/templates.php:69) in /Applications/MAMP/htdocs/mywebsite/functions.php on line 11
 

(So I am aware that this is the problem but I have no idea how to sort it out, could somebody please help me. I have attached the code for the templates file, the functions file and the shopping cart class. I'll say thanks in advance.)

My code for php templates, looks like this:

<?php
function render_header($title = 'My Shop')
{
return '
<!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>
    
    <title>'.$title.'</title>
    
    <link href="css/shop.css" rel="stylesheet" type="text/css" />
    </head>

<body>
 <div class="wrapper">
    <div class="nav">
        <a href="#">view store</a>
        <a href="#">view cart</a>
        <a href="#">clear cart</a>
    </div>
';
}
function render_footer()
{
    return '
    </div>
    </body>
        
    </html>';
} 
function render_products_from_xml()
{  
    $output = '<table class="products">
            <tr>';
    foreach(get_xml_catalog() as $product)
    {
        $output .='
                   <td class="product">
                        <h2>'.$product->title.'</h2>
                        <div>
                            <img src="'.$product->img.'" height="151" width="100" />
                            <span>
                                '.$product->description.'
                            </span>
                        </div>
                        <div class="size">
                                '.$product->size.'
                        </div>
                        <div class="price">
                                '.$product->price.'
                        </div>
                    
                     <div class="addToCart">
                        <a href="addToCart.php?id='.$product->id.'">add to cart</a>
                     </div>
                   </td>';
        
    }
        $output .='
                </tr>
            </table>
        ';
    return $output;
}


?>

My code for functions looks like this:

<?php
/** DEFINE GLOBALS **/
define('STORE_XML_FILE','catalog.xml');

/** DEFINE REFERENCES **/
require_once('templates.php');

require_once('ShoppingCart.php');

/** FUNCTIONS **/
session_start();

function get_xml_catalog()
{
 return new SimpleXMLElement(file_get_contents(STORE_XML_FILE));
}

function get_shopping_cart()
{
    if(! isset($_SESSION['cart']))
        return new ShoppingCart();
else
        return unserialize($_SESSION['cart']);
}

function set_shopping_cart($cart)
{
    $_SESSION['cart'] = serialize($cart);
}

function product_exists($product_id)
{
    foreach(get_xml_catalog() as $product)
    {
        if ($product->id == $product_id)
            return true;
    }
    return false;
}


?>

My code for the ShoppingCart class looks like this:

<?php

class ShoppingCart
{
    protected $items = array();
    
    public function AddItem($product_id)
    {
        if (array_key_exists($product_id , $this->items))
            $this->items[$product_id] = $this->items[$product_id] + 1;
            
        else
            $this->items[$product_id] = 1;
    }
    
    public function GetItems()
    {
        return array_keys($this->items);
    }
    
    public function GetItemQuantity($product_id)
    {
        return $this->items[$product_id];
    }
}

?>

Last edited by slam38 (October 6, 2009 6:14 am)

Vote up Vote down

Re: Shopping cart tutorial quantity won't go up start session problem

i'm still a bit new to php, but i've heard that 'headers already sent' is usually a white space problem...try putting 'session_start();' on the same line as '/** FUNCTIONS **/' and return directly after the slash...

Vote up Vote down

Re: Shopping cart tutorial quantity won't go up start session problem

As mcChris said, this error often means you accidentally have an extra space before the session_start() function. I would check that those files above don't have any blank lines/spaces before the opening <?php tag, or possibly any blank lines / spaces after the closing ?> tag in templates.php.

As a sidenote, although the shopping cart tutorial doesn't go into this, it is sometimes a php best practice to leave out the closing PHP tag ("?>") in PHP pages -- it really isn't necessary. (This only applies to pages that are fully PHP, and include no html. You could drop the closing PHP tag on all three PHP pages above.) It's quite easy for extra spaces to end up on the end of a file, and if you're using several includes, tracking these spaces down can be a pain, especially if you're dealing with lots of files. Spaces after closing PHP tags is known to cause various issues including invalid headers like you are encountering...

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Shopping cart tutorial quantity won't go up start session problem

Thankyou Falfencreate, and McChris,

for getting back to me I will try what you have suggested. Just as an after thought is there a specific way to find and get rid of the white space or do I just do through the file and delete where I think it might be?

Thanks again.

Vote up Vote down

Re: Shopping cart tutorial quantity won't go up start session problem

i uploaded my 'magic cart' to my web server for further testing and ran into this 'headers already sent' issue myself...

after following falkencreative's solutions the issue didn't resolve...then i found this: http://www.phpbuilder.com/board/showthr … t=10310794 ...post #8 was what caused my headers to be sent...there are other ideas there too...

Last edited by mcChris (October 7, 2009 9:58 am)

Vote up Vote down