4

I'm working on a web application that does not work well with Internet Explorer (web socket, json, security issues).

For now, before my application works with IE:

How can I refuse connections coming from Internet Explorer client ?

Thank you

Guillaume Vincent
  • 13,355
  • 13
  • 76
  • 103
  • 1
    Please don't do this. Give them a nice message saying that your app is incompatible and encourage them to use a new browser or install chrome frame. – Nick Tomlin Aug 22 '13 at 20:19
  • possible duplicate of [How to detect Browser type in Django?](http://stackoverflow.com/questions/2669294/how-to-detect-browser-type-in-django) – Maxime Lorant Aug 22 '13 at 20:19
  • 1
    I'd normally agree with Nick, but as it's IE, [just kill their browser](http://www.howtogeek.com/howto/33394/how-to-crash-any-version-of-internet-explorer-with-simple-html/) – Kevin Aug 22 '13 at 20:23
  • `` - a warning would be good, so they can at least see what it looks like. If you want to be blunt, you can do something like `` (your page content) ` <![endif]-->` which should effectively comment out your whole code if the browser is IE. I wouldn't recommend being THAT blunt though. – Ariane Aug 22 '13 at 20:23
  • @NickTomlin Do not you think that the frustration will be greater if a user still try my application? I want to display a message and refuse the connection. Is it really bad? – Guillaume Vincent Aug 22 '13 at 20:27

2 Answers2

4

Create a middleware where you're parsing the request.META['HTTP_USER_AGENT']. If you found that the user use IE, give him a nice message (e.g a notification or a little alert box) to says him that your site isn't optimized for his browser :)

Some code example: Django way

middleware.py (see the doc for more information)

class RequestMiddleware():
    def process_request(self, request):
        if request.META.has_key('HTTP_USER_AGENT'):
            user_agent = request.META['HTTP_USER_AGENT'].lower()
            if 'trident' in user_agent or 'msie' in user_agent:
                 request.is_IE = True
            else:
                 request.is_IE = False

           # or shortest way:
           request.is_IE = ('trident' in user_agent) or ('msie' in user_agent)

your base template:

{% if request.is_IE %}<div class="alert">Watch out! You're using IE, but unfortunately, this website need HTML5 features...</div>{% endif %}

And then add it to your middlewares list.

Optimized: pure HTML way

If you only want to display a message like what I've done, you can use a HTML conditional comment:

<!--[if IE]> <div class="alert">...</div><![endif]-->
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • shouldn't this be `MISE` instead of `Trident` ? – psychok7 May 12 '14 at 09:28
  • @psychok7 Indeed, `MSIE` is more frequent than `Trident` in User Agent strings: http://www.useragentstring.com/pages/Internet%20Explorer/ I'm going to fix it – Maxime Lorant May 12 '14 at 10:08
  • 1
    [According to Microsoft](https://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx): `As of Internet Explorer 10, conditional comments are no longer supported by standards mode. Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser.` – J0ANMM Feb 07 '17 at 14:26
4

There is another way! Just use settings variable DISALLOWED_USER_AGENTS, and make sure that CommonMiddleware is installed on your site.

For example

import re
DISALLOWED_USER_AGENTS = (re.compile(r'msie\s*[2-7]', re.IGNORECASE), )

Cheers!

mnach
  • 520
  • 1
  • 4
  • 9
  • 1
    please put an example of using DISALLOWED_USER_AGENTS – Sheik797 Jul 04 '15 at 06:10
  • is it possible to change default 403 message on that level? – midori Dec 12 '16 at 17:34
  • I guess yes, since Django 1.9 raises a PermissionDenied exception https://docs.djangoproject.com/en/1.10/releases/1.9/#requests-and-responses so, you can change default handler403 https://docs.djangoproject.com/en/1.9/topics/http/views/#customizing-error-views – mnach Dec 15 '16 at 04:48