Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active April 15, 2016 15:04
Show Gist options
  • Save lenivene/790c874f3ac126ec35779b30652875ec to your computer and use it in GitHub Desktop.
Save lenivene/790c874f3ac126ec35779b30652875ec to your computer and use it in GitHub Desktop.
Test if the current browser runs on a mobile device.
<?php
/**
* Test if the current browser runs on a mobile device.
*/
function is_mobile() {
$argent = $_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : null;
$mobile = false;
if ( empty( $argent ) ) {
$is_mobile = $mobile;
} elseif ( strpos( $argent, 'Android' ) !== false
|| strpos( $argent, 'BlackBerry') !== false
|| strpos( $argent, 'Kindle') !== false
|| strpos( $argent, 'Mobile') !== false
|| strpos( $argent, 'Opera Mini') !== false
|| strpos( $argent, 'Opera Mobi') !== false
|| strpos( $argent, 'Silk/') !== false ) {
$is_mobile = true;
} else {
$is_mobile = $mobile;
}
return $is_mobile;
}
if ( is_mobile() ) {
echo ' Mobile ';
}else{
echo ' Desktop ';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment