1

here i came across a weird problem. I have a shopping cart and I want every page of my website to display the number of items in cart. I am adding the code below:

<?php
    session_start();
    if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
    $totalItems = is_array($_SESSION['SHOPPING_CART']) ? count($_SESSION['SHOPPING_CART']) : 0; 
?>

The code works perfectly on the cart page that shows all the items added, but NOT on any other page. And one more thing, the above code works perfectly on XAMPP and not on the web server. Here is the error I am getting:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at index.php:1) in index.php on line 2

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at index.php:1) in index.php on line 2

So what may the possible reason? something to do with php version on server? if so, why its working on cart page?

Junaid Rehman
  • 169
  • 4
  • 11

1 Answers1

1

You're already sending a header at line 2, in the files mentioned by you.

Turn error reporting on and carefully check your code.

Odds are very high that you have previous errors that are silently suppressed, but still appear on the page, thus already sending output to the browser, or you have a white space at the beginning of the PHP starting tag.

Check out this question for further and more in-depth information.

Community
  • 1
  • 1
Andrei
  • 3,434
  • 5
  • 21
  • 44
  • I skimmed that answer, didn't read in detail.. but I checked the possible reasons for this warning mentioned in the answer but none worked for me. But i have solved the problem myself but copying the entire code and made new php file, and it worked now.. nothing i changed. nothing at all. and now I wanna what could be the reason? – Junaid Rehman Apr 10 '15 at 20:27
  • Most likely [Byte order mark](http://en.wikipedia.org/wiki/Byte_order_mark). – Andrei Apr 12 '15 at 19:24