The Wayback Machine - https://web.archive.org/web/20241106183915/https://www.geeksforgeeks.org/understanding-cookies-in-web-browsers/
Open In App

Understanding Cookies in Web Browsers

Last Updated : 18 Oct, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Cookie is a small message from a web server passed to the user’s browser when you visit a website. In other words, Cookies are small text files of information created/updated when visiting a website and stored on the user’s web browser. Cookies are commonly used for information about user sections, user preferences and other data on the website. Cookies help websites remember users and track their activities to provide a personalised experience.

Where are cookies stored?

Cookies are small pieces of data that are stored on a user’s device when they visit a website. These data files are typically stored in the user’s web browser. Depending on the browser and platform, cookies can be found in different locations. Commonly, cookies are stored in a specific folder or directory on the user’s computer or device. I

  • In Windows, they are often found in the C:\Users[username]\AppData\Roaming[browser name]\Cookies directory,
  • In macOS, they can be found in the ~/Library/Cookies directory. However, the exact location can vary based on the browser and operating system.

Types of Cookies:

There are several types of cookies which serve a unique functionality of use. We will discuss the 4 main types of cookies.

Session cookies:

Session cookies are also known as temporary cookies which are present as long as the user browser is open. Session cookies are deleted once the browser is closed and the user’s session is inactive (time-based). Session cookies are most often used to maintain the user’s session ( like authentication ) on the browser.

Persistent cookies:

Persistent cookies are also known as permanent/long-term cookies. Persistent cookies can last longer than session cookies. Persistent cookies are stored on the user’s device for a specific period ( for less than 6 months ). Persistent cookies are most often used for long-term tracking and remembering user preferences.

First-party cookies:

First-party cookies are set by the website that you are currently visiting. First-party cookies are generally used to provide a good user experience, collect the analytics data, remember language settings etc.

Third-party cookies:

Third-party cookies are set by the domains that you are not visiting. Third-party cookies are mostly used for cross-site tracking and advertising purposes these cookies collect data about your browsing habits and serve ads according to it. For example, when you visit a website, it may include content (such as ads or social media widgets) from third-party domains. These third-party domains can set cookies on your browser to track your online activity and build profiles for targeted advertising or analytics.

What are cookies used for?

Cookies serve several purposes, including:

  • User Sessions: Cookies are often used to maintain user sessions, allowing websites to remember user data, preferences, and login status across multiple pages or visits.
  • Tracking: They are used for tracking user behaviour on websites, collecting data on which pages are visited and how users interact with the site.
  • Personalization: Cookies enable websites to offer personalized experiences by remembering user preferences, such as language settings, shopping carts, and customized content recommendations.

Features:

  • Persistence: Cookies can be persistent or session-based, which will be dependent on the time duration of data to last.
  • Key-Value Storage: Cookies stores data in a key-value pair and the key-value pair makes it easy to retrieve and update data.
  • Security: Cookies can have security features like HttpOnly and Secure attributes to protect against unauthorized access.

Benefits:

  • User Convenience: Cookies mainly enhance user experience by storing user preferences like language settings, shopping cart items, etc.
  • Tracking: Cookies are used as trackers to collect user’s behaviour which will be helpful for marketing strategies, improving content, etc.
  • Authentication: Cookies are essential for maintaining the user’s session and authentication to stay logged in.

When to Use Cookies?

Cookies are used when you need

  • To store user preferences and settings.
  • To implement authentication and session management.
  • To track user behaviour for analytics.
  • To customise user experiences.
  • To implement shopping carts and e-commerce functionality.

How do cookies affect privacy?

Cookies can have both positive and negative effects on privacy:

  • Positive: Cookies can enhance user experiences by remembering preferences and login information, making websites more user-friendly.
  • Negative: Cookies can raise privacy concerns when they are used for tracking and profiling without user consent. Excessive tracking can result in a loss of online privacy and the potential for data misuse.

Example:To implement a cookie in JavaScript, follow these steps to set, read, and delete a user’s language cookie:

Javascript




<script>
    function setCookie(name, value, daysToExpire) {
        const date = new Date();
        date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
        const expires = "expires=" + date.toUTCString();
        document.cookie = name + "=" + value + "; " + expires;
        console.log(name+" cookie created");
    }
  
    // read cookie
    function getCookie(name) {
        console.log(document.cookie);
        const decodedCookie = decodeURIComponent(document.cookie);
        const cookies = decodedCookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            let cookie = cookies[i].trim();
            if (cookie.indexOf(name + "=") === 0) {
                return cookie.substring(name.length + 1);
            }
        }
        return null;
    }
  
    // delete cookie
    function deleteCookie(name) {
        document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC;";
        console.log(name+" cookie deleted");
    }
  
    // Set a cookie
    setCookie("userLanguage", "en-US", 30);
  
    // Read a cookie
    const language = getCookie("userLanguage");
    console.log("User's language: " + language);
  
    // Delete a cookie
    deleteCookie("userLanguage");
</script>


Output:

Frame-2-(1)

user’s language cookie example output



Previous Article
Next Article

Similar Reads

History of Web Browsers
Web browsers play an important role in connecting users to the World Wide Web. Whenever we have to search for anything, we open Chrome. Well, Chrome is the most widely used web browser in the world. However, there have been many browsers developed before and after Chrome. Some of them still exist and are being used, whereas others got replaced and
6 min read
What are the best Browsers for Web Development?
Web development is the process of creating and maintaining websites. It involves a variety of skills, including HTML, CSS, and JavaScript. Web developers need to be able to test their websites across different browsers to make sure they are working properly. Table of Content Benefits of Web Browsers for Web DevelopmentWhat are the best Browsers for
5 min read
Boosting Web Development workflow using browsers
Web development workflow refers to the set of processes a developer uses to first develop a website, then deploy it to the web and maintain it. Web development workflow is a structured process that most experienced web developers follow to develop and maintain websites and web applications. Table of Content Web development workflowRole of browsers
6 min read
How does cross platform syncing features works in web browsers?
Cross-platform syncing features in web browsers allow you to seamlessly access your browsing data, such as bookmarks, history, passwords, and extensions, across different devices and operating systems. This means that you can pick up where you left off on any device, regardless of whether you're using a computer, phone, or tablet. Benefits of Cross
3 min read
Common Features of all Web Browsers
A web browser is a software application designed for accessing and navigating websites over the internet. When a user requests a web page from a particular website, the browser retrieves its HTML, CSS, and JS files from a web server and then displays the page on the user's screen. Some Common Features of Web Browsers:1. User Interface: The web brow
5 min read
The Future of Web Browsers
The internet is a vast and ever-changing landscape, and web browsers are our essential tools for navigating it. But what exactly is a web browser, and how does it work? From their humble beginnings in the 1990s to the sophisticated tools of today, web browsers have undergone a remarkable transformation. Let's explore the fascinating world of web br
6 min read
Rendering engines used by different Web Browsers ?
Websites wouldn't exist without browsers, plain and simple. From displaying text and images to making buttons react, everything hinges on how a browser interprets and presents the code behind a website. But here's the secret: different browsers use different "rendering engines" under the hood, and understanding these engines is crucial for ensuring
5 min read
How to Enable or Disable Notifications from Web Browsers?
Web browsers are like magic that opens doors to the internet world from where you can see and travel the whole world by sitting inside the room. But sometimes, they can be too frustrating, when you are working on something and browsers keep on popping the notifications on the screen. Luckily, you can control notifications on your browser, let's lea
3 min read
Accessibility Features for Visually Impaired Users in Web Browsers
Our digital world is filled with we­bsites providing facts, services, and chance­s. Yet those with disabilities can struggle­ accessing online content. Web accessibility refers to the practice of designing and developing websites and web applications to ensure that they are usable by everyone, including those with disabilities. By using acce­ssibil
4 min read
Browsers Role in Web Development
Browsers such as Chrome, Firefox, Edge, and many more are software used to visit various sites on the internet. They act as an interface between the user and the internet content. Modern browsers also provide many additional features, such as privacy, extensions, developer tools, bookmarks, history, etc. Due to the increase in the number of web app
6 min read
Which Browsers offer Cross Platform Syncing?
It is now necessary to be able to move between devices for online browsing without any interruptions in the linked world of today. This is where cross-platform synchronization comes into play, allowing users to synchronize their browser data across devices, independent of the platform or operating system, including bookmarks, history, and active ta
3 min read
10 Best Android Browsers in 2024
The Best Android Browsers not only help in easy surfing on the internet but also provide the best user experience by customizing it in several manners. Most users think that Google Chrome is the best browser Android that can give speed, security & syncing data over different devices. The presence of a large collection of Android Web Browsers in
7 min read
Features in Safari available only for mobile browsers
Safari is developed by Apple. It is available in AppStore which is built into iPhones, Mac book, and iTabs. For iPhone users, Safari is the best option for browsing. Safari becoming a dominant form for all category professionals due to its functionalities and essential features. Most people use mobile browsers instead of web browsers right!, the re
5 min read
Why browsers consume so much RAM
Browsers consume significant RAM due to the complex nature of modern websites and the demand for enhanced features. As websites become more complex with multimedia content, dynamic scripts, and interactive elements, browsers need to allocate more memory to render and manage these elements efficiently. Even, each open tab and extension further contr
1 min read
What are the best internet browsers for 2024?
Let's be honest – picking a web browser used to be a no-brainer. But in 2024? It's a tech jungle out there, with browsers promising everything from supercharged speed to fortress-like privacy. Don't stress; think of me as your friendly internet tour guide, ready to cut through the hype and find the perfect tool to upgrade your online life. The Usua
3 min read
How to change the default search engine in Browsers?
Changing the default search engine in your web browser can enhance your browsing experience by providing results from your preferred search engine. This article will guide you through the process of changing the default search engine in your web browser. The default search engine is the one that your browser uses when you type a search query into t
4 min read
Browsers with Built-in Ad Blockers
Are you ever irritated by the websites that pop up when you are surfing? All those animated logos, ads that conveniently chase you, and videos with automatic playback can ruin your experience. The good news is that there are browsers now that have inbuilt ad-blockers to keep all these irritating ads from popping up. These new browsers with in-built
5 min read
How to Block Websites in Browsers?
Browsers are the famous tool that helps people to surf the internet. When we want to visit any website we simply go to our browser and search for that website then the browser takes us to the IP Address of that website and we become able to access that website. But if we can access all websites at every time then it can affect our productivity. For
4 min read
7 Best Browsers for Online Gaming
Have you ever encountered frustrating stuttering, performance, or distracting pop-ups during your online gaming sessions? Your browser might be the culprit. In this guide, we'll uncover the browsers built for smooth gameplay, revealing the features and configurations that set them apart in the race for the ultimate online gaming experience. Let's s
4 min read
7 Best Browsers With Built-In VPN
Secure, Protective, and Private browsing is an utmost priority of every user when surfing the Internet. Secure browsing becomes more important in accessing content that is geo-restricted. The best examples are movies, shows, and dramas from different countries that might not be available in your country. To access such content one requires a privat
6 min read
How to enable or disable hardware acceleration in various browsers
Hardware acceleration in a browser is a process that utilizes the GPU to handle graphics-intensive tasks, such as rendering web pages, playing videos, and running web applications. This can significantly improve the performance of the web application in the browsers as the CPU load is reduced. Table of Content Benefits of Hardware Acceleration in B
4 min read
View, Edit and Delete Cookies in Microsoft Edge Browser
Application Tab is a tool provided by Edge to view, edit, and delete cookies. Cookies are small files storing user-related information that is being stored by the web server using the client browser to make the website dynamic, i.e., the same site but different UI or functionality for different users. For example, if we visit a site and by default,
7 min read
Understanding Mozilla Firefox Developer Tools
Mozilla Firefox is a privacy-friendly web browser maintained and supported by Mozilla organization. It is completely an open-source project i.e. anyone can view the source code of the browser and contribute to it. It provides various features like an in-browser screenshot tool, reader mode, and more privacy to users and it has been a favorite pick
6 min read
Functions and Features of a Web Browser
What is a Web Browser?In simple words, a web browser is an application through which we can access websites on the World Wide Web (www). It provides users a medium using which they can interact with a web server as it fetches the data and displays it as a web page. We always use Chrome for browsing websites in our daily lives. Well, Chrome is one o
5 min read
What is a web server - Working and Architecture
A web server is a software application or hardware device that stores, processes, and serves web content to users over the internet. It plays a critical role in the client-server model of the World Wide Web, where clients (typically web browsers) request web pages and resources, and servers respond to these requests by delivering the requested cont
6 min read
Sending Web API requests using Network Console Tool in Microsoft Edge Browser
Application programming interface testing, sometimes known as API testing, is a subset of software testing that focuses on evaluating the interactions between various APIs and specific API functions. We can test the API in many ways such as accessing the endpoints through the Postman, browser, etc. One of the most common methods we use is sending w
3 min read
Web Page - A Complete Overview
A web page is one document or resource from a collection that makes up a website that can be viewed through any web browser. This refers to text, pictures, multimedia, and links arranged by page and shown on a screen. It employs a mixture of programming languages such as HTML, CSS, and Javascript that are useful in structuring, fashioning, and oper
7 min read
How to use Safari for web development
Safari is quite popular among web developers, especially professionals who use Mac as a productivity tool. Globally, about 25% of users use Safari as their primary web browser. When it comes to desktop-based browsers, it drops to about 9%, which is not bad when we realize that Safari is the second most used web browser after Chrome in all categorie
6 min read
How do web browser handle tab and multiple windows?
Web browsers are sophisticated software applications designed to enable users to access and interact with information on the internet. One of the key features that enhance user experience is the ability to handle multiple tabs and windows. This functionality allows users to navigate between different web pages and tasks seamlessly. Tabs: Tabs are a
4 min read
Web Browser
When we need any kind of information most of the time we get help from the Internet, and we get information. The Internet provides us with useful information easily. We use mobile phones, computers, and tablets. We search for a lot of things in our daily lives, so we get information about all over the world, but we can not get information by just o
4 min read
three90RightbarBannerImg