Cookies in Php... | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
- 1

Cookies in Php...

Hi, I started to study php a few days ago and I discovered the cookies... As far as I understood, you need to set the cookie at the beginning of the code and before the html... Then, if this instruction is read every time the page is read by the navigator, does it mean that the cookie is set at the opening of each page? Thanks for any answer!

13th Aug 2018, 10:01 PM
Kant1 - avatar
2 Respostas
+ 4
Cookies are set in the HTTP headers, which are sent before the content (e.g. the html). If you try to set cookies once content has been started, PHP will bomb out: "Headers already sent" One way to work around this is output buffering (see ob_start) For various reasons, you cannot send headers from SoloLearn's PHP implementation. This is not a bug. While yes, setting the header generally means a cookie is set at the navigator... ...browsers, proxies, addons and users can easily reject them. Note that cookies are tricky to do correctly; as a gentle suggestion, please include secure cookie handling in your thoughts.
14th Aug 2018, 5:28 AM
Kirk Schafer
Kirk Schafer - avatar
Thanks for your quick answer! It makes it clearer in my mind... However, how can we use cookies inside the page if they are reset at the opening of the page? I mean, imagine you have the username cookie set at 'mypseudo' on the visitor's computer. If the cookie is reset at the opening of page, when it is created, I guess the information saved in the cookie is erased? Moreover, how do you save variables inside the cookie if it is created at the top of page, when the variables are all at NULL...? Thanks!
14th Aug 2018, 12:51 PM
Kant1 - avatar