How to Check is Browser Cookie enabled or disabled using PHP?

How to Check is Browser Cookie enabled or disabled using PHP?

As we know HTTP is a stateless protocol. In web during Client Server communication to identify a particular user it is required to maintain state. In this cause there are several state management techniques. State management techniques are available for both the side client & server. Cookie is a client side state management technique.

A Cookie is a simple text file. It can store maximum 4MB data. Due to cookies are resides in client machine in text format storing data in a cookie is not secured. Cookies are two types session cookies & persistent cookies. Session cookies are available for only that time user is interacting. Once the user close the instance of browser session cookies get destroyed. Where persistent cookies having an expiry time. During we create a cookie we have to set the expiry time for persistent cookies. Expiry time can be a day, month or a year too. Cookies are generally used for websites that have huge databases, having signup & login, have customization themes other advanced features.

Before create a cookie using any programming language we need to check first is Cookies enabled in the client browser or not. Programmatically to check this here I wrote a small php script. Which will tell you is in your machine cookies are enabled or disabled.

The logic I implemented in below script is so simple. Using setcookie() method in php I am creating a cookie with the name demo-cookie. Later using php count() function I am counting the number of cookies available in your machine. If it is greater then 0 then my cookie demo-cookie is created successfully. It means in your browser cookies are enabled. In reverse case if count is not grater then 0 then in your browser cookies are disabled. To enable cookies in your browser go to the browser setting.

is-Cookie-enabled.php

<?php
setcookie("demo-cookie", "demo-data", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled in your Browser.";
} else {
echo "Cookies are disabled in your Browser.";
}
?>
</body>
</html>