I
I
Ilyusha Prokopiev2015-10-25 22:44:14
PHP
Ilyusha Prokopiev, 2015-10-25 22:44:14

Why cookies can only be created at the beginning?

Why did they make this restriction? I always encounter problems associated with this, but I have not found any pluses for 2 years of work. Just wondering why they did it anyway?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-10-25
@iliapro

First you need to understand what cookies are. How cookies are passed to the client. What are the headers and body of HTTP requests and responses. And then everything will fall into place. Well, so that there would be no problems - read about managing output buffering in PHP.
How are cookies different from a session in PHP?

A
Alexey Skobkin, 2015-10-26
@skobkin

In addition to Sergey Protko 's answer , I will say that you can create them at any time. But to install (send) - only before sending the response body.
By the way, things like the Symfony HTTPFoundation and PSR-7 are partly made to avoid shooting yourself in the foot like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question