Fraz Sundal
Fraz Sundal

Reputation: 10448

How to identify if cookies are disabled

I want to identify on my login page that user browser cookies are disabled, so i can display a message that enable your cookies and then try to login, how to do this? Im using asp.net mvc-2

Upvotes: 3

Views: 2884

Answers (2)

Valyok26
Valyok26

Reputation: 501

You can check if cookies are enabled using JavasScript.
Something like this:

<div id="enableCookiesMessage" style="display:none">
    You should enable your cookies to login
</div>
<script type="text/javascript">
    if (!navigator.cookieEnabled)
        document.getElementById("enableCookiesMessage").style.display = "block";
</script>

Upvotes: 1

Andy Rose
Andy Rose

Reputation: 16984

The only real way to check if cookies are disabled is to try and store a cookie in the users browser and then retrieve it on a subsequent request.
I think in your situation you should set a cookie when they arrive at the login page. When they submit the login page you can then check to see if the cookie exists in the request. If it does then all is well and you can authenticate the submitted details. If it doesn't then you can display your 'cookies required' message.
As it required at least to requests to your site to make this check it means you cannot show a message as soon as they request the login page as you cannot assume a user hasn't come to your site directly via this page.

Upvotes: 3

Related Questions