PHP Cookies Introduction
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
Cookie Expiration Dates Set करना
Cookies browser बंद होने पर automatically delete हो जाती हैं जब तक आप setcookie() से एक explicit expiration timestamp set न करें, जो है कि एक 'remember me' checkbox actually sessions में persist क्यों करता है।
उदाहरण: Setting Cookie Expiration Dates
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Cookie Paths और Domains Configure करना
किसी cookie के path और domain parameters exactly control करते हैं कि आपके server पर कौन से URLs इसे वापस पढ़ सकते हैं, आपको ज़रूरत के हिसाब से एक cookie को एक subfolder तक scope करने या subdomains में share करने देते हुए।
उदाहरण: Configuring Cookie Paths and Domains
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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.";
?>
Login to try C/C++/Java/PHP code in the editor
Cookies Delete करना
क्योंकि कोई direct delete operation नहीं है, एक cookie हटाने का मतलब है setcookie() को same name के साथ फिर call करना लेकिन past में एक expiration time के साथ, browser को इसे तुरंत discard करने के लिए बताते हुए।
उदाहरण: Deleting Cookies
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
- output शुरू होने के बाद
setcookie()call करना, जो एक headers already sent warning के साथ fail हो जाता है। - उम्मीद करना कि नई cookie इसे set करने वाली same request के दौरान
$_COOKIEमें दिखेगी, जबकि यह सिर्फ अगली request पर दिखती है। - किसी cookie में passwords जैसा sensitive data store करना, जिसे user पढ़ और edit कर सकता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: