2

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#?

user198729
  • 61,774
  • 108
  • 250
  • 348
  • title of your question was edited to be incorrect ('php') since someone saw php code; feel free to fix. – philfreo Dec 31 '09 at 03:51
  • I edited the title, but only because the OP asked for a PHP solution in the question. Obviously feel free to rollback if this was not the intent. – Daniel Vassallo Dec 31 '09 at 03:52
  • He wants a C# soln not PHP.. see last sentence –  Dec 31 '09 at 03:55
  • Sorry for that. Now wonder the confusion: I am checking the version history of the question, and that last sentence was added only after the 3rd revision. Before it was PHP, both in the tags, and in the question. The C# stuff appeared 22 minutes later :) – Daniel Vassallo Dec 31 '09 at 04:11

5 Answers5

3

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
  • 337,827
  • 72
  • 505
  • 443
  • But the code I provided doesn't check `$_SERVER['HTTP_USER_AGENT']` – user198729 Dec 31 '09 at 03:24
  • 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 Dec 31 '09 at 03:25
  • 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 Dec 31 '09 at 03:37
  • They deal with `$_SERVER['HTTP_ACCEPT']` differently.Is my code above wrong? – user198729 Dec 31 '09 at 03:42
  • It is not wrong, but it might will detect all mobile phone browsers. – Daniel Vassallo Dec 31 '09 at 03:47
2

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

Tomas Markauskas
  • 11,496
  • 2
  • 33
  • 35
0

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
  • 5,855
  • 1
  • 26
  • 22
0

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

  • Wow - provide a direct link on how to do this in C# and downvoted.. someone must be jealous of my answer :) –  Dec 31 '09 at 03:56
  • 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 Dec 31 '09 at 04:16
0

user198729 is asking how to do this in C#
The $_SERVER in PHP e.g $_SERVER['HTTP_ACCEPT'] is performed by Request.Headers in c#
e.g

Request.Headers["HTTP_ACCEPT"]
rqmedes
  • 540
  • 2
  • 14