A cookie is set with the following code:setcookie(name, value, expiration)
The above code sets a cookie in the visitor's browser called "AboutVisit". The cookie sets the value to the current date, and set's the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)
Now let's retrieve the cookie.
This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.
TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!
To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. This is often done when you 'logout' of a site. Here is an example:
For details visit : http://sricharanphp....s-with-php.html
Tags : Php Cookie, set cookie, Cookies
<?php
$Month = 2592000 + time();
//this adds 30 days to the current time
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>
The above code sets a cookie in the visitor's browser called "AboutVisit". The cookie sets the value to the current date, and set's the expiration to be be in 30 days (2592000 = 60 seconds * 60 mins * 24 hours * 30 days.)
Now let's retrieve the cookie.
<?php
if(isset($_COOKIE['AboutVisit'])) {
$last = $_COOKIE['AboutVisit'];
echo "Welcome back! <br> You last visited on ". $last;
} else {
echo "Welcome to our site!";
}
?>
This code first checks if the cookie exists. If it does, it welcomes the user back and tells them when they last visited. If they are new, it skips this and prints a generic welcome message.
TIP: If you are calling a cooking on the same page you plan to set one - be sure you retrieve it first, before you overwrite it!
To destroy the cookie, simply use setcookie again, only set the expiration date to be in the past. This is often done when you 'logout' of a site. Here is an example:
<?php
$past = time() - 10; //this makes the time 10 seconds ago
setcookie(AboutVisit, date("F jS - g:i a"), $past);
?>
For details visit : http://sricharanphp....s-with-php.html
Tags : Php Cookie, set cookie, Cookies
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
Tags
My Blog Links
Recent Entries
-
-
-
Using Cookies with PHPon May 18 2012 04:10 AM
-
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
Categories
|
|



Leave Comment










|