Server-Side Browser Detection
10-13-2009, 01:25 PM
|
Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
Essentially, what I'd like to do is detect a user's browser type on the server side (I know how to do it client side using Javascript) because there are some users out there still using IE6 (why? I don't know.)
The reason for such a call to action is to redirect the users still using IE 6 to a page where they can either choose to upgrade their browser or continue using the site with limited design and functionality effort because they choose to stick with a crappy browser.
[ Note: this all stems from the design and CSS using PNG files and a great layout design - that's not compatible with IE 6 (personally, I figured we'd use the tools out there for correcting it, but alas, they are the bosses and I only do what I'm told.)]
Any suggestions, snippets, etc. are welcome.
You may be able to view it from here but it's in development stage and it may or may not be locked out. I will see what I can do about unlocking it for viewers:
http://dev.emspartner.com/index.html
Thanks.
__________________
Need a vacation.
Last edited by mb2000inc; 10-13-2009 at 02:19 PM..
|
|
|
|
10-13-2009, 01:33 PM
|
Re: Server-Side Browser Detection
|
Posts: 22,214
Location: Blackpool. UK
|
Read the ServerVariables collection and extract the UserAgent property
then match part of the value to ID the culprits
|
|
|
|
10-13-2009, 01:36 PM
|
Re: Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
Thanks, I will give that a shot! 
__________________
Need a vacation.
|
|
|
|
10-13-2009, 02:45 PM
|
Re: Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
See if this looks accurate to you. I'm having troubles getting this to function properly (locally... I check it before I load it on the server.)
First I'm checking, on the page load, if there is a cookie that was placed on the machine.... if it's there, then we continue... if not, that means that this is the user's first visit to the site and we check their browser.
If it's IE 6 then we redirect them to the other page.
(I know, I have some useless strings in there, just play along)
Code:
IfNot Request.Cookies("ContinueCookie") IsNothingThen
Response.Cookies("Continue")("continue") = "Ok"
Response.Cookies("Continue").Expires = DateTime.Now.AddDays(1)
Dim UserCookie AsNew HttpCookie("Continue")
UserCookie.Values("Continue") = "Ok"
UserCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(UserCookie)
Else
Dim sAgent, b_IE, b_Vers, b_Mac, b_Nav, b_Other
Dim ie4win, b_win, ie4, ie5, ie6, ie3, ie4mac, ns4
b_IE = False
b_Nav = False
b_Other = False
b_Vers = 0
sAgent = Request.ServerVariables("HTTP_USER_AGENT")
If (InStr(sAgent, "MSIE") > 0) Then
b_IE = True
b_Vers = Mid(sAgent, InStr(sAgent, "MSIE") + 5, 1)
ElseIf (InStr(sAgent, "MSPIE") > 0) Then
b_Vers = Mid(sAgent, InStr(sAgent, "MSPIE") + 9, 1)
Else
b_Vers = Mid(sAgent, 9, 1)
EndIf
If (Not (b_IE)) Then
b_Nav = InStr(sAgent, "Mozilla") > 0 Or InStr(sAgent, "compatible") > 0
EndIf
b_win = (InStr(sAgent, "Win")) > 0 'Windows
b_Mac = (InStr(sAgent, "Mac")) > 0 'Mac
b_Other = Not (b_win Or b_Mac) 'Other
ie6 = b_IE And b_Vers >= 6 ' IE6
If ie6 Then
'display a client side pop-up that Tells them that the version of their browser is not supported.
ClientScript.RegisterStartupScript(Me.GetType(), "Web Page Name", String.Format("alert('It appears that your browser is outdated, please click ok to continue');"), True)
Response.Redirect("ie6Reroute.aspx")
EndIf
Second, the other page says that if they choose to continue then we start a session
Code:
'start the session
Session("ContinueCookie") = "Continue"
Response.Redirect("Default.aspx")
Do you think this should work?
__________________
Need a vacation.
Last edited by mb2000inc; 10-13-2009 at 02:54 PM..
|
|
|
|
10-13-2009, 03:46 PM
|
Re: Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
I think I got it. Thanks for pointing me in the right direction!
Sticking to the same theme as before... first we check if there's a cookie... if it exists, then do nothing. If it does NOT exist, then we have a look at the user's browser... if the browser is IE6 or lower, then we redirect the user to another page:
Code:
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
If Request.Cookies("Continue") IsNothingThen
'cookie is not present
Dim sAgent, b_IE, b_Vers, b_Mac, b_Nav, b_Other
Dim ie4win, b_win, ie4, ie5, ie6, ie3, ie4mac, ns4
b_IE = False
b_Nav = False
b_Other = False
b_Vers = 0
sAgent = Request.ServerVariables("HTTP_USER_AGENT")
If (InStr(sAgent, "MSIE") > 0) Then
b_IE = True
b_Vers = Mid(sAgent, InStr(sAgent, "MSIE") + 5, 1)
ElseIf (InStr(sAgent, "MSPIE") > 0) Then
b_Vers = Mid(sAgent, InStr(sAgent, "MSPIE") + 9, 1)
Else
b_Vers = Mid(sAgent, 9, 1)
EndIf
If (Not (b_IE)) Then
b_Nav = InStr(sAgent, "Mozilla") > 0 Or InStr(sAgent, "compatible") > 0
EndIf
b_win = (InStr(sAgent, "Win")) > 0 'Windows
b_Mac = (InStr(sAgent, "Mac")) > 0 'Mac
b_Other = Not (b_win Or b_Mac) 'Other
ie4 = b_IE And b_Vers >= 4 ' IE4
ie5 = b_IE And b_Vers >= 5 ' IE5
ie6 = b_IE And b_Vers = 6 ' IE6
ie3 = b_IE And b_Vers < 4 ' Everything <IE4 we consider IE3
ie4mac = b_Mac And ie4 ' IE4 Mac
ns4 = b_Vers >= 4 And b_Nav ' NS4
ie4win = b_win And ie4 AndNot b_Mac ' IE4 Windows
If ie6 Then
'display a client side pop-up that Tells them that the version of their browser is not supported.
ClientScript.RegisterStartupScript(Me.GetType(), "Web Page Name", String.Format("alert('It appears that your browser is outdated, please click ok to continue');"), True)
Response.Redirect("ie6Reroute.aspx")
EndIf
Else
'cookie is present
Return
EndIf
EndSub
If the user chooses to continue, a cookie is placed on the user's machine at page load, and then are redirected back to the original page.
Code:
ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
'start the session
Session("ContinueCookie") = "Continue"
Dim UserCookie AsNew HttpCookie("Continue")
UserCookie.Values("Continue") = "Ok"
UserCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(UserCookie)
EndSub
__________________
Need a vacation.
Last edited by mb2000inc; 10-13-2009 at 03:48 PM..
|
|
|
|
10-13-2009, 04:04 PM
|
Re: Server-Side Browser Detection
|
Posts: 22,214
Location: Blackpool. UK
|
possibly a few errors in there I'll have to load it to a server and run it first.
|
|
|
|
10-13-2009, 04:39 PM
|
Re: Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
Cool, let me know.
I'm using this http://tredosoft.com/Multiple_IE to run multiple instances of IE.
the code seems to work, but I'm sure you can test it further and let me know what I may or may not have missed.
If you figure out how to streamline it a little better that would be cool too.
Thanks for your help.
(and that is why you are the master)
__________________
Need a vacation.
|
|
|
|
10-14-2009, 08:28 AM
|
Re: Server-Side Browser Detection
|
Posts: 91
Name: Sidney shieldon
|
yeah Mark has done it superbly.....
|
|
|
|
10-14-2009, 09:56 AM
|
Re: Server-Side Browser Detection
|
Posts: 118
Name: Mark
Location: Ohio
|
Thank you, sir.
I wouldn't have been able to do it without chrishirst - pointing me in the right direction, in the beginning.
__________________
Need a vacation.
|
|
|
|
|
« Reply to Server-Side Browser Detection
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|