Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 31, 2023 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/ce554f8cdfbaaa8835dc9d063ec45acb to your computer and use it in GitHub Desktop.
Save code-boxx/ce554f8cdfbaaa8835dc9d063ec45acb to your computer and use it in GitHub Desktop.
PHP Detect Mobile Or Desktop

PHP DETECT MOBILE OR DESKTOP

https://code-boxx.com/detect-mobile-desktop-in-php/

NOTES

  1. 1-user-agent.php is a simple "detect mobile".
  2. 2-more.php does a more detailed check.
  3. 3-lib.php uses the Mobile Detect library. Please download the Mobile Detect library from their website, or pull it using Composer with composer require mobiledetect/mobiledetectlib.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) USER AGENT
$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
// (B) MOBILE TABLE DESKTOP
$isMob = is_numeric(strpos($ua, "mobile"));
$isTab = is_numeric(strpos($ua, "tablet"));
$isDesk = !$isMob && !$isTab;
echo $isDesk
? "Desktop device"
: "Mobile device" ;
<?php
// (A) USER AGENT
$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
// (B) DEVICE CHECK
$isIPhone = is_numeric(strpos($ua, "iphone"));
$isIPad = is_numeric(strpos($ua, "ipad"));
$isPixel = is_numeric(strpos($ua, "pixel"));
// (C) OS CHECK
$isWin = is_numeric(strpos($ua, "windows"));
$isMac = is_numeric(strpos($ua, "mac os"));
$isAndroid = is_numeric(strpos($ua, "android"));
$isIOS = $isIPhone || $isIPad;
<?php
// (A) INCLUDE MOBILE DETECT LIBRARY
// DOWNLOAD FROM - http://mobiledetect.net/
require "mobile-detect/Mobile_Detect.php";
$detect = new Mobile_Detect;
// (B) DETECTION ENGINE
if ($detect->isMobile() || $detect->isTablet()) {
echo "MOBILE OR TABLET DEVICE ";
if ($detect->isiOS()) { echo "IOS"; }
if ($detect->isAndroidOS()) { echo "ANDROID"; }
} else { echo "DESKTOP"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment