Why is session_start necessary when using a session?

Why can't I use setcookiewithout any preparation until you need it session_start()before use $_SESSION? And I think the work they do is similar.

+5
source share
4 answers

Because setcookie () defines a cookie that will be sent along with the rest of the HTTP headers. This is a completely different thing than session_start () , for example. creating a session or resuming the current one based on the session identifier transmitted through a GET or POST request, or transmission through a cookie.

- , $_COOKIE $_GET $_POST, session_save_path, $_SESSION, , , , setcookie .

. " PHP" .


< > . @Felix , . , : session_start () , setcookie , . >

+11

. . start_session(), php.ini: session.auto_start

+1

, . session_set_save_handler() , , . .

session_start(). , .

. session_start(), , cookie.

+1

, . session_set_save_handler() , , . .

All this data is processed using session_start(). Thus, you can easily change your goal without disturbing your application.

Note. This is only one reason for session_start(), and again, it is much more than just setting cookies.

0
source

All Articles