Jump to content

Archived

This topic is now archived and is closed to further replies.

Eugene

Hate cookies...

Recommended Posts

I tried this code (found on php.net)...

[code=php:0]
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
  foreach ($_COOKIE['cookie'] as $name => $value) {
      echo "$name : $value <br />\n";
  }
}
?>
[/code]

Nothing... Nothing happens, I tried even putting it ALL the way at the top of the page, NOTHING. Can someone please explain to me why this is happening? I DO HAVE COOKIES SET.

Share this post


Link to post
Share on other sites
I believe if (isset($_COOKIE['cookie']  .. is wrong! you dont have a cookie called "cookie" you have them named "cookie[one]" etc so try if (isset($_COOKIE['cookie[one]']... but again Im not sure if you can have [] inside []

Share this post


Link to post
Share on other sites
[quote author=glenelkins link=topic=101945.msg403960#msg403960 date=1153929461]
I believe if (isset($_COOKIE['cookie']  .. is wrong! you dont have a cookie called "cookie" you have them named "cookie[one]" etc so try if (isset($_COOKIE['cookie[one]']... but again Im not sure if you can have [] inside []
[/quote]
[code=php:0]
<?php
// set the cookies
setcookie("cookiethree", "cookiethree");

// after the page reloads, print them out
if (isset($_COOKIE['cookiethree'])) {
  foreach ($_COOKIE['cookiethree'] as $name => $value) {
      echo "$name : $value <br />\n";
  }
}
?>
[/code]

Tried that, doesn't work. :(

Share this post


Link to post
Share on other sites
Run the script again. Then refresh the browser window. You should now get your cookie back. If you dont make sure you have cookies enabled.

Why do you need to refresh the window? This is becuase, when you first run the script, you are setting the cookie. But when you refresh the browser window. The cookie is retrieved. Its to do with an inviside set of information a browser sends with every request which is called the header. So on every page reguest the browser firstt checks whether there is any valid cookies set for your site, or any site are visiting. If there are cooies for that site it'll send the cookie data with the header information. Then PHP processes this header information getting cookies. This why you have to refresh your browser window in order to get the cookie you have just set.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=101945.msg403971#msg403971 date=1153930382]
Run the script again. Then refresh the browser window. You should now get your cookie back. If you dont make sure you have cookies enabled.

Why do you need to refresh the window? This is becuase, when you first run the script, you are setting the cookie. But when you refresh the browser window. The cookie is retrieved. Its to do with an inviside set of information a browser sends with every request which is called the header. So on every page reguest the browser firstt checks whether there is any valid cookies set for your site, or any site are visiting. If there are cooies for that site it'll send the cookie data with the header information. Then PHP processes this header information getting cookies. This why you have to refresh your browser window in order to get the cookie you have just set.
[/quote]

Got it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.