browscap/browscap-php
browscap-php is a PHP library for detecting browser, platform, and device details from User-Agent strings using the Browscap database. It provides easy updates, caching, and a simple API for accurate capability detection in web apps.
browscap/browscap-php package is ideal for applications requiring browser/device fingerprinting (e.g., analytics, feature detection, user experience optimization, or security policies). It integrates seamlessly with Laravel’s middleware, service providers, or request lifecycle hooks.if ($browser->isMobile()) {
return redirect()->route('mobile.home');
}
.env for security.BrowscapPHP in unit tests (e.g., using browscap/browscap-php's built-in test utilities).browscap/browscap-php's update command).| Risk Area | Mitigation Strategy |
|---|---|
| Data Staleness | Schedule automated DB updates (e.g., cron job) or use hybrid API+DB fallback. |
| Performance Overhead | Cache responses aggressively (e.g., Cache::remember). |
| License Compliance | Verify MIT license aligns with project terms (no restrictions on redistribution). |
| Breaking Changes | Pin package version in composer.json until API stability is confirmed. |
| False Positives | Validate against real user-agent strings in staging before production rollout. |
DetectBrowserMiddleware to inject BrowscapPHP into the request object.
public function handle(Request $request, Closure $next) {
$browser = app(BrowscapPHP::class)->getBrowser($request->userAgent());
$request->merge(['browser' => $browser]);
return $next($request);
}
BrowscapPHP as a singleton with config options.
$this->app->singleton(BrowscapPHP::class, function ($app) {
$browscap = new BrowscapPHP();
$browscap->setCachePath(storage_path('framework/cache/browscap'));
return $browscap;
});
@browser('isMobile')).spatie/laravel-analytics to enrich event data.composer require browscap/browscap-php.Mozilla/5.0 and Safari/605 user-agents).Accept header negotiation).Headless flag) and legacy devices.X-Device-ID).| Step | Dependency | Owner |
|---|---|---|
| 1. Install Package | Composer access | Backend Engineer |
| 2. Configure DB | Browscap DB download | DevOps |
| 3. Build Middleware | Laravel middleware structure | TPM/Backend Engineer |
| 4. Cache Setup | Redis/File cache configured | DevOps |
| 5. Test Suite | Sample user-agents | QA Engineer |
| 6. Rollout | Feature flag (optional) | Product Manager |
php artisan browscap:update.browscap/browscap-php for deprecations (e.g., PHP 8.2+ features).BrowscapPHP::getBrowser() returns null).BrowscapPHP returns stale data.
Fix: Clear cache (php artisan cache:clear) or trigger manual update.isMobile if needed).browscap_ny, browscap_sg).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Browscap DB/API unavailable | Feature degradation | Fallback to default browser profile. |
| Cache corruption | Stale data | Use Redis with persistence. |
| User-agent spoofing | Security/analytics errors | Validate against known patterns. |
| PHP version incompatibility | Integration breaks | Pin to LTS PHP version. |
isMobile used in redirects?").How can I help you explore Laravel packages today?