Cookies Basics

Introduction

Cookies are small text files stored on a user’s device by a website. They help websites remember user information, preferences, login sessions, and browsing activities to improve the user experience.

Cookies are widely used in web development to personalize content, manage sessions, and track website performance.

Objectives

By the end of this training, you will be able to:

  • Understand what cookies are
  • Learn how cookies work
  • Identify different types of cookies
  • Create and manage cookies in web applications
  • Understand the importance of cookies in user sessions
  • Learn cookie security and privacy practices

What Are Cookies

Cookies are data files created by a website and saved in the user’s browser. Each time the user visits the website again, the browser sends the cookie data back to the server.

Cookies help websites:

  • Remember login details
  • Save language preferences
  • Store shopping cart items
  • Track website usage
  • Improve user experience

How Cookies Work

The process of cookies works as follows:

  1. A user visits a website
  2. The website sends a cookie to the browser
  3. The browser stores the cookie on the device
  4. When the user revisits the website, the browser sends the cookie back to the server

This allows the website to recognize returning users and remember information.

Types of Cookies

Session Cookies

Session cookies are temporary cookies that are deleted when the browser is closed.

Uses:

  • User login sessions
  • Temporary shopping cart data
  • Secure website sessions

Persistent Cookies

Persistent cookies remain stored on the device until they expire or are manually deleted.

Uses:

  • Remembering usernames
  • Saving website settings
  • Personalizing content

First Party Cookies

These cookies are created directly by the website the user visits.

Uses:

  • Website functionality
  • User preferences
  • Analytics

Third Party Cookies

These cookies are created by external services or advertisers connected to the website.

Uses:

  • Advertising
  • Tracking user behavior
  • Marketing analytics

Advantages of Cookies

  • Faster website experience
  • Personalized content
  • Automatic login support
  • Better user session management
  • Improved website analytics

Disadvantages of Cookies

  • Privacy concerns
  • Tracking issues
  • Security risks if not managed properly
  • Limited storage size

Creating Cookies in PHP

Example:

<?php
setcookie("username", "Ali", time() + 3600);

echo "Cookie Created";
?>

Explanation:

  • username is the cookie name
  • Ali is the cookie value
  • time() + 3600 sets the cookie expiry time to one hour

Reading Cookies in PHP

<?php
echo $_COOKIE["username"];
?>

Deleting Cookies in PHP

<?php
setcookie("username", "", time() - 3600);

echo "Cookie Deleted";
?>

Cookies in JavaScript

Creating a Cookie

document.cookie = "username=Ali";

Reading Cookies

console.log(document.cookie);

Security Best Practices

To use cookies safely:

  • Avoid storing sensitive information in cookies
  • Use secure cookies on HTTPS websites
  • Set expiration dates carefully
  • Use HttpOnly cookies for security
  • Inform users about cookie usage

Cookie Privacy and Consent

Many countries require websites to inform users about cookie usage. Websites often display cookie consent banners to comply with privacy laws.

Common privacy regulations include:

  • GDPR
  • CCPA

Real World Uses of Cookies

Cookies are commonly used in:

  • E-commerce websites
  • Social media platforms
  • Learning management systems
  • Banking applications
  • Online forums

Career Applications

Understanding cookies is important for:

  • Web Developers
  • Backend Developers
  • Frontend Developers
  • Cybersecurity Professionals
  • Digital Marketing Specialists

Final Presentation

In your final presentation, explain:

  • What cookies are
  • How cookies work
  • Types of cookies
  • Benefits and risks of cookies
  • Creating cookies using PHP and JavaScript
  • Cookie privacy and security practices
Home » Advanced PHP > Sessions and Cookies > Cookies Basics