PHP Cookie Management
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
Cookie Expiration Set करना
अगर आप setcookie() को कोई expiration time pass नहीं करते, cookie को एक session cookie माना जाता है और browser बंद होते ही गायब हो जाती है -- explicitly एक future timestamp set करना वह है जो visits में data persist करवाता है।
उदाहरण: Setting Cookie Expiration
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
Cookies Delete करना
किसी cookie की expiration को past में एक timestamp पर set करना इसे delete करने का standard तरीका है, क्योंकि कोई dedicated delete function नहीं है -- browser expired date देखता है और अपनी अगली check पर cookie discard कर देता है।
उदाहरण: Deleting Cookies
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- इसे set करने वाली same request में एक cookie पढ़ने की कोशिश करना, जबकि यह सिर्फ अगली request पर वापस भेजी जाती है।
- इसे set करने वाले से अलग path या domain के साथ एक cookie delete करना, ताकि यह हटे ही नहीं।
- किसी session-related cookie के लिए
httponlyऔरsecureoptions छोड़ देना।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: