views:

198

answers:

5

Hi,

I'm writing a web application and I'd like to work out what type of browser/OS the request is coming from, and customise the returned content accordingly. So if someone visits the site from an iPhone/Android, they get a more streamlined experience, or if it's a desktop, they get the full version. I will pretty much take a completely different path, rather than try to mix the content together.

What is the recommended approach for this in ASP.NET/IIS and PHP? Is there a single place I can catch incoming HTTP requests, make a decision, then redirect? Or is this usually done on a page by page case? Any gotchas I should look out for?

Edit: A good point was made to make sure there is a link to the full version on the reduced version. That's a good point, but raises the problem that once the user make this choice, all future redirections now have to point to the full version. I'd really rather be doing all of this in one place.

Cheers,

Shane

A: 

You should probably look at Conditional Comments:

http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx

IrishChieftain
Conditional comments are only supported in Internet explorer, and are pretty useless for anything other than providing IE-only material/hacks. I don't think they're suitable for this.
McPherrinM
@WaffleMatt and what would you suggest?
IrishChieftain
+1  A: 

Not a direct answer but it's worth checking out CSS media types. You can specify the handheld type to streamline the page for phones and other small screened devices.

http://www.w3.org/TR/CSS21/media.html

Dan Midwood
+1  A: 

You could take a look at the UserAgent header in the HTTP request and redirect accordingly.

In PHP that would be $_SERVER['HTTP_USER_AGENT'].

You should however watch out that you don't write a lot of duplicate code when doing this.

Peter Stuifzand
A: 

For ASP.NET applications you can check out the Global.asax file and Session_BeginRequest event.

azamsharp
+1  A: 

ASP.NET has a built-in browser detection mechanism. It's driven by a fully extensible collection of XML files (*.browser) that contain regular expressions for matching the incoming User-Agent string and associated properties for the matched agents.

You can access the properties from the Request.Browser object; you can also tag control properties based on browser specifics.

There's a bunch of info on the Web about this -- I also cover it in detail in my book: Ultra-Fast ASP.NET.

RickNZ
Interesting. This sound like an automated way of redirecting, but how would you handle the situation where a user has elected to view the full version of the page? Now the requests aren't redirected based on the browser type, but rather the session.
Shane
In .NET 3.5, just have your logic based on the Request.Browser property or whether some session-level flag is set. It's a little easier in .NET 4.0, since they've added the ability to have full programmatic control over how the Browser is determined, rather than having it based on the User-Agent string and regular expressions alone -- so you could add your session check logic there, and then maybe still use declarative properties in your markup.
RickNZ