How would I identify if a website originates from a mobile browser?

帅比萌擦擦* 提交于 2019-12-20 03:43:12

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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




回答5:


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"]


来源:https://stackoverflow.com/questions/1983756/how-would-i-identify-if-a-website-originates-from-a-mobile-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!