Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between session and cookie?give one example of each ?

user-image
Question added by Mohammad Irfan , Certified Magento Developer , Xcite.com
Date Posted: 2013/10/09
Samuel Eid Safwat Naguib
by Samuel Eid Safwat Naguib , Technology lead ,Project Manager ,Transformation Coach , Octalpha

Cookie: A key/value pair that is stored by the user's browser and is available in the superglobal $_COOKIE array available in PHP. The cookie request is initiated with an explicitly defined expiration date. For example:

setcookie('cookieName', $some_value, time()+3600, "/", ".example.com")

On the next server request, $_COOKIE['cookieName'] will be available. If you use a browser tool to look at the cookie, it will have an expiration date.

Session Cookie: Identical to the above but defined without an expiration date. If you use the same browser tool it will say that the cookie expires at the end of the session; which is ultimately when you close your browser. For example:

setcookie('cookieName', $some_value);

PHP Session: a server side mechanism that will associate a bunch of data with a session id. Every time a session is invoked, it serializes/unserializes it. This could be more data than just a single key/value pair that a cookie supports, but the way of associating this data with a user is by creating a cookie (regular or session as described above) in their browser that contains the session id. This way, the right data can be retrieved for a given user based on the value of that cookie.

Mostafa Hashem
by Mostafa Hashem , Functional consultant / Solution Architect , international turnkey system (its)

1- Session stored Server side, however cookie is stored client side

2- Session is created and maintained by the web server or application server, however is created and maintained by the developer

More Questions Like This