← Back to PHP Course | Chapter 12: Sessions & Cookies | Lesson 3 of 5

PHP Cookies Introduction

एक cookie एक छोटा note है जो website आपके browser से रखने और अगली बार फिर दिखाने के लिए कहती है। यह site को आपकी favorite language जैसी चीज़ें याद रखने में मदद करता है।
Syntax
php
setcookie("name", "value", expiration_timestamp, "/path", "domain");

$value = $_COOKIE["name"];   // available on the next request

एक Cookie क्या है?

एक cookie data का एक छोटा piece है जिसे server browser से store करने और उस site की हर बाद की request पर वापस भेजने के लिए कहता है, आमतौर पर preferences याद रखने या login की ज़रूरत के बिना एक returning visitor पहचानने के लिए इस्तेमाल होता है।

उदाहरण: What is a Cookie?

php
<?php
// Call `setcookie("theme", "dark")`
setcookie("theme", "dark");
// Print "Cookie 'theme' will be sent back on the next request" to the output
echo "Cookie 'theme' will be sent back on the next request";
?>

Cookie Expiration Dates Set करना

Cookies browser बंद होने पर automatically delete हो जाती हैं जब तक आप setcookie() से एक explicit expiration timestamp set न करें, जो है कि एक 'remember me' checkbox actually sessions में persist क्यों करता है।

उदाहरण: Setting Cookie Expiration Dates

php
<?php
// Call `setcookie("remember_me", "yes", time() + (86400 * 30))`
setcookie("remember_me", "yes", time() + (86400 * 30));
// Print "Cookie set to persist for 30 days" to the output
echo "Cookie set to persist for 30 days";
?>

Cookie Paths और Domains Configure करना

किसी cookie के path और domain parameters exactly control करते हैं कि आपके server पर कौन से URLs इसे वापस पढ़ सकते हैं, आपको ज़रूरत के हिसाब से एक cookie को एक subfolder तक scope करने या subdomains में share करने देते हुए।

उदाहरण: Configuring Cookie Paths and Domains

php
<?php
// Call `setcookie("cart", "abc123", 0, "/shop/")`
setcookie("cart", "abc123", 0, "/shop/");
// Print "Cookie scoped to the /shop/ path only" to the output
echo "Cookie scoped to the /shop/ path only";
?>

Secure और HttpOnly Cookies

Secure और HttpOnly flags किसी cookie को common attacks के against harden करते हैं: Secure ensure करता है कि यह सिर्फ HTTPS पर भेजा जाए, और HttpOnly JavaScript को इसे पढ़ने से block करता है, जो एक common XSS attack vector बंद कर देता है।

उदाहरण: Secure and HttpOnly Cookies

php
<?php
// Call `setcookie("session_token", "xyz", 0, "/", "", true, true)`
setcookie("session_token", "xyz", 0, "/", "", true, true);
// Print "Secure: HTTPS only. HttpOnly: hidden from JavaScript." to the output
echo "Secure: HTTPS only. HttpOnly: hidden from JavaScript.";
?>

Cookies Delete करना

क्योंकि कोई direct delete operation नहीं है, एक cookie हटाने का मतलब है setcookie() को same name के साथ फिर call करना लेकिन past में एक expiration time के साथ, browser को इसे तुरंत discard करने के लिए बताते हुए।

उदाहरण: Deleting Cookies

php
<?php
// Call `setcookie("theme", "", time() - 3600)`
setcookie("theme", "", time() - 3600);
// Print "Cookie set to expire in the past -- browser will discard it" to the output
echo "Cookie set to expire in the past -- browser will discard it";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. output शुरू होने के बाद setcookie() call करना, जो एक headers already sent warning के साथ fail हो जाता है।
  2. उम्मीद करना कि नई cookie इसे set करने वाली same request के दौरान $_COOKIE में दिखेगी, जबकि यह सिर्फ अगली request पर दिखती है।
  3. किसी cookie में passwords जैसा sensitive data store करना, जिसे user पढ़ और edit कर सकता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.