PHP

DETECT MOBILE OR DESKTOP IN PHP

(quick guide & examples)

THE USER AGENT $ua = strtolower ($_SERVER["HTTP_USER_AGENT"]);

BASIC USER AGENT CHECK

01

MOBILE TABLET DESKTOP $isMob = is_numeric(strpos($ua, "mobile")); $isTab = is_numeric(strpos($ua, "tablet")); $isDesk = !$isMob && !$isTab;

OS CHECKS $isWin = is_numeric(strpos($ua, "windows")); $isMac = is_numeric(strpos($ua, "mac os")); $isAndroid = is_numeric(strpos($ua, "android")); $isIOS = $isIPhone || $isIPad;

MORE PLATFORM CHECKS

02

DEVICE CHECK $isIPhone = is_numeric(strpos($ua, "iphone")); $isIPad = is_numeric(strpos($ua, "ipad")); $isPixel = is_numeric(strpos($ua, "pixel"));

USE DETECTION LIBRARY

03

LOAD MOBILE DETECT LIBRARY require "mobile-detect/Mobile_Detect.php"; $md = new Mobile_Detect;

MOBILE OR DESKTOP if ($md->isMobile() || $md->isTablet()) {   echo "MOBILE OR TABLET DEVICE ";   if ($md->isiOS()) { echo "IOS"; }   if ($md->isAndroidOS()) { echo "AND"; } } else { echo "DESKTOP"; }