Setting cookies [URGENT]

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

    Setting cookies [URGENT]

    Can anyone tell me why this code does not do what I want it to:

    <?php
    if (!isset($_COOKI E['basket'])) {
    setcookie("bask et", $item_code, time()+60*60*24 *30, "coursework ", '',
    0) ;
    }
    else {
    setcookie( "basket", $_COOKIE['basket']-$item_code );
    }
    ?>

    The page GETs the variable $item_code from a URL and stores it in a cookie
    okay. When I revisit the page with a different $item_code I want the code to
    check if the cookie is set already and, if so, *append* the new $item_code
    to the existing cookie. Problem the old cookie is overwritten by the new one
    rather than appended.

    I've been trying all day to get this to work and whereas some other code
    I've looked at does append, mine doesn't, and I can't see where I am going
    wrong. Here is the other code that does work but it's no good as it is
    different t what I need:


    <?php
    error_reporting (1); // suppress warning messages
    if ($clear)
    setcookie("ckTr olleyContents", "", time()-3600);
    elseif ($choice)
    setcookie("ckTr olleyContents", "$ckTrolleyCont ents<li>$choice </li>");
    ?>

    <html><head><ti tle>PHP cookie example</title>
    </head><body>
    <h2>Shopping trolley (php cookie version) </h2>
    <form method="get" action="CookieT rolley.php">
    <p>Choose an item: </p>
    <p>
    <input type="radio" name="choice" value="camera"/> camera<br />
    <input type="radio" name="choice" value="snorkel"/> snorkel<br />
    <input type="radio" name="choice" value="octopus"/> octopus<br />
    <input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
    />
    <input type="submit" name="submit" value="Add to the trolley"/>
    <input type="submit" name="clear" value="Empty the trolley"/>
    </p>
    </form>

    <?php
    if ((!$choice && !$ckTrolleyCont ents) || $clear)
    echo "<p>Trolley currently empty</p>";
    elseif (!$choice) {
    echo "<p>Trolley contains:</p>";
    echo "<ul>$ckTrolley Contents</ul>";
    } else {
    // NB the cookie will not contain what has been added to it
    // during the current form execution
    echo "<p>Trolley contains:</p>";
    echo "<ul>$ckTrolley Contents<li>$ch oice</li></ul>";
    }
    ?>

    </body></html>


  • Fnark

    #2
    Re: Setting cookies [URGENT]

    There is a line that should read:

    setcookie( "basket", "$_COOKIE['basket']-$item_code" );

    Thanks


    "Fnark" <fake@reallyisf ake.com> wrote in message
    news:Sb1Kb.1749 5$Od5.11825@new s-binary.blueyond er.co.uk...[color=blue]
    > Can anyone tell me why this code does not do what I want it to:
    >
    > <?php
    > if (!isset($_COOKI E['basket'])) {
    > setcookie("bask et", $item_code, time()+60*60*24 *30, "coursework ",[/color]
    '',[color=blue]
    > 0) ;
    > }
    > else {
    > setcookie( "basket", $_COOKIE['basket']-$item_code );
    > }
    > ?>
    >
    > The page GETs the variable $item_code from a URL and stores it in a cookie
    > okay. When I revisit the page with a different $item_code I want the code[/color]
    to[color=blue]
    > check if the cookie is set already and, if so, *append* the new $item_code
    > to the existing cookie. Problem the old cookie is overwritten by the new[/color]
    one[color=blue]
    > rather than appended.
    >
    > I've been trying all day to get this to work and whereas some other code
    > I've looked at does append, mine doesn't, and I can't see where I am going
    > wrong. Here is the other code that does work but it's no good as it is
    > different t what I need:
    >
    >
    > <?php
    > error_reporting (1); // suppress warning messages
    > if ($clear)
    > setcookie("ckTr olleyContents", "", time()-3600);
    > elseif ($choice)
    > setcookie("ckTr olleyContents", "$ckTrolleyCont ents<li>$choice </li>");
    > ?>
    >
    > <html><head><ti tle>PHP cookie example</title>
    > </head><body>
    > <h2>Shopping trolley (php cookie version) </h2>
    > <form method="get" action="CookieT rolley.php">
    > <p>Choose an item: </p>
    > <p>
    > <input type="radio" name="choice" value="camera"/> camera<br />
    > <input type="radio" name="choice" value="snorkel"/> snorkel<br />
    > <input type="radio" name="choice" value="octopus"/> octopus<br />
    > <input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
    > />
    > <input type="submit" name="submit" value="Add to the trolley"/>
    > <input type="submit" name="clear" value="Empty the trolley"/>
    > </p>
    > </form>
    >
    > <?php
    > if ((!$choice && !$ckTrolleyCont ents) || $clear)
    > echo "<p>Trolley currently empty</p>";
    > elseif (!$choice) {
    > echo "<p>Trolley contains:</p>";
    > echo "<ul>$ckTrolley Contents</ul>";
    > } else {
    > // NB the cookie will not contain what has been added to it
    > // during the current form execution
    > echo "<p>Trolley contains:</p>";
    > echo "<ul>$ckTrolley Contents<li>$ch oice</li></ul>";
    > }
    > ?>
    >
    > </body></html>
    >
    >[/color]


    Comment

    • Pedro Graca

      #3
      Re: Setting cookies [URGENT]

      Fnark wrote:[color=blue]
      > There is a line that should read:
      >
      > setcookie( "basket", "$_COOKIE['basket']-$item_code" );[/color]

      curly braces inside double quotes
      setcookie( "basket", "{$_COOKIE['basket']}-$item_code" );

      but I prefer always using single quotes and concatenation :-)
      setcookie('bask et', $_COOKIE['basket'] . '-' . $item_code);


      refer to the manual page on strings for (sic) "Complex syntax"

      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Fnark

        #4
        Re: Setting cookies [URGENT]

        Thanks for your reply.

        I tried the second method previously ( and again for good measure) but it
        still overwrites my cookie. If a cookie is set where the $item_code is '1'
        the cookie value is '1'. But the next time I enter the page where the
        $item_code is '2' the cookie is '2' not '1-2' which is what I want as I need
        to store multiple $item_code(s) in my cookie.

        It's driving me nuts as the other code I posted appended to the cookie but
        mine doesn't even if I try the same way. Incidentally the writer of the
        other code (in a book) said that appending takes place automatically - only
        it won't in my case. Could it be that I use GET where he uses POST?.

        Mark


        "Pedro Graca" <hexkid@hotpop. com> wrote in message
        news:bta9t2$52i q5$1@ID-203069.news.uni-berlin.de...[color=blue]
        > Fnark wrote:[color=green]
        > > There is a line that should read:
        > >
        > > setcookie( "basket", "$_COOKIE['basket']-$item_code" );[/color]
        >
        > curly braces inside double quotes
        > setcookie( "basket", "{$_COOKIE['basket']}-$item_code" );
        >
        > but I prefer always using single quotes and concatenation :-)
        > setcookie('bask et', $_COOKIE['basket'] . '-' . $item_code);
        >
        >
        > refer to the manual page on strings for (sic) "Complex syntax"
        >[/color]
        http://www.php.net/manual/en/languag...arsing.complex[color=blue]
        > --
        > --= my mail box only accepts =--
        > --= Content-Type: text/plain =--
        > --= Size below 10001 bytes =--[/color]


        Comment

        • Rahul Anand

          #5
          Re: Setting cookies [URGENT]

          It should be:

          setcookie( "basket", "$_COOKIE[basket]-$item_code" );
          OR
          setcookie( "basket", $_COOKIE['basket'].'-'.$item_code);

          More efficient and error free.

          --
          Rahul


          "Fnark" <fake@reallyisf ake.com> wrote in message news:<0z1Kb.177 64$Od5.7832@new s-binary.blueyond er.co.uk>...[color=blue]
          > There is a line that should read:
          >
          > setcookie( "basket", "$_COOKIE['basket']-$item_code" );
          >
          > Thanks[/color]

          Comment

          • Rahul Anand

            #6
            Re: Setting cookies [URGENT]

            Hi Mark,

            I tried this code at my system and it is working fine:

            <SNIP>

            $item_code = $_GET['item_code'];
            if (!isset($_COOKI E['basket']))
            setcookie('bask et', $item_code, time()+60*60*24 *30, "/", '',0) ;
            else
            setcookie('bask et', $_COOKIE['basket'].'-'.$item_code ) ;

            print_r($_COOKI E);

            </SNIP>

            Please not a cookie set in a script will be available in $_COOKIE from
            next execution only.

            I think problem must be in some other code.

            Enabling your Notice Error may help in finding the problem code.

            --
            Rahul


            "Fnark" <fake@reallyisf ake.com> wrote in message news:<IF3Kb.634 $K41.329@news-binary.blueyond er.co.uk>...[color=blue]
            > Thanks for your reply.
            >
            > I tried the second method previously ( and again for good measure) but it
            > still overwrites my cookie. If a cookie is set where the $item_code is '1'
            > the cookie value is '1'. But the next time I enter the page where the
            > $item_code is '2' the cookie is '2' not '1-2' which is what I want as I need
            > to store multiple $item_code(s) in my cookie.
            >
            > It's driving me nuts as the other code I posted appended to the cookie but
            > mine doesn't even if I try the same way. Incidentally the writer of the
            > other code (in a book) said that appending takes place automatically - only
            > it won't in my case. Could it be that I use GET where he uses POST?.
            >
            > Mark[/color]

            Comment

            • Fnark

              #7
              Re: Setting cookies [URGENT]

              Thanks Rahul,

              The code works for me to an extent. Did your code append to the cookie on
              subsequent visits? My code writes a cookie with an initial value but then
              overwrites it (not appends) on subsequent visits to the page.

              I have a feeling the code is not getting to the else statement and always
              executing the if statement yet I can't see why this should be so. When I

              print_r($_COOKI E);

              I only get the output Array() do you know how to get the contents of that
              array to print?


              Incidentally, if you like you may test out my code page here



              (Yes it is my coursework but our tutors have no problems with us asking for
              help from our peers debugging code so long as we have made an effort to
              write it ourselves as this is common practice and expected in industry -
              plus I've been stuck on this for days and can't find a solution anywhere!!
              :0) )

              Thanks
              Mark


              "Rahul Anand" <rahulanand_bis @rediffmail.com > wrote in message
              news:628e2f7b.0 401050052.1c5d4 123@posting.goo gle.com...[color=blue]
              > Hi Mark,
              >
              > I tried this code at my system and it is working fine:
              >
              > <SNIP>
              >
              > $item_code = $_GET['item_code'];
              > if (!isset($_COOKI E['basket']))
              > setcookie('bask et', $item_code, time()+60*60*24 *30, "/", '',0) ;
              > else
              > setcookie('bask et', $_COOKIE['basket'].'-'.$item_code ) ;
              >
              > print_r($_COOKI E);
              >
              > </SNIP>
              >
              > Please not a cookie set in a script will be available in $_COOKIE from
              > next execution only.
              >
              > I think problem must be in some other code.
              >
              > Enabling your Notice Error may help in finding the problem code.
              >
              > --
              > Rahul
              >
              >
              > "Fnark" <fake@reallyisf ake.com> wrote in message[/color]
              news:<IF3Kb.634 $K41.329@news-binary.blueyond er.co.uk>...[color=blue][color=green]
              > > Thanks for your reply.
              > >
              > > I tried the second method previously ( and again for good measure) but[/color][/color]
              it[color=blue][color=green]
              > > still overwrites my cookie. If a cookie is set where the $item_code is[/color][/color]
              '1'[color=blue][color=green]
              > > the cookie value is '1'. But the next time I enter the page where the
              > > $item_code is '2' the cookie is '2' not '1-2' which is what I want as I[/color][/color]
              need[color=blue][color=green]
              > > to store multiple $item_code(s) in my cookie.
              > >
              > > It's driving me nuts as the other code I posted appended to the cookie[/color][/color]
              but[color=blue][color=green]
              > > mine doesn't even if I try the same way. Incidentally the writer of the
              > > other code (in a book) said that appending takes place automatically -[/color][/color]
              only[color=blue][color=green]
              > > it won't in my case. Could it be that I use GET where he uses POST?.
              > >
              > > Mark[/color][/color]


              Comment

              • Fnark

                #8
                Re: Setting cookies [SOLVED]

                Problem was I did not put the correct full path in the cookie.

                D'OH!! *slaps forehead*


                "Fnark" <fake@reallyisf ake.com> wrote in message
                news:NmdKb.8954 $K41.3465@news-binary.blueyond er.co.uk...[color=blue]
                > Thanks Rahul,
                >
                > The code works for me to an extent. Did your code append to the cookie on
                > subsequent visits? My code writes a cookie with an initial value but then
                > overwrites it (not appends) on subsequent visits to the page.
                >
                > I have a feeling the code is not getting to the else statement and always
                > executing the if statement yet I can't see why this should be so. When I
                >
                > print_r($_COOKI E);
                >
                > I only get the output Array() do you know how to get the contents of that
                > array to print?
                >
                >
                > Incidentally, if you like you may test out my code page here
                >
                > http://stuweb.cms.gre.ac.uk/~jm925/w...ork/basket.php
                >
                > (Yes it is my coursework but our tutors have no problems with us asking[/color]
                for[color=blue]
                > help from our peers debugging code so long as we have made an effort to
                > write it ourselves as this is common practice and expected in industry -
                > plus I've been stuck on this for days and can't find a solution anywhere!!
                > :0) )
                >
                > Thanks
                > Mark
                >
                >
                > "Rahul Anand" <rahulanand_bis @rediffmail.com > wrote in message
                > news:628e2f7b.0 401050052.1c5d4 123@posting.goo gle.com...[color=green]
                > > Hi Mark,
                > >
                > > I tried this code at my system and it is working fine:
                > >
                > > <SNIP>
                > >
                > > $item_code = $_GET['item_code'];
                > > if (!isset($_COOKI E['basket']))
                > > setcookie('bask et', $item_code, time()+60*60*24 *30, "/", '',0) ;
                > > else
                > > setcookie('bask et', $_COOKIE['basket'].'-'.$item_code ) ;
                > >
                > > print_r($_COOKI E);
                > >
                > > </SNIP>
                > >
                > > Please not a cookie set in a script will be available in $_COOKIE from
                > > next execution only.
                > >
                > > I think problem must be in some other code.
                > >
                > > Enabling your Notice Error may help in finding the problem code.
                > >
                > > --
                > > Rahul
                > >
                > >
                > > "Fnark" <fake@reallyisf ake.com> wrote in message[/color]
                > news:<IF3Kb.634 $K41.329@news-binary.blueyond er.co.uk>...[color=green][color=darkred]
                > > > Thanks for your reply.
                > > >
                > > > I tried the second method previously ( and again for good measure) but[/color][/color]
                > it[color=green][color=darkred]
                > > > still overwrites my cookie. If a cookie is set where the $item_code is[/color][/color]
                > '1'[color=green][color=darkred]
                > > > the cookie value is '1'. But the next time I enter the page where the
                > > > $item_code is '2' the cookie is '2' not '1-2' which is what I want as[/color][/color][/color]
                I[color=blue]
                > need[color=green][color=darkred]
                > > > to store multiple $item_code(s) in my cookie.
                > > >
                > > > It's driving me nuts as the other code I posted appended to the cookie[/color][/color]
                > but[color=green][color=darkred]
                > > > mine doesn't even if I try the same way. Incidentally the writer of[/color][/color][/color]
                the[color=blue][color=green][color=darkred]
                > > > other code (in a book) said that appending takes place automatically -[/color][/color]
                > only[color=green][color=darkred]
                > > > it won't in my case. Could it be that I use GET where he uses POST?.
                > > >
                > > > Mark[/color][/color]
                >
                >[/color]


                Comment

                Working...