Cookies and session are quite similar. is there any advantage or any special situation where one can be used but not the other. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Cookies and session are quite similar. is there any advantage or any special situation where one can be used but not the other.

2nd Jul 2016, 3:38 PM
Midhun Mathew
2 Answers
+ 1
actually, session uses cookies to work. whenever you start a session, the server generates a long random number (in hex) and stores it both in the server and in your computer (as a cookie), and every information you add in the session is stored within the cookie. that way, your browser can remind the server which session id identifies you, and share information within the server. you can easily find out what your session id is with this code: <?php session_start(); print(session_id()); ?> if you want the server to remember something only as long as he is browsing the web, then session is the way to go. Else, if you want data to live longer, then a cookie is the way to go. a good example is the "remember me" on a website. if it is unchecked, then it may be useful to store the login information in the session, so that the user doesn't have to login every time he opens the page. Once the session is destroyed, or time passes by, he will be "forgotten" and requires to login again.
6th Jul 2016, 2:20 PM
Alejandro Aristizabal
Alejandro Aristizabal - avatar
+ 1
cookies will hold their info even after the session ends.
12th Nov 2016, 7:43 AM
Texx Smith
Texx Smith - avatar