Skip to content
Advertisement

Can cookie values be set after html has loaded on a page?

I read that cookie values can be set after HTML content has been sent from a PHP script to the web server.

However, when working with this, I have not been able to set the cookies as I had hoped. Can someone straighten out my discrepancy?

Advertisement

Answer

PHP can only set cookies while sending the headers of the page (that is, before sending the content – the HTML). This is how the HTTP protocol works regarding cookies, the server can send a request to the browser to store cookies.

If you wish to add cookies after the page loaded, you can do it via JavaScript.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement