How to set cookie with php?

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

    How to set cookie with php?

    How to set cookie with php?

    Could someone sanity check this for me? Still new to php...

    ***This is correct:

    <?php
    setcookie("myco okie",time(),36 00,"/");
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    </head>
    <body>
    </body>
    <?php
    // php code here
    ?>
    </html>

    ***This is incorrect:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    </head>
    <body>
    </body>
    <?php
    // php code here
    ?>
    <?php
    setcookie("myco okie",time(),36 00,"/");
    ?>
    </html>

    Does this sound right?


  • Janwillem Borleffs

    #2
    Re: How to set cookie with php?

    deko wrote:[color=blue]
    > How to set cookie with php?
    >
    > Could someone sanity check this for me? Still new to php...[/color]
    [...][color=blue]
    > Does this sound right?[/color]

    Yep, the reason is that setting a cookie involves sending the appriopriate
    headers to the browser. Headers must be send prior to the content (white
    space at the beginning of your script, before the opening php-tag is also
    regarded as content).

    You can hold back sending the content by applying output buffering, but
    that's another story.


    JW



    Comment

    • deko

      #3
      Re: How to set cookie with php?

      > Yep, the reason is that setting a cookie involves sending the appriopriate[color=blue]
      > headers to the browser. Headers must be send prior to the content (white
      > space at the beginning of your script, before the opening php-tag is also
      > regarded as content).
      >
      > You can hold back sending the content by applying output buffering, but
      > that's another story.[/color]

      Great - thanks. But does this mean the page will always fail validation,
      such as offered by http://validator.w3.org/ ?


      Comment

      • Michael Fesser

        #4
        Re: How to set cookie with php?

        .oO(deko)
        [color=blue]
        >How to set cookie with php?
        >
        >Could someone sanity check this for me? Still new to php...
        >[...]
        >Does this sound right?[/color]

        Depends.

        A response from a webserver consists of two parts: the response header,
        containing informations about the returned data, and the response body,
        containing the data itself. Cookies are part of the header and have to
        be sent along with them before any other output. This is how it's done
        in your first example. The second is also correct, but requires output
        buffering (i.e. keeping the output in a buffer on the server until all
        headers are sent to the client).

        Micha

        Comment

        • Janwillem Borleffs

          #5
          Re: How to set cookie with php?

          deko wrote:[color=blue]
          > Great - thanks. But does this mean the page will always fail
          > validation, such as offered by http://validator.w3.org/ ?[/color]

          No, because the php code will be parsed before sending thus invisible for
          the requestor (browser, validator, etc.)


          JW



          Comment

          Working...