Cookie is a file. It is used to identify the user. All of you see the login form with remember me check box. When you click it, it store your name in cookie. You can login again, when you open the page in your browser, even if you signout and close the browser. The cookie keep the information until it expired.
In php, you can
setcookie() is used to create cookies in php. The syntax for create cookies as follows as:
Cookies in php:
In php, you can
1. Create cookies,
2. Retrieve cookies,
3. Delete cookies.
Create cookies in php:
setcookie() is used to create cookies in php. The syntax for create cookies as follows as:
setcookie(name, value, expire, path, domain)
where,
- name is name of the cookie.
- value is value kept by cookie.
- expire is when the cookie expired. It is defined by time() + 60*60*24*30 (cookie set for 30 days). If set the expire is 0 or not defined, then the cookie is expired when close the session.
- path refers where the cookie is available. If the set '/', then the cookie will be available on whole domain. If you want to set cookies only particular folder, then you've to set path '/foldername/'. You can set for sub-folder too like '/folder/sub-folder/'. If it is not set, the cookies will be available only on current directory.
- domain refers where the cookies is available.
Consider the following example:
where,
- name is name of guru.
- guru is the cookie values.
- time()+ (60*5) means cookies expired after 5 minutes. If you want to set cookies in days, then use
time() + (60*60*24*1) where set expiration time for 1 day.
S_COOKIE['name'] is used to retrieve the cookie in php. Consider the following example:
Now you'll get output: guru
You can delete the cookies by set expire time is past in php. Consider following example in php:
signout.php:
When you run the welcome.php, your output like this:
guru
signout
When you click the signout, it delete cookies and navigates to welcome.php. Now you'll get output like below:
signout
Related Post:
<?php
setcookie('name','guru', time()+ (60*5) );
?>
setcookie('name','guru', time()+ (60*5) );
?>
where,
- name is name of guru.
- guru is the cookie values.
- time()+ (60*5) means cookies expired after 5 minutes. If you want to set cookies in days, then use
time() + (60*60*24*1) where set expiration time for 1 day.
Retrieve cookies in php:
S_COOKIE['name'] is used to retrieve the cookie in php. Consider the following example:
<?php
setcookie('name','guru', time()+ (60*5) );
echo $_COOKIE['name'];
?>
setcookie('name','guru', time()+ (60*5) );
echo $_COOKIE['name'];
?>
Now you'll get output: guru
Delete cookies in php:
You can delete the cookies by set expire time is past in php. Consider following example in php:
welcome.php:
<?php
setcookie('name','guru', time()+ (60*5) );
echo $_COOKIE['name'];
echo'<a href="signout.php">Signout</a>';
?>
setcookie('name','guru', time()+ (60*5) );
echo $_COOKIE['name'];
echo'<a href="signout.php">Signout</a>';
?>
signout.php:
<?php
setcookie('name','guru',time() - (60*5));
header('location:welcome.php');
?>
setcookie('name','guru',time() - (60*5));
header('location:welcome.php');
?>
When you run the welcome.php, your output like this:
guru
signout
When you click the signout, it delete cookies and navigates to welcome.php. Now you'll get output like below:
Related Post:
No comments:
Post a Comment