I am writing a website with PHP. Since it will need to be accessed by anyone on the network to access the internet I have to create a mobile version. How do I best check if it's a mobile device? I don't want to have a switch statement with 50 devices at the end since I don't only want to support the iPhone.
Is there a PHP class I could use?
For detection based on User-Agent, use WURFL database. At least it's comprehensive and continually updated.
If you target only high-end(ish) phones, then you may not need to detect them at all, just embed appropriate mobile stylesheets.
Traditionally mobile devices have been detected by comparing the HTTP User-Agent header against a list of well known mobile UA strings. A novel approach instead tries to detect the presence of a desktop OS - anything which is found to not be a desktop OS must then be mobile.
This results in far less false positives.
I've written a post with sample code in Python here: http://notnotmobile.appspot.com
Here is a snippet:
You should look at Tera-WURFL, it is a PHP & MySQL-based software package that detects mobile devices and their capabilities. Here is the Tera-WURFL code that you would use to detect if the visiting device is mobile:
What is a mobile device? Weaker CPU? Lower bandwidth? In reality, it has a screen the resolution of which is below 320x240 and color depth is below 24.
You have to use Javascript also. This link will give you an idea: http://www.w3schools.com/js/tryit.asp?filename=tryjs_browsermonitor
And, this link will teach you what is what: http://www.w3schools.com/htmldom/dom_obj_screen.asp
For the redirection part, I used
For more detail of my implmentation, please read my blog at http://haibuihoang.blogspot.com/2012/11/how-to-redirect-mobile-users-to-your.html
If you want adapt the content to any particular device e.g. to resize images to be the width of the device, then you can also use DeviceAtlas. Based on the useragent of the requesting device, it will tell you the size of the screen, along with supported image formats, supported markup types, maximum page size and so on.