//initialize all known devices as false $iPod = false; $iPhone = false; $iPad = false; $iOS = false; $webOSPhone = false; $webOSTablet = false; $webOS = false; $BlackBerry9down = false; $BlackBerry10 = false; $RimTablet = false; $BlackBerry = false; $NokiaSymbian = false; $Symbian = false; $AndroidTablet = false; $AndroidPhone = false; $Android = false; $WindowsPhone = false; $WindowsTablet = false; $Windows = false; $Tablet = false; $Phone = false; //Detect special conditions devices & types (tablet/phone form factor) if(stripos($_SERVER['HTTP_USER_AGENT'],"iPod")){ $iPod = true; $Phone = true; $iOS = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")){ $iPhone = true; $Phone = true; $iOS = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){ $iPad = true; $Tablet = true; $iOS = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"webOS")){ $webOS = true; if(stripos($_SERVER['HTTP_USER_AGENT'],"Pre") || stripos($_SERVER['HTTP_USER_AGENT'],"Pixi")){ $webOSPhone = true; $Phone = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"TouchPad")){ $webOSTablet = true; $Tablet = true; } } if(stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry")){ $BlackBerry = true; $BlackBerry9down = true; $Phone = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"BB10")){ $BlackBerry = true; $BlackBerry10 = true; $Phone = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet")){ $BlackBerry = true; $RimTablet = true; $Tablet = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"SymbianOS")){ $Symbian = true; $NokiaSymbian = true; $Phone = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){ $Android = true; if(stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){ $AndroidPhone = true; $Phone = true; }else{ $AndroidTablet = true; $Tablet = true; } } if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows")){ $Windows = true; if(stripos($_SERVER['HTTP_USER_AGENT'],"Touch")){ $WindowsTablet = true; $Tablet = true; } if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows Phone")){ $WindowsPhone = true; $Phone = true; } } //Target form factors if( $Phone ){ //we're phone form factor }else if( $Tablet ){ //we're a tablet form factor }else{ //we're neither a phone or tablet } //Target operating systems if( $iOS ){ //we're an iOS device }else if( $Android ){ //we're an Android device }else if( $Windows ){ //we're a Windows device }else if( $BlackBerry ){ //we're a BlackBerry device }else if( $webOS ){ //we're a webOS device }else if( $Symbian ){ //we're a Symbian device }else{ //we're neither a phone or tablet } //Target individual devices if( $iPod || $iPhone ){ //we're an iPhone/iPod touch -- do something here }else if($iPad){ //we're an iPad -- do something here }else if($AndroidPhone){ //we're an Android Phone -- do something here }else if($AndroidTablet){ //we're an Android Tablet -- do something here }else if($WindowsPhone){ //we're an Windows Phone -- do something here }else if($WindowsTablet){ //we're an Windows Tablet -- do something here }else if($webOSPhone){ //we're a webOS phone -- do something here }else if($webOSTablet){ //we're a webOS tablet -- do something here }else if($BlackBerry9down){ //we're an outdated BlackBerry phone -- do something here }else if($BB10){ //we're an new BlackBerry phone -- do something here }else if($RimTablet){ //we're a RIM/BlackBerry Tablet -- do something here }else if($NokiaSymbian){ //we're a Nokia Symbian device -- do something here }else{ //we're not a known device. }
This code is meant to be a starting point. It can be easily implemented as a class for more modular/efficient use or it can be used in it’s current form. I hope it helps you with your project!
Hello,
There is a bug on line 56 and on line 62 where the closing bracket is reversed, and so if you include this code through using require() it gives a fatal error. I just wanted to let you know!
Thank you for the useful script! <3
Sorry it is lines 57 and 62, where the bracket should be closing and not opening.
Thanks for catching this.
thanks the codes worked for me
This is perfect, the only problem I found is with Blackberry (maybe because I tested from BB10) but I didn’t work, I aded a new var for BB10 and fixed it, in case anyone wants it, just use this:
$BlackBerry10 = stripos($_SERVER[‘HTTP_USER_AGENT’],”BB10″);
and use
}else if($BlackBerry10){
//we’re a BlackBerry10 phone — do something here
}else{
//we’re not a mobile device.
}
Forgot to mention at the time, I’ve incorporated BlackBerry 10 into the above example.
Much thanks for posting your framework code for mobile detection! Extremely helpful
Thanks Man.
It working fine.
Much thanks for posting your framework code for mobile detection! Extremely helpful.
It works. But now a question about nokia phones and windows phones. How must i redirect this.
Because i redirect ipad, iphone, android mobile, webos, blackberry en rim tablet.
But i don’t see how i must redirect windows smartphone or other smartphones or tablets that are not in de list.
Can someone please help.
I wanted to add a handy contribution for detecting tablet or mobile device
orientation in order to serve formatted content based on the orientation of the
device in either Portrait or Landscape mode:
/* Specific CSS can be applied to page elements if the user is holding their device in a given orientation. This is the CSS script. */
@media (orientation:portrait)
{
#isPortrait { display: block; }
#isLandscape { display: none; }
}
@media (orientation:landscape)
{
#isPortrait { display: none; }
#isLandscape { display: block; }
}
And these are the blocks you would place in your HTML page:
<div id=”isPortrait”>
//Show the content formatted for Portrait orientation between these Div tags.
</div>
<div id=”isLandscape”>
//Show the content formatted for Landscape orientation between these Div tags.
</div>
What if the switch in the middle of browsing? Does your method still work?
Thanks Anthony
nvm I played with it and it works great. Thanks
Hi Chris,
I’ve stripped the details out of this script and left the focus on BlackBerry devices
only. I will be providing useful examples to other developers at the upcoming BlackBerry
Jam conference on August 23rd and this is a fine example of Agent detection for net
based content developers.
Thank you in advance for the useful code snippet :)
$BlackBerry = stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$RimTablet= stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet");
if($BlackBerry)
{
//we're a BlackBerry phone -- do something here
}
if($RimTablet)
{
//we're a RIM/BlackBerry Tablet -- do something here
}
Thanks for this, it might not work in every single case (Nokia Symbian and Windows Phone are missing, off the top of my head), and I need OS version checking for blackberrys as I plan on supporting the newer ones, but a very lightweight, usable script that gets the job done.
While it may not include support for all user agents out of the box it’s quite easily expanded. I’ll try and add in a few more user agents in the near future. Glad you found it useful.
Will the code above also resize it for the mobile device?
This code simply detects the most common mobile os’s. How you use it is up to you but it does not modify how a page displays. The only OS excluded is Windows Mobile — which I will add shortly.
How can the code be modified to give the user the option to view the desktop version of the site if required from their mobile device?
Also on the conditional code section i.e.
}else if($iPad){
//were an iPad — do something here
How would add a redirect here to a m.mydomain.com address ?
You could simply add }else{ //do something } for a desktop version.
As for a redirect — php’s header function is your friend: header(‘Location: http://www.example.com/‘) — http://php.net/manual/en/function.header.php
Hello, to detect BBerries and playbook table we’d be talking about something like that:
$blackberry = stripos($_SERVER[‘HTTP_USER_AGENT’],”blackberry”);
or is there any other thing required?
Many thanks for your solution!
I’ll look up the BlackBerry and Playbook user agents tomorrow and update the code to add support.
I’ve updated the example above to detect BlackBerry’s and Playbooks. Hope it helps!
Thanks Chris for your contribution, great work!