PHP Cookies and Session LEARNOVITA

PHP Cookies and Session Outline | Everything You Need to Know

Last updated on 04th Nov 2022, Artciles, Blog

About author

Kanagavel (PHP Developer )

Kanagavel is a PHP Developer Expert with 7+ years of experience in PHP, JavaScript, CSS-Cascading Style Sheets, MySQL, Core PHP, and Bootstrap. He spends most of his time researching technology and startups.

(5.0) | 19482 Ratings 2125
    • In this article you will get
    • Session
    • Cookie
    • Why and when to use Sessions?
    • Why and when to use Cookies?
    • Conclusion


A session is used to save an information on a server momentarily so that it may be utilized across differenf pages of a website. It is overall amount of time spent on activity. The user session begins when a user logs in to the specific network application and ends when user logs out of the program or shuts down a machine.

Session values are far more secure since they are saved in a binary or encrypted form and can only be a decoded at server. When the user shuts down machine or logs out of a program, the session values are an automatically deleted. Must save a values in database to keep them forever.

Sessions in Server


A cookie is the small text file that is saved on a user’s computer. The maximum file size for the cookie is 4KB. It is also known as HTTP cookie, a web cookie, or internet cookie. When user first visits website, the site sends a data packets to user’s computer in a form of a cookie.

The information stored in a cookies is not safe since it is kept on a client-side in a text format that anybody can see. Can activate or disable cookies based on needs.

    Cookies are the client-side files on local computer that hold user information. Sessions are a server-side files that contain a user data.
    Cookies end on a lifetime set by user. When the user quits a browser or logs out of programmed, the session is over.
    It can only save a certain amount of info. It can hold the indefinite quantity of a data.
    The browser’s cookies have maximum capacity of a 4 KB. Can keep as much data as like within a session, however there is maximum memory restriction of a 128 MB that a script may consume at a one time.
    Because cookies are kept on a local computer, and don’t need to run function to start them. To begin session, must use a session start() method.
    Cookies are not be secured. A Session are more secured compare than a cookies.
    Cookies stored data in a text file. A Session save data in an encrypted form.
    Cookies stored on limited data. A Session stored a unlimited data.
    In PHP, to get a data from Cookies , $_COOKIES the global variable is be used. In PHP , to get a data from Session, $_SESSION a global variable is used.
    Can set an expiration date to delete a cookie’s data. It will automatically delete a data at a specific time. In PHP, to destroy or remove data stored within a session, can use a session_destroy() function, and to unset specific variable, can use a unset() function.
Cookies in Server

Why and when to use a Sessions?

To save important information such as user id more securely on a server where malicious users are cannot temper with them. Sessions are used to pass the values from one page to another.

It is also used when want the alternative to the cookies on browsers that do not support cookies, to store a global variables in an efficient and more secure way compared to passing them in an URL, developing an application such as shopping cart that has to temporary store information with the capacity larger than 4KB.

Why and when to use a Cookies?

Http is the stateless protocol; cookies allow us to track a state of the application using small files stored on user’s computer. The path were the cookies are stored depends on a browser. Internet Explorer usually stores them in a Temporal Internet Files folder. Personalizing a user experience – this is achieved by allowing the users to select their preferences. The page requested that follow are personalized based on a set preferences in cookies. Tracking a pages visited by user.


Cookies are be considered to be less safe than a sessions since a third-party can manipulate a data that is stored in them, whereas a sessions are stored in the encrypted form that user alone can read.Sessions also save a passwords on the computer and form data, which means that each time the user accesses application, that program will not ask user to log in again. This is because a passwords and form a data are saved.

Are you looking training with Right Jobs?

Contact Us

Popular Courses