Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

29. Cookies in PHP

Lesson 26 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Cookies in PHP

What is a Cookie?

A cookie is a small piece of data stored on the user’s browser by a website. It is used to remember user information between multiple requests.

Exam Definition: A cookie is a client-side storage mechanism used to store small amounts of data in the user’s browser.

---

Why Cookies are Needed

Cookies are required because HTTP is stateless, meaning it does not remember users between requests.

Uses of Cookies

  • Remember login details
  • Store user preferences (theme, language)
  • Track user activity
  • Enable “Remember Me” feature
  • Maintain shopping cart data
Exam Line: Cookies help maintain state in web applications.

---

Types of Cookies

1. Session Cookies

  • Temporary
  • Deleted when browser closes
  • No expiration time
Use Case: Login sessions.

2. Persistent Cookies

  • Stored for a fixed time
  • Remain even after browser closes
Use Case: Remember Me functionality.

3. Secure Cookies

  • Transmitted only over HTTPS
  • More secure

4. HttpOnly Cookies

  • Not accessible via JavaScript
  • Protects against XSS attacks

Cookie Types Summary Table

TypeLifetimeUse
Session CookieUntil browser closesLogin
Persistent CookieFixed timeRemember Me
Secure CookieHTTPS onlySensitive data
HttpOnly CookieServer access onlySecurity

---

How to Create Cookies

setcookie() Function

setcookie(name, value, expire, path);

Example: Create Cookie

setcookie("user", "Rohit", time() + 3600, "/");

✔ Stored for 1 hour

Exam Tip: Cookies must be set before any HTML output.

---

Accessing Cookies

echo $_COOKIE["user"];

---

Deleting a Cookie

setcookie("user", "", time() - 3600, "/");
Exam Line: Cookies are deleted by setting expiry time in the past.

---

Advantages of Cookies

  • Simple to use
  • Client-side storage
  • Improves user experience

Limitations of Cookies

  • Limited size (≈4KB)
  • Less secure
  • Can be disabled by users