Where will I come in handy $_COOKIE, $_SESSION on practice? | Sololearn: Learn to code for FREE!


Where will I come in handy $_COOKIE, $_SESSION on practice?


6/13/2020 6:52:15 PM


3 Answers

New Answer


[1/2] I am assuming you are trying to ask what is the use of the Sessions and Cookes, and how you can use the related superglobals in the PHP and here is my quick explanation A cookie is a piece of data that is set by the server in the client (In most cases it will be a browser), and later on, all the cookie get send to the server whenever you create a request to any server, from the same site. Cookies can be changed or removed by the user manually but not via JavaScript, and if you look by the perspective of the authentication the cookies are no safe place to put sensitive data. On the Other hand, Session is a kind of cookie and that also gets stored in the server and in the client-side most of the time, it will get stored in the encrypted form using some sort of crypto algorithms such as md5, sha2, and others. Now Let's not go too much in the theory and let's take a live example, I would highly recommend using some sort of computer rather than code playground for better understanding of the concept To set a Cookie in the client-side, you will use set-cookie header like this 👇, and in the response of the request you will make to the browser, a cookie will get set in the browser or client https://code.sololearn.com/w1m8Dm1f3d6F/ and whenever the client will make a request to your server, the cookie will be in the superglobal named $_COOKIE, to retrieve that cookie you can simply use the code below, and notice here cookies can be removed from the client-side storage https://code.sololearn.com/wGlxle3FW8R4/#php So now let's talk about the sessions, and there use cases, In PHP most of the time sessions are used to authenticate the user, a basic session can be started by session_start() function, and you will have to use the sesssion_start() function in every page, wherever you want to access this particular session, sessions can also be named


[2/2] Now to end the session we use session_distroy() to end the session, and on the result of the session ending the user will get logged out, the default behavior of the session is whenever you will close your browser, it will get destroyed unless you add a session remember function in your code. Sessions are stored in both client-side (In encrypted form) and server-side, whenever you make a request to the server, the server will check the encrypted form of your request cookie and will try to compare it with the stored data about the session on the server-side, if matched you will be authenticated otherwise not. in PHP $_SESSION superglobal is used to store session data, and you can set the data once the session is started in the server-side using $_SESSION['your_data'] = 'your_value' and it will be available to every page in the same session This can be an infinite long essay, but I hope this will help you understand the concept of cookies and session just a little better.