You can find cookies on internet. Cookies are made for webmasters, so that they can save data related to the users and the stopover on other computers.
In this lecture, you will get to know about the basic way of saving and recovering cookie. You can also fix some options with the cookies.
Make a Cookie in PHP:
You must know that while making a cookie, you have to declare 3 arguments. These are name , value and expiration.
1. name: It shows name of that cookie. The name of the cookie should be remembered.
2. value: It shows the value saved in cookie. Mostly, the name of the client and their last visit is stored in the cookies.
3. expiration: It shows the date of expiration. In case, you forget to give the date, it will erased with resume of browser.
Here, we are going to make a cookie. It will keep the last view and will also show the number of people, who have visited this webpage. We are going to set the deletion date equal to 2 months.
Code of PHP:
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
The significant thing is to know the way of making a cookie. It needs three basic arguments which are mentioned above.
How to recover the new cookie?
You can get back your cookie from the computer of the user, only if your cookie is not expired. We will use the command $_COOKIE. You can get your cookie back by using its perfect name.
$visit = $_COOKIE['lastVisit'];
echo "These cookies are out of date!";
echo "Last time of your visit is - ". $visit;
First of all, we have to know about the presence of cookie. If we want to check our cookie then it is must that it should not be expired. We can get to know about this by using a function named as “isset”. We want to see the cookie related to last visit. If it exists then we can get to know about the visitor. The result will be as given below:
Last time of your visit is -12:23 – 08/12/10