Web Cookies

Gajindu Bandara
2 min readDec 23, 2021

Origin of the cookies … The term “cookie” was coined by web programmer Lou Montulli. It was derived from the term “magic cookie”, which is a packet of data a program receives and sends back unchanged, used by Unix programmers.

What is a cookie? It's a small piece of text sent to your browser by a website you visit.

Why do we need cookies?? It helps to keep records of the sites you visit.

At the present, we can see most of the websites are using cookies to keep records so there are advantages and disadvantages of using cookies. As advantages, web cookies are very user-friendly and convenient. Mostly the cookies are used for marketing purposes. As the disadvantages security impacts and privacy concerns are there. Also, there are size limitations for the cookies.

Now I’m gonna show you how to create a web page with a button that remembers how many times that you have clicked. Tough you refresh the page the count stays the same because of the cookie. Let’s get started!!!

I'm gonna do this with PHP so first I create a new file. The next thing is to create a form with the post method and there should be a button. That's the initial step the next thing is to create a new cookie and write a simple condition which counts the clicks and assigns the value to the cookie.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>

<form method="post">
<input type="submit" name="btnClick" value="Click Me!">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] = "POST"){

$count=1;
if (isset($_COOKIE["clicks"]))
{
$count=$_COOKIE["clicks"];
}
echo $count;
$count++;
setcookie("clicks",$count);


}
?>
</body>
</html>

--

--