ornicar/php-user-agent 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ornicar/php-user-agent

最新稳定版本:1.0.0

Composer 安装命令:

composer require ornicar/php-user-agent

包简介

无描述信息

关键字:

README 文档

README

Browser detection in PHP5. Uses a simple and fast algorithm to recognize major browsers.

Overview

$userAgent = new phpUserAgent(); $userAgent->getBrowserName() // firefox $userAgent->getBrowserVersion() // 3.6 $userAgent->getOperatingSystem() // linux $userAgent->getEngine() // gecko

Why you should use it

PHP provides a native function to detect user browser: get_browser(). get_browser() requires the "browscap.ini" file which is 300KB+. Loading and processing this file impact script performance. And sometimes, the production server just doesn't provide browscap.ini.

Although get_browser() surely provides excellent detection results, in most cases a much simpler method can be just as effective. php-user-agent has the advantage of being compact and easy to extend. It is performant as well, since it doesn't do any iteration or recursion.

Usage

// include classes or rely on Composer autoloader require_once '/path/to/php-user-agent/phpUserAgent.php'; require_once '/path/to/php-user-agent/phpUserAgentStringParser.php'; // Create a user agent $userAgent = new phpUserAgent(); // Interrogate the user agent $userAgent->getBrowserName() // firefox $userAgent->getBrowserVersion() // 3.6 $userAgent->getOperatingSystem() // linux $userAgent->getEngine() // gecko

Advanced

Custom user agent string

When you create a phpUserAgent object, the current user agent string is used. You can specify another user agent string:

// use another user agent string $userAgent = new phpUserAgent('msnbot/2.0b (+http://search.msn.com/msnbot.htm)'); $userAgent->getBrowserName() // msnbot // use current user agent string $userAgent = new phpUserAgent($_SERVER['HTTP_USER_AGENT'); // this is equivalent to: $userAgent = new phpUserAgent();

Custom parser class

By default, phpUserAgentStringParser is used to analyse the user agent string. You can replace the parser instance and customize it to match your needs:

// create a custom user agent string parser class myUserAgentStringParser extends phpUserAgentStringParser { // override methods } // inject the custom parser when creating a user agent: $userAgent = new phpUserAgent(null, new myUserAgentStringParser());

Run tests

You can run the unit tests on your server:

$ php prove.php

Contribute

If you found a browser of operating system this library fails to recognize, feel free to submit an issue. Please provide the user agent string. And well, if you also want to provide the patch, it's even better.

统计信息

  • 总下载量: 302.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 151
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 150
  • Watchers: 10
  • Forks: 31
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04