Jump to content


Photo

Hate cookies...


  • Please log in to reply
4 replies to this topic

#1 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 26 July 2006 - 03:49 PM

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

<?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";
   }
}
?> 

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.

#2 glenelkins

glenelkins
  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 26 July 2006 - 03:57 PM

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 []
http://thewebsolutionprovider.com - A Solution For Everything Web!

#3 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 26 July 2006 - 04:02 PM

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 []

<?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";
   }
}
?> 

Tried that, doesn't work. :(

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 July 2006 - 04:13 PM

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.

#5 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 26 July 2006 - 04:25 PM

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.


Got it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users