Cookie used to store user data in form of text file or memory and access the values from it, Cookie are the part of HTTP header.
To Create cookie
setcookie("cookie_name", "cookie_value", time()+60, "/");
Accessing a cookie
$_COOKIE['cookie_name'];
Deleting Cookie
unset($_COOKIE['cookie_name']);
Another way of deleting cookie
setcookie("cookie_name", "cookie_value", time()-60, "/");
Persistent Cookie which will store the data in text file with expire date and time. If you are access the website or particular page cookie will be created based on cookie name. Next time If you are visiting the website or page it will be accessible from the cookie file which was created in the client browser.
Also Persistent Cookie is unsafe, because it will be easy viewable in the browser.
Above PHP file will create cookie and expire within 1 hour, you can view the cookie expire time in browser cookie part.
What is Non-Persistent Cookie ?
Non-Persistent Cookie create in memory and access from it, If you close the browser the cookie will be deleted automatically. So next time if you are visiting the website the cookie won’t be exists because it not stored in the text file. So Non-Persistent Cookie are safer than the Persistent Cookie.
non_persistent_cookie.php
cookie_value.php
To Create cookie
setcookie("cookie_name", "cookie_value", time()+60, "/");
Accessing a cookie
$_COOKIE['cookie_name'];
Deleting Cookie
unset($_COOKIE['cookie_name']);
Another way of deleting cookie
setcookie("cookie_name", "cookie_value", time()-60, "/");
In php we have 2 types of cookies persistent cookie and non-persistent cookie
What is Persistent Cookie ?
create 2 files with names persistent_cookie.php and cookie_value.php
persistent_cookie.php
<?php
// creating the name:cookie_name_persistent and value:my cookie value with expire 1 hour
setcookie(
"cookie_name_persistent"
,
"my cookie value"
,time()+3600);
cookie_value.php
<?php
// checking the cookie_name_persistent cookie is created or not
if
(isset(
$_COOKIE
[
"cookie_name_persistent"
])) {
// accessing the cookie_name_persistent values from the cookie file
echo
"Persistent Cookie Value : "
.
$_COOKIE
[
"cookie_name_persistent"
];
}
create 2 files with names non_persistent_cookie.php and cookie_value.php
<?php
// creating the name:cookie_name_non_persistent and value:my cookie value without expire time
setcookie(
"cookie_name_non_persistent"
,
"my cookie value"
);
<?php
// checking the cookie_name_non_persistent cookie is created or not
if
(isset(
$_COOKIE
[
"cookie_name_non_persistent"
])) {
// accessing the cookie_name_non_persistent values from the memory
echo
"Non Persistent Cookie Value : "
.
$_COOKIE
[
"cookie_name_non_persistent"
];
}
0 comments:
Post a Comment