0

I am using AJAX to call a php script to add "+1" to a certain product and keep it in a cart. Everything seems to be working but I have the following warning:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

My code is the following:

<?php
session_start();
if (!isset($_SESSION["product1"])) {
    $_SESSION["product1"] = 0;
}
if (!isset($_SESSION["product2"])) {
    $_SESSION["product2"] = 0;
}
var_dump($_SESSION);
session_write_close();
?>

<!doctype html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="js/ajax.js"></script>
</head>
<body>
<p id="add1">Prod 1: <?php echo $_SESSION["product1"]; ?></p>
<p id="add2">Prod 2: <?php echo $_SESSION["product2"]; ?></p>
<li><a href="#" onclick=ajax("add1")> Add product1 </a><br></li>
<li><a href="#" onclick=ajax("add2")> Add product2 </a><br></li>
</body>
</html>

The ajax.js is:

function ajax(str) {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }

    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(str).innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", str + ".php", true);
    xmlhttp.send();
}

And the add1.php is similar to add2.php:

<?php
session_start();
$_SESSION["product1"] += 1;
$response = 'Vinho 1: ' . $_SESSION["product1"];
echo $response;
session_write_close();
?>

What am I doing wrong? I just want to continue the session. I tried to get rid of session_write_close() everywhere but I still have the samewarning. Using @session_start() solves the problem but I think it is just hiding the warnings right?

By the way, what are the best pratices in terms of a shop cart? Should I use cookies? I am just starting and as far as I can see sessions only save data while you don't close the browser, so I am guessing it would be nice to store all data in a cookie for a couple of days and at start check if there is any cookie set. If someone can give me some good example of using simple cookies (and if there are any downsides of it) please tell me.

genesst
  • 1,333
  • 1
  • 10
  • 39
2Noob2Good
  • 159
  • 1
  • 12

1 Answers1

1

Possibly, you have some characters before your <?php tag.

If you don't see any character, open your files in hex-editor and probably you see some sort of BOMs.

Glavić
  • 42,781
  • 13
  • 77
  • 107
Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38
  • Oh god... I'm so ashamed right now. I just wasn't looking correctly at the 2 add.php files and when I checked for the 100th time I saw a tab before the opening php. – 2Noob2Good Dec 28 '13 at 18:28
  • It's ok! This is one of mistakes which happen only one time per life :) – Anton Bessonov Dec 28 '13 at 18:45