Skip to content

Instantly share code, notes, and snippets.

@harikt
Created May 15, 2014 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/acd34c21a799255cef2d to your computer and use it in GitHub Desktop.
Save harikt/acd34c21a799255cef2d to your computer and use it in GitHub Desktop.
Something is wrong. But now working fine
<?php
if (isset($_COOKIE['counter'])) {
$counter = $_COOKIE['counter'];
echo $counter;
$counter++;
setcookie('counter', $counter, time() + 60*60*24*30, '/', '', false, true);
} else {
setcookie('counter', 1, time() + 60*60*24*30, '/', '', false, true);
}
<?php
if (isset($_COOKIE['counter'])) {
$counter = $_COOKIE['counter'];
if ($counter%2 == 0) {
echo "Even";
} else {
echo "Odd";
}
echo $counter;
$counter++;
setcookie('counter', $counter, time() + 60*60*24*30, '/', '', false, true);
} else {
setcookie('counter', 1, time() + 60*60*24*30, '/', '', false, true);
}
@harikt
Copy link
Author

harikt commented May 15, 2014

Built in server cookie functionality

php -S localhost:8000 cookie1.php

When the code was cookie1.php I was getting cookie values multiples of 2 on each request. 2, 4, 6 etc.

So I made a check to see whether 2 request are going. But now it is again with the correct value. May be as https://twitter.com/f_u_e_n_t_e/status/466854136647270400 mentioned there may be some hidden request ?

@ramondelafuente
Copy link

What requests do you see in the console when running the php -S command? They should show up there.. otherwise, also open up the network tab in the debugger of your browser to see what requests are being done.

@ramondelafuente
Copy link

Keep in mind that all requests are routed to the file you point to as index (cookie1.php).
From the manual:

"If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser."

@m6w6
Copy link

m6w6 commented May 15, 2014

Favicon!

@harikt
Copy link
Author

harikt commented Aug 6, 2014

@m6w6 no favicon is here see the php script .

@ramondelafuente Yes the script was started with php -S . Sorry for the delay guyes, I missed to see the comments. I am not sure why gist comments are not mailed by github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment