[X] Choose Font Here

PHP Cookies

 

User ေတြကို identify လုပ္ႏိုင္ဖို ့ cookies ကို သံုးရတာျဖစ္ပါတယ္။

 

What is a Cookies?

User ကို ဘယ္လို မွတ္မိေအာင္လုပ္လဲဆိုရင္ေတာ့ user ရဲ ့computer ထဲမွာ ေသးငယ္တဲ့ cookie file ကိုလာျပီး embed

လုပ္ထားတာျဖစ္ပါတယ္။ Browser ကေနျပီး request လုပ္ျပီဆိုတာနဲ ့ Server ကေနျပီး အဲ့ဒီ computer အတြက္ cookie file ကို

embed လုပ္ဖို ့send လုိက္တာျဖစ္ပါတယ္။ PHP မွာ အဲ့ဒီ cookie value ေတြကို create ေရာ retrieve ပါလုပ္ႏိုင္ပါတယ္။



How to create a cookie?

Cookie ကို create လုပ္ေတာ့မယ္ဆိုရင္ setcookie( )ဆိုတဲ့ Function ကို သံုးရပါတယ္။ setcookie ကုိ ေရးမယ္ဆိုရင္ေတာ့

<htm> tag မတိုင္ခင္မွာ ေရးရပါမယ္။


setcookie(name, value, expire, path, domain);


ဒီ Example မွာဆိုရင္ user ဆိုတဲ့ cookie တစ္ခုကို create လုပ္ျပီး ‘zack’ ဆိုတဲ့ value ကို assign လုပ္ထားတာျဖစ္ပါတယ္။

ဘယ္အခ်ိန္မွာ ဒီ cookie ဟာ expire ျဖစ္သြာမယ္ဆိုတာကိုပါ သတ္မွတ္ထားႏိုင္ပါတယ္။


<?php
setcookie("user", "zack", time()+3600);
?>
<html>
....


ဒီ cookie value ေတြကို Browser မွာ send ေနတဲ့ အခ်ိန္မွာ URLencoded နဲ ့သြားတာျဖစ္ပါတယ္။ Receive လုပ္တဲ့

အခ်ိန္မွာ Auto URLdecode ျပန္လုပ္ပါတယ္။

How to Retrieve a Cookie Value?

PHP မွာ cookie value ကို Retrieve လုပ္ဖို ့ $_COOKIE ဆိုတဲ့ variable ကို သံုးပါတယ္။ ေအာက္က code ကေတာ့ User

ဆိုတဲ့ cookie ထဲက value ကို $_COOKIE သံုးျပီး display ျပန္ရိုက္ျပတာျဖစ္ပါတယ္။


<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>


ေနာက္ sample ကေတာ့ ကိုယ့္ရဲ ့ website ကို ၀င္ၾကည့္ေနတဲ့ User ဟာ Register user လား၊ Guest လားဆိုတာကို

စစ္ထားတဲ့ code ျဖစ္ပါတယ္။ inset function ကို သံုးထားပါတယ္။ PHP sessions တုန္းက ေျပာျပီးသားျဖစ္ပါတယ္။

<html>
<body>
<?php
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
else
echo "Welcome guest!<br />";
?>
</body>
</html>


How to Delete a Cookie?

Cookie ကို delete လုပ္ေတာ့မယ္ဆိုရင္ေတာ့ ကိုယ္ထည့္ထားတဲ့ expire date ကို ေက်ာ္လြန္ခဲ့ ျပီ

ဆိုတာကို ေသခ်ာေအာင္လုပ္သင့္ပါတယ္။

 

<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>


သေဘာကေတာ့ setcookie ဆိုတဲ့ function ကိုပဲ သံုးျပီး expire ျဖစ္မယ့္ Time ကိုသာ change သြားတာျဖစ္ပါတယ္။

ဒါကေတာ့ PHP cookie ရဲ ့ အေျခခံသေဘာတရားပဲ ျဖစ္ပါတယ္။

 

တျခား PHP Post ေတြအားလုံး ကို ႏွစ္သက္ရာ ေရြးဖတ္ ခ်င္တယ္ဆိုရင္ ဒီ Link ေလးကို Click လိုက္ပါ။

 

 

Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

Login Form

Categories Table View

JoomlaWatch Stats 1.2.9 by Matej Koval

Facebook Share

Share on facebook

Accordion FAQ

mod_joomtouch

Version Iphone

Version Iphone by JoomTouch