Introduction Cookies have long been used in PHP scripts, and are a very useful function. But what exactly are cookies? Maybe you have used then, but you still don't know exactly what they are. Or you are completely new to cookies? It doesn't matter, because in this tutorial I will show you exactly what cookies are, and what they are used for.
Cookies in a nutshell Cookies are small pieces of information that is stored on computer of your visitors. Each browser handles it differently, but most simply store information in a small text file. Internet Explorer has a special folder, which can be found in your C:Windows or C:WindowsSystem32 folder. You can delete all your cookies, by going to Options and 'Clearing Cookies' or deleting them by hand. I don't recommend this though.
Or you could remember their last visit, and highlight everything that is new. And that's just beginning.
Using Cookies Using cookies in PHP is extremely easy. In fact, there is nothing to it, because of PHP's inbuilt setcookie() function. Have a look at documentation, and then try following example:
// Set a cookie // Cookie name: name // Cookie value: Dennis Pallett // Cookie expire: in 24 hours
setcookie ('name', 'Dennis Pallett', time() + (60*60*24)); ?>
If you run code above, then a cookie will be set. That's all. The cookie name and value are pretty obvious. The cookie expire is when cookie expires, or goes away. Simply use time() function and add number of seconds you want to have cookie available to it. In example I added 60*60*24=86400 seconds, or 24 hours.
If you have looked at documentation, you probably noticed there are additional arguments. As documentation says, path is to limit a cookie to a specific path on your web server. This is often used when you run multiple instances of same script in separate directories. You can safely omit this argument when it doesn't matter if cookie is available site-wide.
There is also domain argument. This can be used to limit cookie to a specific sub-domain, e.g. test.example.com. You can also safely ignore this argument, or set it to .example.com (note beginning period, this is essential!).
Finally, there is also secure argument. This argument is only used for cookies that are sent over a secure HTTPS connection (SSL). Just ignore this argument, unless you're working with a secure connection.
One thing that should be mentioned is that cookies must be set, before you display any HTML/text. It's probably best if you turn on output buffering by putting ob_start() at top of your page.
Now that you have set a cookie, you probably want to retrieve value as well. After all, that is whole point of using cookies. Thankfully, as PHP is ever so easy, you can retrieve same way as you retrieve a GET value. See following example to retrieve value of previous example:
<?php echo 'Your name is ' . $_COOKIE['name']; ?>
This should print "Your name is Dennis Pallett". There's nothing more to it. It's just that easy!
Finally, one thing you probably want to do as well is remove cookies. This is as easy as setting them. Simply change value of cookie to FALSE, and change expire date to -3000 seconds. See following example: