PHP setcookie() လုပ်ဆောင်ချက်
ဥပမာ
အောက်ပါဥပမာသည် "အသုံးပြုသူ" ဟူသောတန်ဖိုး "ဂျွန်ဒို" ဖြင့် ကွတ်ကီးတစ်ခုကို ဖန်တီးသည်။ ကွက်ကီးသည် ရက်ပေါင်း 30 ကြာပြီးနောက် (86400 * 30) သက်တမ်းကုန်ဆုံးပါမည်။ "/" ဆိုသည်မှာ ကွက်ကီးကို ဝဘ်ဆိုက်တစ်ခုလုံးတွင် ရနိုင်သည် (မဟုတ်ပါက သင်နှစ်သက်ရာ လမ်းညွှန်ကို ရွေးပါ)။
ထို့နောက် ကျွန်ုပ်တို့သည် ကွတ်ကီး "အသုံးပြုသူ" ၏တန်ဖိုး (ကမ္ဘာလုံးဆိုင်ရာ ပြောင်းလဲနိုင်သော $_COOKIE ကိုအသုံးပြု၍) ထုတ်ယူသည်။ 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>
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
setcookie() လုပ်ဆောင်ချက်သည် ကျန် HTTP ခေါင်းစီးများနှင့်အတူ ပေးပို့မည့် cookie တစ်ခုကို သတ်မှတ်သည်။
အသုံးပြုသူတစ်ဦးကို ခွဲခြားသတ်မှတ်ရန် ကွတ်ကီးကို မကြာခဏအသုံးပြုသည်။ ကွတ်ကီးသည် အသုံးပြုသူ၏ကွန်ပျူတာပေါ်တွင် ဆာဗာထည့်သွင်းထားသည့် ဖိုင်ငယ်တစ်ခုဖြစ်သည်။ တူညီသောကွန်ပျူတာသည် browser တစ်ခုနှင့်စာမျက်နှာတစ်ခုတောင်းဆိုသောအခါတိုင်း၊ ၎င်းသည် cookie ကိုလည်းပေးပို့လိမ့်မည်။ PHP ဖြင့် သင်သည် ကွတ်ကီးတန်ဖိုးများကို ဖန်တီးနိုင်ပြီး ထုတ်ယူနိုင်သည်။
ကွတ်ကီးအမည်ကို တူညီသောအမည်ကွဲလွဲချက်တစ်ခုသို့ အလိုအလျောက်သတ်မှတ်ပေးသည်။ ဥပမာအားဖြင့်၊ "အသုံးပြုသူ" အမည်ဖြင့် ကွတ်ကီးကို ပို့လိုက်လျှင် ကွတ်ကီးတန်ဖိုးပါရှိသော $user ဟုခေါ်သော ကိန်းရှင်ကို အလိုအလျောက် ဖန်တီးပေးပါသည်။
မှတ်ချက်- setcookie() function သည် <html> tag မတိုင်မီ ပေါ်လာရပါမည်။
မှတ်ချက်- ကွတ်ကီး၏တန်ဖိုးကို ကွတ်ကီးကို ပေးပို့သည့်အခါ အလိုအလျောက် URL ကုဒ်လုပ်ထားပြီး လက်ခံရရှိသည့်အခါတွင် အလိုအလျောက် ကုဒ်လုပ်သည် (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 |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | အောင်မြင်မှုအပေါ် သစ္စာရှိပါ။ ရှုံးနိမ့်မှုအပေါ် မှားသည်။ |
---|---|
PHP ဗားရှင်း- | 4+ |
PHP Changelog- | PHP 5.5 - သုံးစွဲသူ PHP 5.2 သို့ပေးပို့သည့် Set-Cookie ခေါင်းစီးတွင် Max-Age ရည်ညွှန်းချက်တစ်ခု ထည့်သွင်းခဲ့သည် - 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>
ဥပမာ
ကွက်ကီးတစ်ခုကို မွမ်းမံရန်၊ setcookie() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ကွတ်ကီးကို (တစ်ဖန်) သတ်မှတ်လိုက်ပါ-
<?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>
ဥပမာ
ကွက်ကီးတစ်ခုကို ဖျက်ရန် ယခင်က သက်တမ်းကုန်ဆုံးရက်နှင့်အတူ setcookie() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
ဥပမာ
ကွက်ကီးများကို ဖွင့်ထားခြင်းရှိမရှိ စစ်ဆေးသည့် သေးငယ်သော script တစ်ခုကို ဖန်တီးပါ။ ပထမဦးစွာ၊ setcookie() လုပ်ဆောင်ချက်ဖြင့် စမ်းသပ် cookie တစ်ခုကို ဖန်တီးရန် ကြိုးစားပါ၊ ထို့နောက် $_COOKIE array variable ကို ရေတွက်ပါ-
<?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>
❮ PHP Network ရည်ညွှန်းချက်