Cookie problems (simple code included)

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

    Cookie problems (simple code included)

    Hi all,

    I'm trying to develop a website that selects from 3 stylesheets
    depending on which style they have chosen. Their preference is stored
    in a cookie and this is where the problems begin. I have boiled my code
    down to the bare essentials in the hope that someone can help me with
    this problem. I would be grateful if you could explain what I have done
    is wrong too, because I read that cookie settings should be done before
    the <htmltag, which is what I have done.

    === CODE (this can be copied and pasted into a php file) ===

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <?php
    $cstyle = (int)$_COOKIE["cstyle"];
    print("***$csty le***");

    if ($cstyle < 1 || $cstyle 3)
    {
    $cstyle = 1;
    }

    @extract($_POST );
    $cstylenew = (int)$_GET['cstyle'];

    if ($cstylenew >= 1 && $cstylenew <= 3)
    {
    setcookie("csty le3", "$cstylenew ", time()+5000000) ;
    }
    ?>

    <html>

    <head>
    <link href="style<?ph p print($cstyle); ?>.css" rel="stylesheet "
    type="text/css">
    </head>

    <body>
    <br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=1">st yle 1</a><br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=2">st yle 2</a><br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=3">st yle 3</a><br>
    </body>
    </html>

    Best regards,
    ii2o

  • Kim André Akerø

    #2
    Re: Cookie problems (simple code included)

    ii2o wrote:
    Hi all,
    >
    I'm trying to develop a website that selects from 3 stylesheets
    depending on which style they have chosen. Their preference is stored
    in a cookie and this is where the problems begin. I have boiled my
    code down to the bare essentials in the hope that someone can help me
    with this problem. I would be grateful if you could explain what I
    have done is wrong too, because I read that cookie settings should be
    done before the <htmltag, which is what I have done.
    >
    === CODE (this can be copied and pasted into a php file) ===
    >
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <?php
    $cstyle = (int)$_COOKIE["cstyle"];
    print("***$csty le***");
    >
    if ($cstyle < 1 || $cstyle 3)
    {
    $cstyle = 1;
    }
    >
    @extract($_POST );
    $cstylenew = (int)$_GET['cstyle'];
    >
    if ($cstylenew >= 1 && $cstylenew <= 3)
    {
    setcookie("csty le3", "$cstylenew ", time()+5000000) ;
    }
    ?>
    >
    <html>
    >
    <head>
    <link href="style<?ph p print($cstyle); ?>.css" rel="stylesheet "
    type="text/css">
    </head>
    >
    <body>
    <br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=1">st yle 1</a><br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=2">st yle 2</a><br>
    <a href="<?php print("$PHP_SEL F"); ?>?cstyle=3">st yle 3</a><br>
    </body>
    </html>
    >
    Best regards,
    ii2o
    Cookies are set in the headers, meaning that setcookie() must be called
    before *any* output is sent to the user (even before your DOCTYPE
    statement). Make sure that you don't have any blank spaces before <?php
    as well.

    --
    Kim André Akerø
    - kimandre@NOSPAM betadome.com
    (remove NOSPAM to contact me directly)

    Comment

    • ii2o

      #3
      Re: Cookie problems (simple code included)

      Hi there,

      Thanks for the response. Unfortunately even when I move the doctype
      statement to below the php code, I still get the following error
      "Warning: Cannot modify header information - headers already sent by
      (output started at d:\testserver\t estcookies.php: 3) in
      d:\testserver\t estcookies.php on line 14".

      When you mean before any output is sent to the user, do you mean that I
      can't use the $_GET statement? If so, how is possible to select a
      stylesheet in the code without accessing this variable?

      I'm very confused!

      Thanks,
      ii2o


      Kim André Akerø wrote:
      ii2o wrote:
      >
      Hi all,

      I'm trying to develop a website that selects from 3 stylesheets
      depending on which style they have chosen. Their preference is stored
      in a cookie and this is where the problems begin. I have boiled my
      code down to the bare essentials in the hope that someone can help me
      with this problem. I would be grateful if you could explain what I
      have done is wrong too, because I read that cookie settings should be
      done before the <htmltag, which is what I have done.

      === CODE (this can be copied and pasted into a php file) ===

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <?php
      $cstyle = (int)$_COOKIE["cstyle"];
      print("***$csty le***");

      if ($cstyle < 1 || $cstyle 3)
      {
      $cstyle = 1;
      }

      @extract($_POST );
      $cstylenew = (int)$_GET['cstyle'];

      if ($cstylenew >= 1 && $cstylenew <= 3)
      {
      setcookie("csty le3", "$cstylenew ", time()+5000000) ;
      }
      ?>

      <html>

      <head>
      <link href="style<?ph p print($cstyle); ?>.css" rel="stylesheet "
      type="text/css">
      </head>

      <body>
      <br>
      <a href="<?php print("$PHP_SEL F"); ?>?cstyle=1">st yle 1</a><br>
      <a href="<?php print("$PHP_SEL F"); ?>?cstyle=2">st yle 2</a><br>
      <a href="<?php print("$PHP_SEL F"); ?>?cstyle=3">st yle 3</a><br>
      </body>
      </html>

      Best regards,
      ii2o
      >
      Cookies are set in the headers, meaning that setcookie() must be called
      before *any* output is sent to the user (even before your DOCTYPE
      statement). Make sure that you don't have any blank spaces before <?php
      as well.
      >
      --
      Kim André Akerø
      - kimandre@NOSPAM betadome.com
      (remove NOSPAM to contact me directly)

      Comment

      • Kim André Akerø

        #4
        Re: Cookie problems (simple code included)

        ii2o wrote:
        Kim André Akerø wrote:
        ii2o wrote:
        Hi all,
        >
        I'm trying to develop a website that selects from 3 stylesheets
        depending on which style they have chosen. Their preference is
        stored in a cookie and this is where the problems begin. I have
        boiled my code down to the bare essentials in the hope that
        someone can help me with this problem. I would be grateful if you
        could explain what I have done is wrong too, because I read that
        cookie settings should be done before the <htmltag, which is
        what I have done.
        >
        === CODE (this can be copied and pasted into a php file) ===
        >
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <?php
        $cstyle = (int)$_COOKIE["cstyle"];
        print("***$csty le***");
        >
        if ($cstyle < 1 || $cstyle 3)
        {
        $cstyle = 1;
        }
        >
        @extract($_POST );
        $cstylenew = (int)$_GET['cstyle'];
        >
        if ($cstylenew >= 1 && $cstylenew <= 3)
        {
        setcookie("csty le3", "$cstylenew ", time()+5000000) ;
        }
        ?>
        >
        <html>
        >
        <head>
        <link href="style<?ph p print($cstyle); ?>.css" rel="stylesheet "
        type="text/css">
        </head>
        >
        <body>
        <br>
        <a href="<?php print("$PHP_SEL F"); ?>?cstyle=1">st yle
        1</a><br <a href="<?php print("$PHP_SEL F");
        ?>?cstyle=2">st yle 2</a><br <a href="<?php
        print("$PHP_SEL F"); ?>?cstyle=3">st yle 3</a><br </body>
        </html>
        >
        Best regards,
        ii2o
        Cookies are set in the headers, meaning that setcookie() must be
        called before any output is sent to the user (even before your
        DOCTYPE statement). Make sure that you don't have any blank spaces
        before <?php as well.
        >
        Hi there,
        >
        Thanks for the response. Unfortunately even when I move the doctype
        statement to below the php code, I still get the following error
        "Warning: Cannot modify header information - headers already sent by
        (output started at d:\testserver\t estcookies.php: 3) in
        d:\testserver\t estcookies.php on line 14".
        >
        When you mean before any output is sent to the user, do you mean that
        I can't use the $_GET statement? If so, how is possible to select a
        stylesheet in the code without accessing this variable?
        >
        I'm very confused!
        print() is considered sending output, too.

        --
        Kim André Akerø
        - kimandre@NOSPAM betadome.com
        (remove NOSPAM to contact me directly)

        Comment

        • ii2o

          #5
          Re: Cookie problems (simple code included)

          Brilliant - I see where I was going wrong now. Thanks for your help!
          ii2o

          Kim André Akerø wrote:
          ii2o wrote:
          >
          Kim André Akerø wrote:
          ii2o wrote:
          >
          Hi all,

          I'm trying to develop a website that selects from 3 stylesheets
          depending on which style they have chosen. Their preference is
          stored in a cookie and this is where the problems begin. I have
          boiled my code down to the bare essentials in the hope that
          someone can help me with this problem. I would be grateful if you
          could explain what I have done is wrong too, because I read that
          cookie settings should be done before the <htmltag, which is
          what I have done.

          === CODE (this can be copied and pasted into a php file) ===

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          <?php
          $cstyle = (int)$_COOKIE["cstyle"];
          print("***$csty le***");

          if ($cstyle < 1 || $cstyle 3)
          {
          $cstyle = 1;
          }

          @extract($_POST );
          $cstylenew = (int)$_GET['cstyle'];

          if ($cstylenew >= 1 && $cstylenew <= 3)
          {
          setcookie("csty le3", "$cstylenew ", time()+5000000) ;
          }
          ?>

          <html>

          <head>
          <link href="style<?ph p print($cstyle); ?>.css" rel="stylesheet "
          type="text/css">
          </head>

          <body>
          <br>
          <a href="<?php print("$PHP_SEL F"); ?>?cstyle=1">st yle
          1</a><br <a href="<?php print("$PHP_SEL F");
          ?>?cstyle=2">st yle 2</a><br <a href="<?php
          print("$PHP_SEL F"); ?>?cstyle=3">st yle 3</a><br </body>
          </html>

          Best regards,
          ii2o
          >
          Cookies are set in the headers, meaning that setcookie() must be
          called before any output is sent to the user (even before your
          DOCTYPE statement). Make sure that you don't have any blank spaces
          before <?php as well.
          Hi there,

          Thanks for the response. Unfortunately even when I move the doctype
          statement to below the php code, I still get the following error
          "Warning: Cannot modify header information - headers already sent by
          (output started at d:\testserver\t estcookies.php: 3) in
          d:\testserver\t estcookies.php on line 14".

          When you mean before any output is sent to the user, do you mean that
          I can't use the $_GET statement? If so, how is possible to select a
          stylesheet in the code without accessing this variable?

          I'm very confused!
          >
          print() is considered sending output, too.
          >
          --
          Kim André Akerø
          - kimandre@NOSPAM betadome.com
          (remove NOSPAM to contact me directly)

          Comment

          Working...