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;
}

Share and Enjoy

  • Facebook
  • Twitter
  • Google Plus
  • Live
  • Delicious
  • Digg
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • Instapaper
  • FriendFeed

Comments

  1. Hay,

    $_COOKIE is just global variable for cookie.

    When use setcookie it will set the response header for the current request.

    so the next request you get correct information about the cookie.

    remove the @ sigh and test again. your PHP code will not work.

  2. 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.

  3. Hi there, You’ve done a great job. I?ll certainly digg it and personally suggest to my friends. I’m confident they will be benefited from this website.

  4. Thank you for another informative website. Where else may just I am getting that kind of info written in such a perfect manner? I’ve a project that I’m just now operating on, and I’ve been on the look out for such information.