PHP: Cookies = Global Variables?

  • Thread starter Thread starter Jon-o
  • Start date Start date
J

Jon-o

Guest
While I was coding just a few minutes ago, I found that some seemingly new, empty variables in my PHP code already had data in them. After lots of investigating, I found that these local PHP variables were sharing names with Javascript cookies I placed in the browser from other pages.

I am setting the cookies using Javascript, ie setCookie('bananaCount','25', '1000'), then on a completely different page, I've found that if I enter the following code: <?php echo $bananaCount; ?> I'll get "25".

I found some tutorials that describe the proper way to retrieve cookies with PHP: echo $_COOKIE["user"]; but haven't found anything discussing this weird phenomenon.

Is this a new feature of PHP? Though it may not be recommended, is it safe to use cookies as global PHP variables? I haven't seen anything mentioned on this topic anywhere, any leads would be appreciated.
I understand what cookies are. My question pertains to the use of $_COOKIE["bananaCount"] vs $bananaCount because PHP seems to use both to recall cookies. Why include "$_COOKIE" feature if you can simply use the $variable function? Are there limitations or risks involved with this?
 
Back
Top