Aug 12, 2019

What is a cookie?

If you are working with web applications you may have heard about cookies. Also when we browsing internet though a web browser we see this term often. So in today's tutorial I selected this topic to discuss with you. Actually there are other type of cookies too. There is a term stack cookies in binary exploitation that prevent stack smashing . Also we know there is a delicious food called cookies. I think it's another name for biscuits . correct me if I'm wrong. Anyway let's talk about our topic. Why web applications use cookies? If you registered in HacksLand's forum you may have seen that there is a option called "remember me" at the login page. What it does. If you click that option when you login web app doesn't ask your password for some time.(It may be one/two months or an year) . But what happen if you try to login from another browser? It says to login or register. This is because When you choose remember me option, web application will create a cookie on your browser with some validity date. so When you visit the site again web app check if your browser has a session cookie. If it is it'll never ask for password and create a session for you. I'll explain about sessions in our PHP tutorial. Let's see how we can create and view cookies . First I'm going to show you how to use cookies in JavaScript. You know that JavaScript is a client side scripting language and executed on user's browser. In following code spinet I create a cookie named "user". So it'll keep the name of current user.
Now we want to view it. There are many ways to do that. We can use alert() function like following.
JavaScript cookie example Also we can use innerHtml method also.
<div id='cookie'>

Also there is another parameter called "expires" .It'll decide when to expire this cookie. If you don't specify a date it'll last till you close the browser.
document.cookie="user=Thilan; expires=Thu, 31 Dec 2020 12:00:00 UTC";
Now let's see how we can do it in PHP.  PHP has a function called setcookie() and we can use it to create a cookie. It'll expect 4 arguments. Cookie name, Value time to expire and Path. In JavaScript we gave a date as the expire date. But hear we give a time duration . The time() function will return current time. We add a number of seconds to it.  If we want to last a cookie for one hour we can give 3600 [60 x 60] as the value. If we want 1 day we have to use 86400 [60 x 60 x 24] . If you want one year you'll get 86400 x 365 .That's too boring to calculate as this.  .  (Do you know in  my A/L Physics paper I had to calculate 400 years in to seconds. I think it was an Atomic radiation question. I couldn't decide whether I use 365 or 366 :-) ) Anyway hear is our function.
setcookie('user','Thilan', time() + (3600), "/");
It'll create a cookie named 'user' and it will hold the value 'Thilan' . In following code we get the value from our newly created cookie. This is very similar to get a value from $_SESSION[] or $_GET[] variables. Like SERVER,GET,POST and SESSION  COOKIE is  also a global variable. That means we can use it in anywhere.
if(!isset($_COOKIE['user')) {
    echo 'No cookie named \'user\'' ;
} else {

    echo "Value is: " . $_COOKIE['user'];
In first line I used isset() function to check if Cookie variable is set or not. It's a good programming practice to check the variable is live or not before access it's value. So guys it's all for this tutorial. I'll write more on these web developing topics. Thank you for reading.

Jul 02
PHP Secure fie uploading

Hear I'm going to share a easy and secure way to upload your images and other files. You have to be....

Mar 09
What are the most important directories in Linux

Working with a Linux distribution is a completely different thing. If you are a windows user you....

Mar 09
Best ethical hacking books for beginners

Hacking is an interesting area in computer science to study. In this article, I'm going to....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.