Coder Social home page Coder Social logo

martinboksa / php-user-agent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ornicar/php-user-agent

0.0 1.0 0.0 211 KB

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

License: MIT License

php-user-agent's Introduction

PHP User Agent

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.

php-user-agent's People

Contributors

ornicar avatar gelolabs avatar dkushnikov avatar cystbear avatar

Watchers

Martin Bokša avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.