PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

PHP · Theory

Cookies

← All stacks

Theory

43/352

Cookies

A cookie is a small string stored in the browser. setcookie('theme', 'dark', time()+86400, '/'); then $_COOKIE['theme']. Use for theme or ‘remember me’ token — not the raw password.

HttpOnly cookies are harder for JS to steal. Expiry in the past deletes a cookie.

Trap — setcookie after HTML started. Trap: huge data in cookies — use sessions.

Exam tip

setcookie then $_COOKIE. Sessions for login state.

Example

<?php
// Cookies
$scores = [80, 90, 70];
$avg = array_sum($scores) / count($scores);
echo "avg=" . number_format($avg, 1) . "\n";
foreach ($scores as $s) {
  if ($s >= 80) echo "high: $s\n";
}

Cookies: scores array, print avg, then high marks. Say the output.

Short notes

  • DefCookie = small data in the browser.
  • Rulesetcookie before HTML. Not for passwords.
  • TrapOutput before setcookie.

Questions

1

Cookie vs session?

43 / 352

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.