PHP setrawcookie() လုပ်ဆောင်ချက်
ဥပမာ
အောက်ပါဥပမာသည် PHP ဖြင့် cookie တစ်ခုကိုဖန်တီးသည်။ ကွက်ကီးကို "အသုံးပြုသူ" ဟု အမည်ပေးထားပြီး တန်ဖိုးမှာ "ဂျွန်ဒို" ဖြစ်သည်။ ကွက်ကီးတန်ဖိုးသည် URL ကုဒ်လုပ်မည်မဟုတ်ပါ။ ကွက်ကီးသည် ရက်ပေါင်း 30 ကြာပြီးနောက် (86400 * 30) သက်တမ်းကုန်ဆုံးပါမည်။ "/" ကိုအသုံးပြုခြင်းဖြင့် ကွက်ကီးသည် ဝဘ်ဆိုက်တစ်ခုလုံးတွင် ရနိုင်သည်ဟု ဆိုလိုသည် (မဟုတ်ပါက သင်နှစ်သက်ရာ လမ်းညွှန်ကို ရွေးပါ)။
<?php
$cookie_name = "user";
$cookie_value = "John";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>
<?php
echo "Cookie is set.";
?>
</body>
</html>
?>
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
setrawcookie() လုပ်ဆောင်ချက်သည် ကျန် HTTP ခေါင်းစီးများနှင့်အတူ ပေးပို့ရန် ကွတ်ကီး (URL ကုဒ်သွင်းခြင်းမရှိဘဲ) ကို သတ်မှတ်သည်။
အသုံးပြုသူတစ်ဦးကို ခွဲခြားသတ်မှတ်ရန် ကွတ်ကီးကို မကြာခဏအသုံးပြုသည်။ ကွတ်ကီးသည် အသုံးပြုသူ၏ကွန်ပျူတာပေါ်တွင် ဆာဗာထည့်သွင်းထားသည့် ဖိုင်ငယ်တစ်ခုဖြစ်သည်။ တူညီသောကွန်ပျူတာသည် browser တစ်ခုနှင့်စာမျက်နှာတစ်ခုတောင်းဆိုသောအခါတိုင်း၊ ၎င်းသည် cookie ကိုလည်းပေးပို့လိမ့်မည်။ PHP ဖြင့် သင်သည် ကွတ်ကီးတန်ဖိုးများကို ဖန်တီးနိုင်ပြီး ထုတ်ယူနိုင်သည်။
ကွတ်ကီး၏အမည်ကို တူညီသောအမည်ကွဲလွဲချက်တစ်ခုသို့ အလိုအလျောက်သတ်မှတ်ပေးသည်။ ဥပမာအားဖြင့်၊ "အသုံးပြုသူ" အမည်ဖြင့် ကွတ်ကီးကို ပို့လိုက်လျှင် ကွတ်ကီးတန်ဖိုးပါရှိသော $user ဟုခေါ်သော ကိန်းရှင်ကို အလိုအလျောက် ဖန်တီးပေးပါသည်။
မှတ်ချက်- setrawcookie() function သည် <html> tag မတိုင်မီ ပေါ်လာရပါမည်။
မှတ်ချက်- ပေးပို့သည့်အခါ ကွတ်ကီးတန်ဖိုးကို URL-ကုဒ်ဖြင့် အလိုအလျောက် ကုဒ်လုပ်ပြီး လက်ခံသည့်အခါ အလိုအလျောက် ကုဒ်လုပ်ရန်၊ ၎င်း အစား setcookie() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။
အထားအသို
setrawcookie(name, value, expire, path, domain, secure);
ကန့်သတ်တန်ဖိုးများ
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 not set, the cookie will expire at the end of the session (when the browser closes) |
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. |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | အောင်မြင်မှုအပေါ် သစ္စာရှိပါ။ ရှုံးနိမ့်မှုအပေါ် မှားသည်။ |
---|---|
PHP ဗားရှင်း- | ၅+ |
နောက်ထပ် ဥပမာများ
ဥပမာ
"အသုံးပြုသူ" ဟု အမည်ပေးထားသည့် ကွတ်ကီး၏တန်ဖိုး (ကမ္ဘာလုံးဆိုင်ရာ ပြောင်းလဲနိုင်သော $_COOKIE ကို အသုံးပြု၍) ထုတ်ယူပါ။ cookie ရှိမရှိ သိရှိနိုင်ရန် isset() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
ဥပမာ
ကွက်ကီးတစ်ခုကို မွမ်းမံရန်၊ setrawcookie() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ကွတ်ကီးကို (တစ်ဖန်) သတ်မှတ်လိုက်ပါ-
<?php
$cookie_name = "user";
$cookie_value = "Alex";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
ဥပမာ
ကွက်ကီးတစ်ခုကို ဖျက်ရန်၊ ယခင်က သက်တမ်းကုန်ဆုံးရက်စွဲဖြင့် setrawcookie() လုပ်ဆောင်ချက်ကို အသုံးပြုပါ-
<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
ဥပမာ
ကွက်ကီးများကို ဖွင့်ထားခြင်းရှိမရှိ စစ်ဆေးသော ဇာတ်ညွှန်းအသေးစားတစ်ခုကို ဖန်တီးပါ။ ပထမဦးစွာ setrawcookie() လုပ်ဆောင်ချက်ဖြင့် စမ်းသပ် cookie တစ်ခုကို ဖန်တီးရန် ကြိုးစားပါ၊ ထို့နောက် $_COOKIE array variable ကို ရေတွက်ပါ-
<?php
setrawcookie("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 ရည်ညွှန်းချက်