A simple problem so I thought...

Setting a cookie in a php file like so:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?

// set cookies
setcookie ("gamelevel", "jv", time()+3600, "/");
if(isset($_COOKIE['gamelevel']))
{
echo "Cookie is set ";
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>JV Football Concessions</title>

will not set the cookie. I get an error stating "Cannot modify header information - headers already sent by...". When I look this up it tells me to look for blank space after my php, which I've done throughout my code. I can go into Firefox alsoand it is not set. And I have cookied enabled in my browser. Any other ideas?

    You cannot do a setcookie() or any other functions that send headers after you've sent output to the client. Just move your cookie-setting PHP code to the very top of the file (before your <html> tag) and make sure there are no spaces or line-feeds before the opening <?php tag.

      Ok I fixed the error "Cannot modify header information - headers already sent by...", but I still cannot read the value of the cookie set. This is how I'm reading it:

      echo $HTTP_COOKIE_VARS['gamelevel'];

      also tried:
      echo $HTTP_COOKIE_VARS['gamelevel'];

      neither work. This is a php file, and I can see thru Firefox that the cookie was set. I'd really like to assign the cookie to a variable, but since I can't even print it out at this point... Any help would be appreciated.

        You used $_COOKIE initially and then $HTTP_COOKIE_VARS for this

        Why did you change??

        Try echoing using $_COOKIE

          my bad - typo - I've used _COOKIE in all cases and it still does not show up. argh!

            setcookie() sends a cookie to the client. It does not set any variables. The $_COOKIE array gets populated when the client sends a HTTP request to your server and the client currently has an active cookie for your domain. If you refresh the page, at that point the cookie should be set from the previous page access.

              OK, so can I do the following: (I've never worked with cookies before obviously)
              1. set a cookie in page1.php (setcookie ("gamelevel", "jv", time()+3600, "/")😉

              1. go to page2.php and read the cookie (this DOES NOT work)
                $gamelevel=$_COOKIE['gamelevel'];
                echo "cookie gamelevel is $gamelevel"

              2. then, still on page2.php, use the variable $gamelevel to retrieve some data from a db (this works), then assign the php variable (from the cookie) to a javascript variable:

              // assign this to javascript for later
              echo "<script language=\"javascript\">";
              echo "var level = $gamelevel";
              echo "</script>";

              1. still on page2.php, open a popup window via javascript, and pass the variable level thru to page3.php

              2. go to page3.php and read the javascript variable again, to retrieve more data

              Does this make sense?

                Do you need this information stored for when the user comes back at a later date after closing the browser.

                If not you could use $_SESSION variables instead.

                  Write a Reply...