पीएचपी सेटकुकी () फ़ंक्शन
उदाहरण
निम्न उदाहरण "जॉन डो" मान के साथ "उपयोगकर्ता" नामक कुकी बनाता है। कुकी 30 दिनों (86400 * 30) के बाद समाप्त हो जाएगी। "/" का अर्थ है कि कुकी पूरी वेबसाइट पर उपलब्ध है (अन्यथा, अपनी पसंद की निर्देशिका का चयन करें)।
फिर हम कुकी "उपयोगकर्ता" (वैश्विक चर $_COOKIE का उपयोग करके) का मान प्राप्त करते हैं। कुकी सेट है या नहीं यह पता लगाने के लिए हम isset() फ़ंक्शन का भी उपयोग करते हैं:
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
परिभाषा और उपयोग
सेटकुकी () फ़ंक्शन एक कुकी को बाकी HTTP हेडर के साथ भेजने के लिए परिभाषित करता है।
कुकी का उपयोग अक्सर उपयोगकर्ता की पहचान करने के लिए किया जाता है। कुकी एक छोटी फ़ाइल है जिसे सर्वर उपयोगकर्ता के कंप्यूटर पर एम्बेड करता है। हर बार जब वही कंप्यूटर किसी ब्राउज़र वाले पेज का अनुरोध करता है, तो वह कुकी भी भेजेगा। PHP के साथ, आप कुकी मान बना सकते हैं और पुनर्प्राप्त कर सकते हैं।
कुकी का नाम स्वचालित रूप से उसी नाम के वेरिएबल को असाइन किया जाता है। उदाहरण के लिए, यदि कोई कुकी "उपयोगकर्ता" नाम से भेजी गई थी, तो कुकी मान वाले $user नामक एक चर स्वचालित रूप से बनाया जाता है।
नोट: सेटकुकी () फ़ंक्शन <html> टैग से पहले दिखाई देना चाहिए।
नोट: कुकी भेजते समय कुकी का मान स्वचालित रूप से URLencoded होता है, और प्राप्त होने पर स्वचालित रूप से डिकोड हो जाता है (URLencoding को रोकने के लिए, इसके बजाय setrawcookie() का उपयोग करें )।
वाक्य - विन्यास
setcookie(name, value, expire, path, domain, secure, httponly);
पैरामीटर मान
Parameter | Description |
---|---|
name | Required. Specifies the name of the cookie |
value | Optional. Specifies the value of the cookie |
expire | Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0 |
path | Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in |
domain | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain |
secure | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE |
httponly | Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | सफलता पर सच। विफलता पर FALSE |
---|---|
पीएचपी संस्करण: | 4+ |
पीएचपी चेंजलॉग: | PHP 5.5 - क्लाइंट को भेजे गए सेट-कुकी हेडर में एक अधिकतम-आयु विशेषता शामिल की गई थी PHP 5.2 - httponly पैरामीटर जोड़ा गया था |
और ज्यादा उदाहरण
उदाहरण
कुकीज़ के लिए कई समाप्ति तिथियां:
<?php
$value = "Hello world!";
// cookie will expire when the browser close
setcookie("myCookie", $value);
// cookie will expire in 1 hour
setcookie("myCookie", $value, time() + 3600);
// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie("myCookie", $value, time() + 3600, "/php/");
?>
<html>
<body>
...some code...
</body>
</html>
उदाहरण
किसी कुकी को संशोधित करने के लिए, बस सेटकुकी () फ़ंक्शन का उपयोग करके कुकी को (फिर से) सेट करें:
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
उदाहरण
एक कुकी को हटाने के लिए, अतीत में समाप्ति तिथि के साथ सेटकुकी () फ़ंक्शन का उपयोग करें:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
उदाहरण
एक छोटी सी स्क्रिप्ट बनाएं जो जांचती है कि कुकीज़ सक्षम हैं या नहीं। सबसे पहले, सेटकुकी () फ़ंक्शन के साथ एक परीक्षण कुकी बनाने का प्रयास करें, फिर $_COOKIE सरणी चर की गणना करें:
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
पीएचपी नेटवर्क संदर्भ