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

PHP Cookie Management

Cookie management यह decide करना है कि note क्या कहता है, यह कितनी देर रहता है, और कौन इसे पढ़ सकता है। PHP ज़रूरत न रहने पर cookies को लिख, पढ़, और फाड़ सकता है।
Syntax
php
setcookie("name", "value", [
    "expires"  => time() + seconds,
    "path"     => "/",
    "secure"   => true,
    "httponly" => true,
]);

setcookie("name", "", time() - 3600);   // delete

Cookies का Introduction

setcookie() वह function है जो actually visitor की machine पर एक cookie बनाता है; यह cookie का name और value plus इसकी lifetime, scope, और security control करने वाली optional settings लेता है।

उदाहरण: Introduction to Cookies

php
<?php
// Call `setcookie("username", "Alice")`
setcookie("username", "Alice");
// Print "Cookie created with a name and value" to the output
echo "Cookie created with a name and value";
?>

Cookie Expiration Set करना

अगर आप setcookie() को कोई expiration time pass नहीं करते, cookie को एक session cookie माना जाता है और browser बंद होते ही गायब हो जाती है -- explicitly एक future timestamp set करना वह है जो visits में data persist करवाता है।

उदाहरण: Setting Cookie Expiration

php
<?php
setcookie("username", "Alice"); // session cookie -- disappears when browser closes
setcookie("remembered_user", "Alice", time() + 86400); // persists for 1 day
echo "Two cookies set with different lifetimes";
?>

Cookie Security Options

किसी cookie को HttpOnly mark करना client-side scripts को इसे पढ़ने से रोकता है (एक common XSS attack path block करते हुए), जबकि इसे Secure mark करना ensure करता है कि browser इसे सिर्फ एक encrypted HTTPS connection पर भेजे।

उदाहरण: Cookie Security Options

php
<?php
// Call `setcookie("token", "abc123", ['httponly' => true, 'secure' => true])`
setcookie("token", "abc123", ['httponly' => true, 'secure' => true]);
// Print "HttpOnly blocks JS access; Secure requires HTTPS" to the output
echo "HttpOnly blocks JS access; Secure requires HTTPS";
?>

Cookies Delete करना

किसी cookie की expiration को past में एक timestamp पर set करना इसे delete करने का standard तरीका है, क्योंकि कोई dedicated delete function नहीं है -- browser expired date देखता है और अपनी अगली check पर cookie discard कर देता है।

उदाहरण: Deleting Cookies

php
<?php
// Call `setcookie("username", "", time() - 3600)`
setcookie("username", "", time() - 3600);
// Print "Expiration set in the past -- cookie will be discarded" to the output
echo "Expiration set in the past -- cookie will be discarded";
?>

Check करना कि Cookies Enabled हैं या नहीं

आप एक page load पर एक throwaway cookie set करके और अगली पर इसकी presence check करके cookie support test कर सकते हैं; अगर यह missing है, visitor का browser शायद cookies disabled या blocked रखता है।

उदाहरण: Checking if Cookies are Enabled

php
<?php
// Call `setcookie("test_cookie", "1")`
setcookie("test_cookie", "1");
// Check whether `isset($_COOKIE['test_cookie'])`
if (isset($_COOKIE['test_cookie'])) {
    // Print "Cookies are enabled" to the output
    echo "Cookies are enabled";
// Otherwise, run this branch
} else {
    // Print "Cookies may be disabled -- check on next page load" to the output
    echo "Cookies may be disabled -- check on next page load";
}
?>
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. इसे set करने वाली same request में एक cookie पढ़ने की कोशिश करना, जबकि यह सिर्फ अगली request पर वापस भेजी जाती है।
  2. इसे set करने वाले से अलग path या domain के साथ एक cookie delete करना, ताकि यह हटे ही नहीं।
  3. किसी session-related cookie के लिए httponly और secure options छोड़ देना।
🔒

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.