How to check browser cookie enabled with php and javascript

Cookies are typically used by web servers to perform functions such as tracking your visits to websites, enabling you to log in to sites, and storing your shopping cart. Cookies can be used to interaction between Javascript and Server Side Scripts like PHP.Cookies also used to transfer small amount of data between one page to other pages via HTTP headers .If user disables this cookies or user’s browser won’t support the interaction between JS and PHP fails.So we must ensure user must enable these Cookies.
Checking with Javascript
if (navigator.cookieEnabled == 0) {
alert("You need to enable cookies for this site to load properly!");
}



Checking with PHP
If user disables Javascript we fail to check the Cookie enabled or not.So we check on the server side also.below PHP snippet check the Cookie enabled or not.
function check_cookie(){
if (setcookie("test", "test", time() + 360))
{
if (isset ($_COOKIE['test']))
{
return true;
}
}
return false;
}

Comments

Alok said…
Good work...thanks
Hi,

I removed the '@' sign and my php code is working.
Pritesh Loke said…
hmm but i am telling this is not right way to check cookie is working or not.

You cookie check should be on next request not current request.

Popular posts from this blog

cookie and subdomains

Output buffering in php