Today we are going to discuss about cookies. Cookies are just like sweet donuts which work efficiently for you as long as you treat them efficiently. Well in simple words, cookies are the finished products which reside at the client side and you can reuse them whenever you require (only if they are not expired). Cookies get stored in the folder determined by the browser , the scripts that we write have the limited access to that file, i.e. we can just read & write the data of certain length. No you cannot even spam someone, there is certain limit to store cookies at the client site. One domain can have only 20 cookies at a time each being less than 4Kilobyte.
A simple cookie has the following properties:
A cookie Name
Cookie value
Cookie's expiration time
Cookies store certain values which u don't want to store on your server or if the data isn't that important to be handled secretly. We can store the number of visits from that IP, we can store the last time that person visit your website, his preferences on your website etc etc. But be sure, that this information is washed out when the end user clears his/her browser's cookies, or if they expire (that we will see later). So, this is a small token which makes your website more interactive & functional.
Setting a cookie in PHP :
A very simple function called setcookie() is used to set the cookies in php.
This way we can set a simple cookie, wondering why the cookie did not appear at once you requested the page. Well it is obvious, the first time you loaded the page the headers were sent and the cookie was created, but had nothing to display cause when the output was sent to the browser by the server at that time the cookie did not exist, so you have refresh the page once to see the set cookie. This is the only reason why at times programmers use session variables to store information or even choose URLs.
Cookie Expiration:
Now that the cookie is set, of course you want to delete it at some particular time, right? The third argument which we passed in the above function setcookie is for that only. It sets the time of the cookie to live. Apparently, you can see in the above code, we wrote time()+3600, meaning the cookie will live 3600 secs from the current time. So in the same way, if we write time()-3600 the cookie will definitely expire or will die from the client side. So this is the best method to delete a cookie. Certainly the fact is, if the client himself clears the cookie folder then you wont be able to help it ;)
Now some simple steps to see this script working.
1) Copy the above code and save this file as test_cookie.php (or any other filename).
2) Open Internet explorer and run the script file.
3) At first you will see the message You do not have a cookie set.
4) Now try refreshing the page.
5) This time you will see the value of the cookie you see in the setcookie method
6) Now goto start->run and type cookies, a folder will be opened.
7) To view your cookie file, look for the domain you are using. It can be localhost if you are doing it locally. This folder contains all the cookies of other websites which may have set them when you visited them.
Some safe practices:
Try to encrypt your values and/or keys with an algorithm like base64. Alternatively you can add a key to the cookie, which you can always check while reusing the cookie, that if the cookie has been tampered or no?
Do not store password in the cookie (especially clear text passwords), they can invite trouble for you.
So you see, you can have lots of fun with cookies and you can make your website much more interesting.
Liked it, Bookmark Article
A simple cookie has the following properties:
A cookie Name
Cookie value
Cookie's expiration time
Cookies store certain values which u don't want to store on your server or if the data isn't that important to be handled secretly. We can store the number of visits from that IP, we can store the last time that person visit your website, his preferences on your website etc etc. But be sure, that this information is washed out when the end user clears his/her browser's cookies, or if they expire (that we will see later). So, this is a small token which makes your website more interactive & functional.
Setting a cookie in PHP :
A very simple function called setcookie() is used to set the cookies in php.
<?php
setcookie("test","This is my first test cookie value",time()+3600);
if($_COOKIE['test'])
echo $_COOKIE['test'];
else
echo "You do not have a cookie set";
?>
This way we can set a simple cookie, wondering why the cookie did not appear at once you requested the page. Well it is obvious, the first time you loaded the page the headers were sent and the cookie was created, but had nothing to display cause when the output was sent to the browser by the server at that time the cookie did not exist, so you have refresh the page once to see the set cookie. This is the only reason why at times programmers use session variables to store information or even choose URLs.
Cookie Expiration:
Now that the cookie is set, of course you want to delete it at some particular time, right? The third argument which we passed in the above function setcookie is for that only. It sets the time of the cookie to live. Apparently, you can see in the above code, we wrote time()+3600, meaning the cookie will live 3600 secs from the current time. So in the same way, if we write time()-3600 the cookie will definitely expire or will die from the client side. So this is the best method to delete a cookie. Certainly the fact is, if the client himself clears the cookie folder then you wont be able to help it ;)
Now some simple steps to see this script working.
1) Copy the above code and save this file as test_cookie.php (or any other filename).
2) Open Internet explorer and run the script file.
3) At first you will see the message You do not have a cookie set.
4) Now try refreshing the page.
5) This time you will see the value of the cookie you see in the setcookie method
6) Now goto start->run and type cookies, a folder will be opened.
7) To view your cookie file, look for the domain you are using. It can be localhost if you are doing it locally. This folder contains all the cookies of other websites which may have set them when you visited them.
Some safe practices:
Try to encrypt your values and/or keys with an algorithm like base64. Alternatively you can add a key to the cookie, which you can always check while reusing the cookie, that if the cookie has been tampered or no?
Do not store password in the cookie (especially clear text passwords), they can invite trouble for you.
So you see, you can have lots of fun with cookies and you can make your website much more interesting.
Share your views:
Name (required)
Email (will not be displayed)(required)
min. 10 characters
| Enter Result to prove you are human |


![Validate my RSS feed [Valid RSS]](http://thinkofcode.com/ask/img/valid-rss.png)

Nice brief explanation