Monday, April 2, 2012

[PHP][Javascript] Detect if Cookies are Enabled or Disabled

When we create a website, there are some applications that needed for the cookies to be enabled. If the cookies are disabled then the user's must be inform that their cookies must be enabled. And to inform the users, we need to detect if the cookies are enabled or not.

Here's the code in PHP:
$cookies_stat = false;
if(setcookie("test", "test", time() + 360))
{
  if(isset($_COOKIE['test']))
  {
    $cookie_stat = true;
  }
}
The general idea of the PHP code is first we create a cookie then we check if the cookie have the values that has been set on it. If the values are set then the cookies in the user's browser was enabled.

Here's the code in Javascript:
var cookies_stat = false;
if (navigator.cookieEnabled == 0) {
  cookies_stat = true;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...