1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Cookies in PHP

Discussion in 'Software' started by Poppa_joe, 30 Sep 2005.

  1. Poppa_joe

    Poppa_joe What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    22
    Likes Received:
    0
    I have a strange problem. I can not create cookies using this code. But if I run the example code using exactly the same syntax the cookies work fine.
    Here is my code that is not working:
    <?php
    $checkbox1= $_POST['check2'];
    echo $checkbox1; //this line works properly
    //make cookie

    setcookie("assign", $checkbox1);
    setcookie("assign", $checkbox1, time()+3600, ".joe.tolumalaska.com", 1);

    echo $_COOKIE["assign"]; //this line does not display anything
    ?>

    Here is the example code that works fine:

    <?php
    $value = 'something from somewhere';

    setcookie("TestCookie", $value);
    //setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */
    setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".joe.tolumalaska.com", 1);
    ?>

    <?php
    // Print an individual cookie
    echo $_COOKIE["TestCookie"];
    //echo $HTTP_COOKIE_VARS["TestCookie"];

    // Another way to debug/test is to view all cookies
    //print_r($_COOKIE);
    ?>
     
  2. simon w

    simon w What's a Dremel?

    Joined:
    3 Nov 2003
    Posts:
    1,302
    Likes Received:
    0
    Cookies must be set before printing anything so you need to get rid of the echo command - check http://php.net/setcookie
     

Share This Page