Difference Between Session and Cookies in PHP

What is a Session?

A session is all about the total time taken to complete an activity. In a computer any session begins or “user session” begins when the user logs in or gets an access to any computer, network server or any software service. The session ends when the user shuts down the computer or logs out any service. A session temporarily stores data or information that is related to the activity of the user while the system is connected. Session Cookie in the web page is for storing data or information when the user exits the web page or shuts down the internet browser.

For instance, any e-commerce website can remember your last searched products or products from your shopping cart when you come back.

What is a Cookie ?

Cookies are termed as small text files , given ID tags that are used to keep a record on your computer’s browser directory or program data sub folders. Cookies are generally generated when a user uses their browser to visit a website that uses cookies to keep track records of your activities and movements within the website, which helps the user to resume where he/she has left off, to remember the user’s registered login details, themes, preferences, and other customized functions. The website also stores corresponding files with the same ID Tag one for the browser and another file to track and keep information on the movement within the website.

Session Versus Cookies in PHP

Session

  • Sessions data is stored on the server.
  • Session can store unlimited amount of data and information
  • Session information is more secure, it can’t be accessed easily.
  • By using session_destroy(), one can destroy the sessions.
  • Session ends when the user shuts the browser or the computer.
  • Session is always depends on cookies.
  • In PHP a session starts with “session_start()”.
  • A session can be unset or destroyed by the command “ session_destroy()”.
  • In session a user can store as much as data he/she wants.
  • A session only limits the maximum data a script can conuse at a time by default is 128MB.

Cookies

  • Cookies store data in the visitor’s browser.
  • Cookies are bound to store a limited amount of data
  • A Cookie is only allowed to store 4kb (4096bytes).
  • Cookies are considered to be less secured and can be accessed easily.
  • A Cookie’s end depends on the time period the user sets for it.
  • Cookies are not dependent on session.
  • Cookie doesn’t need to be started, it is automatically stored in the local machine.
  • A cookie can’t be undone or unset.
  • A Cookie has limited storage as compared to session.
  • A Cookie starts with setcookie() function in PHP.

Add a Comment

Your email address will not be published. Required fields are marked *