Skip to content

Commit f2ff1c2

Browse files
committed
Create Detector.php
1 parent 053b148 commit f2ff1c2

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/Utills/Detector.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Binafy\LaravelUserMonitoring\Utills;
4+
5+
class Detector
6+
{
7+
/**
8+
* Get browser list names.
9+
*
10+
* @var array
11+
*/
12+
public array $browserName = [
13+
'Edge' => 'Edge',
14+
'MSIE' => 'Internet Explorer',
15+
'Trident' => 'Internet Explorer',
16+
'Firefox' => 'Firefox',
17+
'OPR' => 'Opera',
18+
'Chrome' => 'Chrome',
19+
'Safari' => 'Safari',
20+
'Opera' => 'Opera',
21+
];
22+
23+
/**
24+
* Get device list names.
25+
*
26+
* @var array
27+
*/
28+
public array $deviceName = [
29+
'/iPhone/i' => 'iPhone',
30+
'/iPad/i' => 'iPad',
31+
'/Android/i' => 'Android Device',
32+
'/Windows/i' => 'Windows',
33+
];
34+
35+
/**
36+
* Get browser name.
37+
*/
38+
public function getBrowser(): string
39+
{
40+
$userAgent = $_SERVER['HTTP_USER_AGENT'];
41+
42+
foreach ($this->browserName as $key => $browser) {
43+
if (str_contains($userAgent, $key)) {
44+
return $browser;
45+
}
46+
}
47+
48+
return 'Unknown Browser';
49+
}
50+
51+
/**
52+
* Get device name.
53+
*/
54+
public function getDevice(): string
55+
{
56+
$userAgent = $_SERVER['HTTP_USER_AGENT'];
57+
58+
foreach ($this->deviceName as $pattern => $name) {
59+
if (preg_match($pattern, $userAgent)) {
60+
return $name;
61+
}
62+
}
63+
64+
return 'Unknown Device Name';
65+
}
66+
}

0 commit comments

Comments
 (0)