views:

387

answers:

4

Is developing a website for a cellphone a totally different world?

How would I detect whether a page is visited from computer or from a cellphone?

I ask this because I see code like below:

if (isset($_SERVER['HTTP_ACCEPT']) &&
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==FALSE)
&& (strpos($_SERVER['HTTP_ACCEPT'],'text ml') === FALSE 
||
(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml') <
strpos($_SERVER['HTTP_ACCEPT'],'text ml'))

)) { //cellphone

   readfile('index.wml');

} else readfile('index.htm');

How do I port the code into C#?

+3  A: 

In php you would typically check the $_SERVER['HTTP_USER_AGENT'] header in order to identify the web browser from where a web request originates.

Developing a web site for a mobile browser is not a totally different world. However you have to keep in mind the following constraints:

  • Screen Size: Not only your screen real-estate is smaller, but sizes and orientations vary a lot between different mobile devices.

  • Flash Support: The majority of mobile browsers do not support flash.

  • JavaScript Support: JavaScript is much more supported than Flash on mobile browsers, especially in modern mobile phones and PDAs.

  • Rendering Performance: Complex pages take longer to get rendered in mobile browsers. In general if you decided to use JavaScript, manipulation of the DOM through JavaScript should be minimal.

  • Mobile Bandwidth: Remember to keep images compressed as much as possible, and to minify all the HTML, CSS and JavaScript.

Daniel Vassallo
But the code I provided doesn't check `$_SERVER['HTTP_USER_AGENT']`
Yes, there are other methods. You may want to check out the example in this article: http://mobiforge.com/developing/story/lightweight-device-detection-php
Daniel Vassallo
The link doesn't have enough explanations...
The code in that article is quite straightforward. It checks for all known HTTP headers that original from mobile devices. Then the snippet ends with `if($mobile_browser>0) {} else {}`. If `$mobile_browser` is greater than 0, your request originates from a mobile device.
Daniel Vassallo
They deal with `$_SERVER['HTTP_ACCEPT']` differently.Is my code above wrong?
It is not wrong, but it might will detect all mobile phone browsers.
Daniel Vassallo
A: 

You can either parse the user agent string (which is easily faked) or use media queries that check for things like a ridiculously small maximum viewport size.

Azeem.Butt
+2  A: 

To detect a cell phone and to find out it's capabilities you can use the WURFL library.

Tomas Markauskas
A: 

You are going to want to take a look at this MSDN article on Getting Mobile: Using WML and WAP to Display Web Sites on Mobile Devices. By designing WML, the mobile phone knows to use the low-res version.

This page here on Detecting Mobile Devices using ASP.NET and C# shows you how to do it with classes ported from PHP. The API in that link can detect iPhones, Androids, Blackberries, Symbion, etc.

WML decks are stored on an ordinary web server trivially configured to serve the text/vnd.wap.wml MIME type in addition to plain HTML and variants. The WML cards when requested by a device are accessed by a bridge WAP gateway, which sits between mobile devices and the World Wide Web, passing pages from one to the other much like a proxy. The gateways send the WML pages on in a form suitable for mobile device reception (WAP Binary XML). This process is hidden from the phone, so it may access the page in the same way as a browser accesses HTML, using a URL (for example, http://example.com/foo.wml). (Provided the mobile phone operator has not specifically locked the phone to prevent access of user-specified URLs.)

Wikipedia Source

0A0D
Wow - provide a direct link on how to do this in C# and downvoted.. someone must be jealous of my answer :)
0A0D
Roberto, there was probably some confusion around this question. The question was initially formulated to request a php solution, but 3 revision and 22 minutes later, the last sentence appeared along with a c# tag... even though the second sentence was still asking for a php method!
Daniel Vassallo