Hi

setCookie has that error msg and here is the code:

if (isset($_COOKIE['last_currency_id'])) //if 'last_currency_id' was set in cookie, I want to get the data from DB.
{ 
   $currency_id = $_COOKIE['last_currency_id'];
   list($currency_symbol, $exchange_rate) = getCurrencyInfo($currency_id); 
   //it returns the following data:
   echo $currency_id;
   echo $currency_symbol;
   echo $exchange_rate;	   
} else //if no cookie was set, then search the default currency from DB and set them in cookie { $data = getDefaultCurrency(); list($currency_id, $currency_symbol, $exchange_rate) = $data; setcookie("last_currency_id", $currency_id, $year10); setcookie("last_currency_symbol", $currency_symbol, $year10); setcookie("last_currency_exchange_rate", $exchange_rate, $year10); }

However, it showed the error "Warning: Cannot modify header information - headers already sent by "

How can I solve it?

    Use setcookie() before printing anything, including blank lines at the start.

      I deleted
      //echo $currency_id;
      //echo $currency_symbol;
      //echo $exchange_rate;
      and still has the same error.

        Those echoes aren't your problem. You can't have any space before any php tags in the script itself, and any that it includes - so check those. The error message should tell you where output started, so look there first.

          Thanks, I fixed it. I have another problem. There is a radio button combo to list out all currency. The checked currency is the cookie's currency_id. How can I select other currency from the radio button and the cookie's $currency_id is reset.

          Here is the code of the radio button:

          <?php
          $query = 'SELECT * FROM currency';
          $result = mysql_query($query) or die('Error : ' . mysql_error());
          while ($row = mysql_fetch_assoc($result)) {
          echo '<input type="radio" name="cur" value="' . $row['currency_id']
          . '"' . ($row['currency_id'] == $currency_id ? 'checked="checked"' : '') . '>'
          . $row['currency_symbol'] . "<br>\n";			   
          } ?>

          where $currency_id is from the cookie.

            Somewhere before that loop then check for form data, then use setcookie to set it to a new value. Remember that in the script $_COOKIE will still contain the old value, so either manually update it or set your $currency_id variable separately from that.

              Write a Reply...