Mobile-Detect
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
Top Related Projects
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Browser sniffing gone too far — A useragent parser library for PHP
Quick Overview
Mobile-Detect is a lightweight PHP class for detecting mobile devices and browsers. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment, providing an easy way to identify various mobile devices, operating systems, and browsers.
Pros
- Regularly updated with new device and browser signatures
- Lightweight and easy to integrate into existing PHP projects
- Supports a wide range of mobile devices, tablets, and operating systems
- Provides methods for detecting specific device types and features
Cons
- Relies on User-Agent strings, which can be spoofed or modified
- May require frequent updates to stay current with new devices and browsers
- Limited to server-side detection, not suitable for client-side implementations
- Some edge cases or less common devices might not be accurately detected
Code Examples
Example 1: Basic usage
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if ($detect->isMobile()) {
echo 'Mobile device detected';
} elseif ($detect->isTablet()) {
echo 'Tablet device detected';
} else {
echo 'Desktop device detected';
}
Example 2: Checking for specific devices or operating systems
if ($detect->isiOS()) {
echo 'iOS device detected';
} elseif ($detect->isAndroidOS()) {
echo 'Android device detected';
}
if ($detect->isIphone()) {
echo 'iPhone detected';
} elseif ($detect->isSamsung()) {
echo 'Samsung device detected';
}
Example 3: Using version detection
$ios_version = $detect->version('iOS');
if ($ios_version !== false && $ios_version >= 13) {
echo 'iOS 13 or higher detected';
}
$android_version = $detect->version('Android');
if ($android_version !== false && $android_version >= 10) {
echo 'Android 10 or higher detected';
}
Getting Started
-
Install via Composer:
composer require mobiledetect/mobiledetectlib
-
Include the library in your PHP file:
require_once 'vendor/autoload.php'; $detect = new Mobile_Detect;
-
Use the detection methods as needed in your code:
if ($detect->isMobile()) { // Handle mobile device } elseif ($detect->isTablet()) { // Handle tablet device } else { // Handle desktop device }
Competitor Comparisons
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Pros of Device Detector
- More comprehensive device detection, including operating systems and browsers
- Regularly updated database of user agents
- Supports multiple programming languages through ports
Cons of Device Detector
- Larger codebase and database, potentially impacting performance
- More complex setup and integration process
- Steeper learning curve for customization
Code Comparison
Mobile-Detect:
$detect = new Mobile_Detect;
if ($detect->isMobile()) {
echo 'Mobile device detected';
}
Device Detector:
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$dd = new DeviceDetector($userAgent);
$dd->parse();
if ($dd->isSmartphone()) {
echo 'Smartphone detected';
}
Key Differences
- Mobile-Detect focuses primarily on mobile device detection, while Device Detector offers broader device and software identification
- Device Detector provides more detailed information about detected devices and browsers
- Mobile-Detect has a simpler API and is easier to integrate for basic mobile detection needs
- Device Detector is more suitable for advanced use cases requiring detailed device information
Use Case Recommendations
- Choose Mobile-Detect for simple mobile detection in PHP projects with minimal overhead
- Opt for Device Detector when comprehensive device information is needed across multiple platforms
Browser sniffing gone too far — A useragent parser library for PHP
Pros of Parser-PHP
- More comprehensive device detection, including operating systems and browsers
- Regular updates to its device database
- Supports a wider range of devices and platforms
Cons of Parser-PHP
- Larger file size and potentially higher memory usage
- May have a steeper learning curve due to more complex API
- Slower performance compared to Mobile-Detect for simple device checks
Code Comparison
Mobile-Detect:
$detect = new Mobile_Detect;
if ($detect->isMobile()) {
echo 'Mobile device detected';
}
Parser-PHP:
$result = new WhichBrowser\Parser(getallheaders());
if ($result->isType('mobile')) {
echo 'Mobile device detected';
}
Both libraries offer simple ways to detect mobile devices, but Parser-PHP provides more detailed information about the user agent. Mobile-Detect is more lightweight and focused specifically on mobile detection, while Parser-PHP offers broader device and browser information at the cost of increased complexity and resource usage.
The choice between these libraries depends on the specific needs of your project. If you require detailed device information and regular updates, Parser-PHP might be the better choice. However, if you need a simple, lightweight solution for basic mobile detection, Mobile-Detect could be more suitable.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
MobileDetect, PHP mobile detection class
Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
Before you install
There are three versions of MobileDetect.
4.8.x
is the main version that is ALWAYS going to be updated first.
Version | Tests | Namespace | Code | PHP Version | Status |
---|---|---|---|---|---|
2.8.x | \Mobile_Detect | 2.8 | >=5.0,<7.0 | Deprecated | |
3.74.x | Detection\MobileDetect | 3.74 | >=7.4,<8.0 | LTS | |
4.8.x | Detection\MobileDetect | 4.8 | >=8.0 | Current, Recommended |
ð¤ Supporting
If you are using Mobile Detect open-source package in your production apps, in presentation demos, hobby projects, school projects or so, you can sponsor my work by donating a small amount :+1:. I'm currently paying for hosting and spend a lot of my family time to maintain the project and planning the future releases. I would highly appreciate any money donations.
Special thanks to:
- the community :+1: for donations, submitting patches and issues
- the JetBrains team for the open-source licenses for PHPStorm IDE
- Gitbook team for the open-source license for their technical documentation tool.
ð Documentation
The entire documentation is available on Gitbook: https://docs.mobiledetect.net
ð¾ Demo
Point your device to: https://demo.mobiledetect.net
ð Testing
vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-html .coverage
ð¤ Contributing
Please see the Contribute guide for details.
ð Security
If you discover any security related issues, please email serbanghita@gmail.com instead of using the issue tracker.
ð Credits
Top Related Projects
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Browser sniffing gone too far — A useragent parser library for PHP
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot