Skip to content

Instantly share code, notes, and snippets.

@scarvell
Created March 25, 2012 10:20
Show Gist options
  • Save scarvell/2192706 to your computer and use it in GitHub Desktop.
Save scarvell/2192706 to your computer and use it in GitHub Desktop.
Detects if visitor is using a mobile device
<?php
function isMobileBrowser()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobileBrowser = false;
// iPhone check
if( eregi('ipod',$user_agent)|| eregi('iphone',$user_agent) )
{
$mobileBrowser = true;
}
// Black Berry check
if( eregi('blackberry',$user_agent) )
{
$mobileBrowser = true;
}
// End Opera check
if( eregi('opera mini',$user_agent) )
{
$mobileBrowser = true;
}
// Android check
if( eregi('android',$user_agent) )
{
$mobileBrowser = true;
}
// Palm check
if( preg_match('/(palm os|hiptop|avantgo|blazer|xiino|plucker|palm|elaine)/i', $user_agent) )
{
$mobileBrowser = true;
}
// Windows phone check
if( preg_match('/(windows ce; ppc;|windows ce; smartphone;|windows ce; iemobile)/i', $user_agent) )
{
$mobileBrowser = true;
}
return $mobileBrowser;
}
echo ( isMobileBrowser() ) ? 'Mobile user' : 'desktop user';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment