Cookie and Session store state in HTTP

        Usually, server does not remember what client make requests. It treats every requests from a new anonymous client. If server want to provide personalized service(login state, exercise records of sports, saved favourite blog post) to the client, it must know the client and send back the relevant data. Cookie and Session is used to help recognize the client.  

        Session is a storage space and cookie is s small piece of data. Both are created by the server. When server want to remember the client, it saves client information into Session with a unique ID. Then server creates a cookie that contains this Session ID. Server sends back the data requested by the client associated with the cookie. The client save the cookie.

        Next time, the client send new request to the same server with the cookie saved. The server check the cookie and extract the Session ID for search. If the server find the record in Session, then server know the client and can provide personalized service.

        The diagram below is a simply workflow.

Diagram to explain how server remember client by using cookie and session


        The cookie is saved on client side and also transfer between the client and server in the internet. There needs some action for security to prevent from other stealing your cookie and behave as you on the server side. 

For more detail about cookie, you can find in Mozilla MDN - Using HTTP cookies.

Comments

Popular posts from this blog

Use okhttp to download file and show progress bar

Download File into app specific storage with Retrofit

Unzipp file with Zip4j library