• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 14 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

php-github-api

NOT MAINTAINED - Development moved to github.com/KnpLabs/php-github-api
PHP
322
star
2

lichess-old

DEPRECATED - see http://github.com/ornicar/lila
PHP
252
star
3

dotfiles

My beloved Linux dot files for zsh, tmux, git, tig, vim, mpd, mutt, rtorrent, weechat, xmonad, urxvt, vimperator, ranger...
Shell
192
star
4

vindinium

Artificial Intelligence Challenge - scala game server
JavaScript
169
star
5

php-git-repo

NOT MAINTAINED - Provide an object oriented wrapper to run any Git command. For PHP 5.2 and 5.3.
PHP
164
star
6

lichess-puzzler

Python
99
star
7

ApcBundle

UNMAINTAINED - Use https://github.com/Smart-Core/AcceleratorCacheBundle instead
PHP
92
star
8

scalex

[abandoned] Hoogle-like documentation search engine, for scala
Scala
90
star
9

vindinium-starter-python

Python starter bot for Vindinium
Python
36
star
10

OrnicarAkismetBundle

Akismet.com integration for your Symfony2 application
PHP
30
star
11

php-html-writer

Write HTML tags with CSS expressions
PHP
21
star
12

FOQTyperBundle

Generate PHP classes from YAML templates
PHP
19
star
13

pgn4web

Git mirror of http://code.google.com/p/pgn4web/
PHP
17
star
14

zulip-remind

Zulip bot that posts messages in a stream at a fixed date.
TypeScript
16
star
15

ltc

French website built on Symfony2
PHP
15
star
16

scalariver

scalariform server to format scala faster
HTML
14
star
17

ZendPaginatorAdapter

Provides Zend Paginator Adapters. Currently Doctrine MongoDB and Doctrine ORM pagers are implemented.
PHP
12
star
18

diem-project

Diem Project official website
PHP
12
star
19

vindinium-starter-clojure

Clojure starter bot for Vindinium
Clojure
12
star
20

userstyles

My collection of user styles, since userstyles.org is down
CSS
12
star
21

vindinium-starter-scala

Scala starter bot for Vindinium
Scala
10
star
22

lichess-puzzle-kit

10
star
23

scalex-web

Simple JS client to Scalex JSON API
JavaScript
10
star
24

lichess-layout

CSS
9
star
25

chesshq.com-front

Source code for the frontend of chesshq.com
TypeScript
9
star
26

dmWidgetGalleryPlugin

Adds a Gallery widget to the Add menu
JavaScript
8
star
27

scala-elasticsearch

Elasticsearch client for scala [EDIT] You may want to check that one instead: https://github.com/bsadeh/scalastic
Scala
7
star
28

OrnicarInsaneMarkdownBundle

DEPRECATED - Use PHPSkirt instead https://github.com/chobie/phpskirt
PHP
6
star
29

scala-paginator

Generic paginator for scala
Scala
6
star
30

fide-pgn-viewer

Source code of the page https://worldrapidandblitz.fide.com/open-rapid/, provided by FIDE under the GPL license.
TypeScript
5
star
31

dmContactPlugin

Display a contact form on the site, manage entries on admin
PHP
5
star
32

scala-aichallenge

My take on aichallenge.org in scala
Scala
5
star
33

furet

Music database written in scala
Scala
5
star
34

backdoor

HTTP command line backdoor using scala/play-2.1
Scala
5
star
35

passport-lichess

GitHub authentication strategy for Lichess and Node.js.
JavaScript
5
star
36

dmChessPlugin

Multiplayer and artificial intelligence Chess game
PHP
5
star
37

lichess-game-migration

Scala
4
star
38

clojure-kit

Clojure kit for prismic.io
Clojure
4
star
39

chessify-frontend

Source code of the chessify.me frontend
JavaScript
4
star
40

muteZatShit

Let's kick web radio ads in the teeth
JavaScript
4
star
41

dmFlowPlayerPlugin

Allows to read video, sound and flash medias
JavaScript
4
star
42

dmSqlBackupPlugin

Creates SQL backups of your project
PHP
4
star
43

vim-php-debugger

Vim & xdebug debugger proted from http://www.vim.org/scripts/script.php?script_id=1929
Python
4
star
44

dmCkEditorPlugin

Adds a CkEditor WYSIWYG to your site
JavaScript
4
star
45

diem-talk

Website using dmTalkPlugin to provide a web chat
PHP
4
star
46

nixos-config

Just my personal Nix config
Nix
4
star
47

haichess.com

Mirror of the haichess.com source code. I do not maintain this repo. It's a copy of https://gitee.com/baidaye/lila
Scala
3
star
48

pgn-recorder

Fetches and stores PGN from a URL every second.
JavaScript
3
star
49

lantenora

Photo gallery made with Diem 5.1
PHP
3
star
50

jdubext

Wrapper and extensions for jdub
Scala
3
star
51

dmCommentPlugin

Allows to add comments to your records.
PHP
3
star
52

wolfie

Vindinium Scala AI
Scala
3
star
53

app.chesslang.com

All source code courtesy of chesslang.com
TypeScript
3
star
54

dmWidgetTwitterPlugin

Display tweets in a front widget
PHP
3
star
55

stream-voice

My voice on https://www.twitch.tv/ornicar2/
JavaScript
3
star
56

zulip-lichess

Multi-purpose Zulip bot for the Lichess workspace
TypeScript
3
star
57

worldchess-frontend

Front-end source code for https://worldchess.com and https://arena.myfide.net/, under the GPL-v3 license. Contact [email protected] to get the latest version of this source code.
TypeScript
3
star
58

lichess-mongo-import

import tournaments and stuff from prod mongodb
TypeScript
2
star
59

dmGoogleMapPlugin

Easily display configurable google maps
PHP
2
star
60

dmGithubPlugin

Provides php-github-api and some front widgets
PHP
2
star
61

sf2playground

A dumb Symfony2 project configured with mongodb+mysql to try out bundles
PHP
2
star
62

dmTalkPlugin

HTML+CSS+JS chat without registration
PHP
2
star
63

nestor

Hostel management webapp using event sourcing
Scala
2
star
64

mongoPHP

mongoPHP is a simple PHP based MongoDB administration tool based on the GAS Framework. It is completely open source and maintained by Simon Fletcher (http://twitter.com/simonify).
PHP
2
star
65

wwc

World War Chess
JavaScript
2
star
66

conx

scala learning project: cli connectX game with decent AI
Scala
2
star
67

dmBotPlugin

Browses your website, detects errors and preloads the cache
PHP
2
star
68

ape-petition

A complex web application based on Diem 5.1. Buzz & social petitions for a french environmental association.
PHP
2
star
69

ornicar.github.com

HTML
2
star
70

sltc

JavaScript
2
star
71

muteZatShit-old

Ad blocker for web radios - take that, ads!
JavaScript
2
star
72

vim-mru

Plugin to manage Most Recently Used (MRU) files
Vim Script
2
star
73

lichess-gmail

Extra gmail features to better help lichess users
JavaScript
2
star
74

github-actions-graph

TypeScript
2
star
75

vim-dirdo

[Git mirror] Performs Vim commands over files recursively under multiple directories.
Vim Script
2
star
76

http-pgn-split

Split big PGN sources into multiple URLs, for use with lichess broadcasts
JavaScript
2
star
77

dmWidgetFeedReaderPlugin

Displays RSS and Atom feeds on your site
PHP
2
star
78

lichess-crowdin

OCaml
1
star
79

dmWidgetExternalVideoPlugin

Provides widgets to embed videos from youtube, dailymotion and vimeo
1
star
80

clojure-starter

Clojure starter kit for prismic.io
Clojure
1
star
81

lichess-search-import

Scala
1
star
82

zen-catacomb

Connected foosball cave
Scala
1
star
83

clife-step1

Clojure
1
star
84

play-cdi-leak

Demo classloader leak in playframework dev run
Scala
1
star
85

munin-lichess

PHP
1
star
86

dmOppPlugin

Observatoire Photographique du Paysage
PHP
1
star
87

lichess-data

1
star
88

play-run-mem

Scala
1
star
89

s2test

Scala
1
star
90

mixit-lichess

JavaScript
1
star
91

lila-jmh-benchmarks

Java
1
star
92

tsp

Yet Another TSP Solution
C++
1
star
93

kcacup

Scala
1
star
94

dmCoreTranslatorPlugin

Helps writing Diem translations
PHP
1
star
95

hacksquare

HTML
1
star
96

sf2minimal

Minimal sf2 project. Trying to determine whether or not SF2 is testable under Windows
PHP
1
star
97

haskant

Ants artificial intelligence written in Haskell
Haskell
1
star
98

idbase

Playframework / MongoDB advanced search
Scala
1
star
99

scalabug

scalabug
Scala
1
star
100

praxeum

Scala
1
star