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;
}
About these ads

10 thoughts on “How to check browser cookie enabled with php and javascript

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s