cookie basics again 
Author Message
 cookie basics again

I tried the following:

<?php
        if (isset($_COOKIE['myCookie'])) {
                setCookie("myCookie", ++$theValue);
                echo $_COOKIE['myCookie'];
        }
        else setCookie("myCookie", $theValue=1);
?>

I had hoped that this would check for the existence of myCookie, if it
exists increment its value, and print out its value.  It appears that
the script does indeed create myCookie on the first visit, but fails
to perform the increment on subsequent visits; that is, when the
browser loads the page the first time it shows nothing, refreshing the
page shows the value of myCookie to be 1, and subsequent refreshings
show the value to be still 1.
What am I failing to understand?
Thanks for any help,
Gerard McConnell



Fri, 20 May 2005 22:13:40 GMT  
 cookie basics again

Quote:
> <?php
> if (isset($_COOKIE['myCookie'])) {
> setCookie("myCookie", ++$theValue);
> echo $_COOKIE['myCookie'];
> }
> else setCookie("myCookie", $theValue=1);
> ?>

Hint: where is $theValue defined and where does it get its value, noting
that each time your code is executed it gets a clean start ("stateless")?

See also http://php.net/error_reporting and set it to E_ALL

a.



Fri, 20 May 2005 22:34:23 GMT  
 cookie basics again

Quote:

>> <?php
>> if (isset($_COOKIE['myCookie'])) {
>> setCookie("myCookie", ++$theValue);
>> echo $_COOKIE['myCookie'];
>> }
>> else setCookie("myCookie", $theValue=1);
>> ?>
>Hint: where is $theValue defined and where does it get its value, noting
>that each time your code is executed it gets a clean start ("stateless")?

I thought that the line:
        else setCookie("myCookie", $theValue=1);

would only get executed if the cookie had not been set.  It appears
that the cookie does get set because the echo happens, so shouldn't
the increment happen in setCookie("myCookie", ++$theValue);  ?
Apologies for not understanding, I've spent about 6 hours trying
variations on the code and checking the online manual, but I just
can't seem to get it right.
Gerard McConnell

Quote:
>See also http://php.net/error_reporting and set it to E_ALL
>a.



Fri, 20 May 2005 22:59:58 GMT  
 cookie basics again
Quote:

> >> <?php
> >> if (isset($_COOKIE['myCookie'])) {
> >> setCookie("myCookie", ++$theValue);
> >> echo $_COOKIE['myCookie'];
> >> }
> >> else setCookie("myCookie", $theValue=1);
> >> ?>

> >Hint: where is $theValue defined and where does it get its value,
noting
> >that each time your code is executed it gets a clean start

("stateless")?

Quote:
> I thought that the line:
> else setCookie("myCookie", $theValue=1);

> would only get executed if the cookie had not been set.

True -- your problem is not the cookie! Try

    <?php
      echo $theValue;
      $theValue++;
      echo $theValue;
    ?>

and now open that page over and over again -- you will *not* see $theValue
increase. "Stateless".

You need something like

    <?php
      error_reporting( E_ALL );
      if (isset($_COOKIE['myCookie'])) {

        $theValue = $_COOKIE['myCookie'];
        setCookie("myCookie", ++$theValue);

        // of course, one could also use a single line, like
        // setCookie( 'myCookie', $_COOKIE['myCookie'] + 1 );

        // not too relevant at all; does NOT prove that the cookie
        // has been sent to, or accepted by, the browser:
        echo "New value: " . $_COOKIE['myCookie'];
      }
      else
      {
        setCookie("myCookie", 1);
      }
  ?>

Quote:
> shouldn't the increment happen in setCookie("myCookie", ++$theValue);


such mistakes!

a.



Sat, 21 May 2005 00:01:13 GMT  
 cookie basics again


Quote:


> >> <?php
> >> if (isset($_COOKIE['myCookie'])) {
> >> setCookie("myCookie", ++$theValue);
> >> echo $_COOKIE['myCookie'];
> >> }
> >> else setCookie("myCookie", $theValue=1);
> >> ?>

> >Hint: where is $theValue defined and where does it get its value, noting
> >that each time your code is executed it gets a clean start ("stateless")?
> I thought that the line:
> else setCookie("myCookie", $theValue=1);

> would only get executed if the cookie had not been set.  It appears
> that the cookie does get set because the echo happens, so shouldn't
> the increment happen in setCookie("myCookie", ++$theValue);  ?
> Apologies for not understanding, I've spent about 6 hours trying
> variations on the code and checking the online manual, but I just
> can't seem to get it right.
> Gerard McConnell

> >See also http://php.net/error_reporting and set it to E_ALL

> >a.

try something along the lines of

<?php
if(isset($_COOKIE["myCookie"])) {
    $_COOKIE["myCookie"]++;
    setCookie("myCookie", $_COOKIE["myCookie"]);
    echo $_COOKIE["myCookie"];

Quote:
} else {

    setCookie("myCookie", 1);
    echo "Cookie Set";
Quote:
}

?>


Sat, 21 May 2005 00:49:28 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. cookie once again

2. Basic Cookie question

3. cookie basics

4. Scheme vs ML again and again and again and again

5. Scheme vs ML again and again and again and again

6. ANNOUNCE: Cookie.py A module to manage create web cookies

7. Toss your cookies (Mozilla cookie file filter)

8. Http basic authentication example (again)

9. APL and J (again and again) (long)

10. APL and J (again and again)

11. 'SQL Server Login': Again, again, ...

12. static vs. dynamic typing (again, again)

 

 
Powered by phpBB® Forum Software