Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmmdq committed Jun 12, 2021
0 parents commit e0be9f9
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/*
vendor/*
index.php
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 mhmmdq

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Php package detects user browser details
This package extracts `$_SERVER['HTTP_USER_AGENT']` details of the client browser and provides the details to you as an array.
## Installation
Installed by [Composer](https://getcomposer.org/ "Composer")
```bash
$ composer require mhmmdq\useragent
```
## How to use
To use the package, first add the Autoloder Composer file to the software and then call the class
```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Mhmmdq\Useragent\UserAgent;

$useragent = UserAgent::analyze();

var_dump($useragent);
```
The output provides you with user information that has the following information

`useragent` useragent received from the client browser

`browser` An array containing the name and version of the client browser

`platform` Client platform

`language` Client browser language derived from `$_SERVER['HTTP_ACCEPT_LANGUAGE']` analysis

`is_mobile` Check whether the client is mobile or not (The output of this value is **true** or **false**)
##### If you need to check different user agents, you can pass them to the analysis method
```php
$useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36';
var_dump(UserAgent::analyze($useragent));
```
### Check if the client is mobile or not
You can use the following method to do this
```php
UserAgent::is_mobile();
```
##### Example
```php
if(UserAgent::is_mobile()) {
header('location: https://m.example.com');
}
```
### Get the default browser language
The default language of the client browser is received using `$_SERVER['HTTP_ACCEPT_LANGUAGE']` and after checking the output is provided as a string
```php
UserAgent::lang();
```


------------


- [List of browsers recognizable by this class](./browsers.md "List of browsers recognizable by this class")
- [List of recognizable operating systems](./os.md "List of recognizable operating systems")
- [List of recognizable platforms](./platform.md "List of recognizable platforms")
50 changes: 50 additions & 0 deletions browsers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# List of recognizable browsers
| id | name |
| :------------: | :------------: |
| 1 | Safari |
| 2 | Mozilla |
| 3 | Google Chrome |
| 4 | Firefox |
| 5 | Vivaldi |
| 6 | Microsoft edge |
| 7 | Internet Explorer 11 |
| 8 | Beamrise |
| 9 | Opera |
| 10 | Shiira |
| 11 | Chimera |
| 12 | Phoenix |
| 13 | Firebird |
| 14 | Camino |
| 15 | Netscape |
| 16 | OmniWeb |
| 17 | Konqueror |
| 18 | iCab |
| 19 | Lynx |
| 20 | Links |
| 21 | HotJava |
| 22 | Amaya |
| 23 | IBrowse |
| 24 | iTunes |
| 25 | Silk |
| 26 | Dillo |
| 27 | Maxthon |
| 28 | Arora |
| 29 | Galeon |
| 30 | Iceape |
| 31 | Iceweasel |
| 32 | Midori |
| 33 | QupZilla |
| 34 | Namoroka |
| 35 | NetSurf |
| 36 | BOLT |
| 37 | EudoraWeb |
| 38 | ShadowFox |
| 39 | Swiftfox |
| 40 | Uzbl |
| 41 | UCBrowser |
| 42 | Kindle |
| 43 | wOSBrowser |
| 44 | Epiphany |
| 45 | SeaMonkey |
| 46 | Avant Browser |
| 47 | Internet Explorer |
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "mhmmdq/useragent",
"description": "A small package to identify the user's browser",
"keywords": ["useragent", "Browser Identification" ],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "mhmmdqasemi",
"email": "[email protected]"
}
],
"autoload": {
"psr-4":{
"Mhmmdq\\": "src/mhmmdq"
}
},
"require": {
"php": ">=5.5"
}
}
20 changes: 20 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions os.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# List of recognizable operating systems
| id | name |
| :------------: | :------------: |
| 1 | Windows 10 |
| 2 | Windows 8.1 |
| 3 | Windows 8 |
| 4 | Windows 7 |
| 5 | Windows Vista |
| 6 | Windows Server 2003/XP x64 |
| 7 | Windows XP |
| 8 | Windows XP |
| 9 | Windows 2000 |
| 10 | Windows ME |
| 11 | Windows 98 |
| 12 | Windows 95 |
| 13 | Windows 3.11 |
| 14 | Mac OS X |
| 15 | Mac OS 9 |
| 16 | Linux |
| 17 | Ubuntu |
| 18 | iOS |
| 19 | iPod |
| 20 | iPadOs |
| 21 | Android |
| 22 | BlackBerry |
| 23 | Mobile |
25 changes: 25 additions & 0 deletions platform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# List of recognizable platforms
| id | name |
| :------------: | :------------: |
| 1 | Windows |
| 2 | iPad |
| 3 | iPod |
| 4 | iPhone |
| 5 | Apple |
| 6 | Android |
| 7 | Linux |
| 8 | Nokia |
| 9 | BlackBerry |
| 10 | FreeBSD |
| 11 | OpenBSD |
| 12 | NetBSD |
| 13 | UNIX |
| 14 | DragonFlyBSD |
| 15 | OpenSolaris |
| 16 | SunOS |
| 17 | OS/2 |
| 18 | BeOS |
| 19 | Windows |
| 20 | Linux |
| 21 | PalmOS |
| 22 | RebelMouse |
110 changes: 110 additions & 0 deletions src/mhmmdq/Useragent/DetectBrowser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Mhmmdq\Useragent;

class DetectBrowser{

/**
* @var mixed
*/
private static $userAgent;

/**
* @var string[]
*/
private static $browsers = array (
'Vivaldi'=>'Vivaldi',
'Edg' => 'Microsoft edge',
'Trident\/7.0' => 'Internet Explorer 11',
'Beamrise' => 'Beamrise',
'Opera' => 'Opera',
'OPR' => 'Opera',
'Shiira' => 'Shiira',
'Chimera' => 'Chimera',
'Phoenix' => 'Phoenix',
'Firebird' => 'Firebird',
'Camino' => 'Camino',
'Netscape' => 'Netscape',
'OmniWeb' => 'OmniWeb',
'Konqueror' => 'Konqueror',
'icab' => 'iCab',
'Lynx' => 'Lynx',
'Links' => 'Links',
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
'iTunes' => 'iTunes',
'Silk' => 'Silk',
'Dillo' => 'Dillo',
'Maxthon' => 'Maxthon',
'Arora' => 'Arora',
'Galeon' => 'Galeon',
'Iceape' => 'Iceape',
'Iceweasel' => 'Iceweasel',
'Midori' => 'Midori',
'QupZilla' => 'QupZilla',
'Namoroka' => 'Namoroka',
'NetSurf' => 'NetSurf',
'BOLT' => 'BOLT',
'EudoraWeb' => 'EudoraWeb',
'shadowfox' => 'ShadowFox',
'Swiftfox' => 'Swiftfox',
'Uzbl' => 'Uzbl',
'UCBrowser' => 'UCBrowser',
'Kindle' => 'Kindle',
'wOSBrowser' => 'wOSBrowser',
'Epiphany' => 'Epiphany',
'SeaMonkey' => 'SeaMonkey',
'Avant Browser' => 'Avant Browser',
'Firefox' => 'Firefox',
'Chrome' => 'Google Chrome',
'MSIE' => 'Internet Explorer',
'Internet Explorer' => 'Internet Explorer',
'Safari' => 'Safari',
'Mozilla' => 'Mozilla',
);

/**
* DetectBrowser constructor.
* @param null $useragent
*/
public function __construct($useragent = null)
{
self::$userAgent = !empty($useragent) ? $useragent : $_SERVER['HTTP_USER_AGENT'];
}

/**
* @return array
*/
public static function analyze() {
foreach(self::$browsers as $pattern => $name) {
if( preg_match("/".$pattern."/i",self::$userAgent, $match)) {
$browserName = $name;

$known = array('Version', $pattern, 'other');
$pattern_version = '#(?<browser>' . join('|', $known).')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern_version, self::$userAgent, $matches)) {
$browserVersion = 'Version not detected';
}

$i = count($matches['browser']);
if ($i != 1) {


if (strripos(self::$userAgent,"Version") < strripos(self::$userAgent,$pattern)){
@$browserVersion = $matches['version'][0];
}
else {
@$browserVersion = $matches['version'][1];
}
}
else {
$browserVersion = $matches['version'][0];
}
break;
}
}
return ['name'=>$browserName , 'version'=>$browserVersion];
}

}
Loading

0 comments on commit e0be9f9

Please sign in to comment.