Login

Browser Verification

Author:
zeeg
Posted:
March 2, 2007
Language:
Python
Version:
Pre .96
Tags:
middleware browsers validation
Score:
1 (after 1 ratings)

A page we used on Curse to stop users who are new, who are using old browsers (ones who usually have extreme issues with CSS handling) and recommend they update.

Can easily be modified to suit your needs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from django.http import HttpResponse, QueryDict
from django.conf import settings
from cursesite.functions.functions import render_template

ALLOWED_BROWSERS = (
	('Firefox', 1.5),
	('Opera', 9),
	('MSIE', 7),
	('Safari', 85),
)

def check_browser(agent):
	
	for b in ALLOWED_BROWSERS:
		loc = agent.find(b[0])
		if loc != -1:
			start = loc+len(b[0])+1
			end = agent.find(' ', start)
			if end != -1:
				version = agent[start:end]
			else:
				version = agent[start:]
			if version == '':
				version = '2.0'
			elif version[-1] == ';':
				version = version[0:-1]
			version = version.split('.')
			version = "%s.%s" % (version[0].split(')')[0], ''.join(version[1:]).split(',')[0].split('+')[0].split('u')[0].split('a')[0])
			if float(version) < float(b[1]):
				valid = False
			else:
				valid = True
			ec = {'browser_version': version, 'browser': b[0], 'valid': valid}
			return ec

	return {'browser': agent, 'browser_version': '', 'valid': True}

class ValidateBrowser(object):
	def process_response(self, request, response):
		if '_verify_browser' in request.COOKIES or request.META['HTTP_USER_AGENT'] == 'Python-urllib/1.16' or request.GET.get('skipcheck', False) or request.META['HTTP_USER_AGENT'].find('Googlebot') != -1:
			return response
			
		agent = request.META['HTTP_USER_AGENT']
		ec = check_browser(agent)	
		if '_verified' in request.GET:
			ec.update({'no_cookies': True})
			
		# Redirect URL
		if 'PATH_INFO' in request.META:
			url = request.META['PATH_INFO']
		elif 'REQUEST_URI' in request.META:
			url = request.META['REQUEST_URI']
		else:
			url = '/'
		q = QueryDict(request.META['QUERY_STRING'])
		if '_verified' not in q:
			q = q.copy() # to make it mutable
			q.update({'_verified': '1'})
		url += "?%s" % q.urlencode()
		
		ec.update({'url': url})
		
		response = render_template(request, 'browsercheck.html', ec)
		response.set_cookie('_verify_browser', '1', max_age=60*60*24*30, domain=settings.SESSION_COOKIE_DOMAIN)
		return response

More like this

  1. Clear session data on login and logout by jb0t 7 years, 2 months ago
  2. Text replacement with alternative font faces by scsiboy 6 years ago
  3. Cheetah-style comments by olivier 7 years, 11 months ago
  4. Mobile browser detection middleware by pavl 5 years, 2 months ago
  5. Django Number Input by silent1mezzo 4 years, 4 months ago

Comments

Please login first before commenting.