Why I can use setcookie without any preparation while need a session_start() before using $_SESSION?And I think works they do are similar.
Why I can use setcookie without any preparation while need a session_start() before using
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because setcookie() defines a cookie to be sent along with the rest of the HTTP headers. That’s a completely different thing than what session_start() does, e.g. creating a session or resuming the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
The first just adds something to the header and sends it to the browser, while the other gets the Session ID from $_COOKIEs or $_GET or $_POST and then tries finding the session file in the session_save_path and when found unserializing the values of it into $_SESSION and if not, create a new session, probably using
setcookiein the process to set the Session Id.See the chapter on Sessions in the PHP Manual.
**Edit** Like @Felix correctly points out below, the session is not necessarily saved in a file. It’s not that important though, because the argument stays the same: `session_start` will find and (re-) initialize your session data, while `setcookie` just does what the name implies.