Setting a Cookie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Agent M

    #1

    Setting a Cookie

    Hi there

    New to PHP. Im trying to set a cookie but for some reason it wont work.
    The code is inside a Submit button and happens after I have read the MySQL
    database and succesfully retrieved the record. For some reason the cookie is
    not created.

    Here is the code

    setcookie("firs tname",$firstna me,time() + (7 * 86400));

    $firstname contains a string value. It is not empty.

    What can be wrong?

    Thanks



  • Pedro Graca

    #2
    Re: Setting a Cookie

    Agent M wrote:[color=blue]
    > New to PHP. Im trying to set a cookie but for some reason it wont work.
    > The code is inside a Submit button and happens after I have read the MySQL
    > database and succesfully retrieved the record. For some reason the cookie is
    > not created.[/color]
    (snip)[color=blue]
    > What can be wrong?[/color]

    Has the client /sent/ the cookie to the server?
    Only /received/ cookies are available in the $_COOKIE (or equivalent)
    variable.

    Try this:

    <?php
    echo '<p>cookie not set yet!</p>';
    if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>', $_COOKIE['test'], '</b></p>';
    setcookie('test ', 'true');
    echo '<p>cookie has been set!</p>';
    if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>', $_COOKIE['test'], '</b></p>';
    ?>

    Now open this page in your browser and see what happens ... then refresh
    the browser and see what happens


    The first time you open the page, the client will not have send the
    cookie (it doesn't have it); the second time the client sends the cookie
    (once, before the script starts -- it's the web server job to make it
    available to the script) and you'll see it in both echo's
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Eric Bohlman

      #3
      Re: Setting a Cookie

      Pedro Graca <hexkid@hotpop. com> wrote in
      news:c0eccq$169 6a1$2@ID-203069.news.uni-berlin.de:
      [color=blue]
      > Try this:
      >
      > <?php
      > echo '<p>cookie not set yet!</p>';
      > if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>',
      > $_COOKIE['test'], '</b></p>'; setcookie('test ', 'true');
      > echo '<p>cookie has been set!</p>';
      > if (isset($_COOKIE['test'])) echo '<p>Cookie is <b>',
      > $_COOKIE['test'], '</b></p>'; ?>
      >
      > Now open this page in your browser and see what happens ... then
      > refresh the browser and see what happens[/color]

      Not what you intend; setcookie() will always fail there because it has to
      send a header, which it can't do because you've already output non-header
      text.

      Comment

      • Pedro Graca

        #4
        Re: Setting a Cookie

        Eric Bohlman wrote:[color=blue]
        > Pedro Graca <hexkid@hotpop. com> wrote in
        > news:c0eccq$169 6a1$2@ID-203069.news.uni-berlin.de:
        >[color=green]
        >> <?php
        >> echo '<p>cookie not set yet!</p>';
        >> setcookie('test ', 'true');[/color][/color]

        Error in this line: headers already sent
        [color=blue][color=green]
        >> Now open this page in your browser and see what happens ... then
        >> refresh the browser and see what happens[/color]
        >
        > Not what you intend; setcookie() will always fail there because it has to
        > send a header, which it can't do because you've already output non-header
        > text.[/color]

        You're right, I was wrong; sorry everyone.

        The idea was to let the OP verify that cookies are not available for the
        script/page/access that sets them.

        The script that set the cookie send it to the client, and that's it!

        When the client revisits *it* send the cookie along with the request.
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        Working...