21

I'm building a laravel application where I want to keep track of client browser details such as browser name. How do I do it using Laravel ?

public function postUser(Request $request)
    {        
            $user = new User();                                 
            $user->name = $request->Input(['name']);           
            $device=   $request->header('User-Agent');
            dd($device);
            $user->save();            
            return redirect('userSavePage');          
    }

I have used this $device= $request->header('User-Agent'); But while I dd() the output I get something Like this:

"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"

How do I get actual browser details?

Hola
  • 2,163
  • 8
  • 39
  • 87

6 Answers6

37

I ended using the faster, and simpler way:

$request->header('User-Agent');

Hope it helps someone!

dahngeek
  • 554
  • 1
  • 4
  • 3
  • It do not detect exactly which browser . – Cong LB Jan 18 '19 at 02:07
  • @CongLB Yep, it only gets the User-Agent which is de identifier of the browser's technologies and details. Yo can check Panjesh answer to understand the User-Agent. – dahngeek May 27 '19 at 17:09
11

First add the package to your composer:

{
    "require": {
        "hisorange/browser-detect": "2.*" // For laravel 5.* versions
        "hisorange/browser-detect": "1.*" // For laravel 4.* versions
    }
}

After the composer update/install add the service provider to your app.php:

'providers' => array(
    // ...
    'hisorange\BrowserDetect\Provider\BrowserDetectService',
    // ...
)

Add the alias to the aliases in your app.php:

'aliases' => array(
    // ...
    'BrowserDetect' => 'hisorange\BrowserDetect\Facade\Parser',
)

You must use personal configurations, just publish the package's configuration files, (plugins.php also published with this)

php artisan vendor:publish

You can get result informations by simply call on the facade.

// You can always get the result object from the facade if you wish to operate with it.
BrowserDetect::detect(); // Will resolve and return with the 'browser.result' container.

// Calls are mirrored to the result object for easier use.
BrowserDetect::browserVersion(); // return '3.6' string.

// Supporting human readable formats.
BrowserDetect::browserName(); // return 'Firefox 3.6' string.

// Or can be objective.
BrowserDetect::browserFamily(); // return 'Firefox' string.

For details: https://github.com/hisorange/browser-detect

Javid Aliyev
  • 436
  • 4
  • 11
  • it return "HiSoRange Generic OS" on osFamily. – Hendrik Eka Aug 10 '17 at 06:46
  • @JavidAliyev hi i checked the package its great but i want to ask how to get browser unique fingureprint or signature, is that even possible,?Because i was wondering push notification such as one signal get track of some signature so is there anyway of doing so ? – user7747472 Jun 06 '18 at 15:29
  • 1
    Please, see this link: https://stackoverflow.com/questions/4085230/ways-to-create-a-unique-user-fingerprint-in-php – Javid Aliyev Jun 07 '18 at 10:54
8

For most of newest laravel versions, this method is working :

Route::get('/agent', function () {
    return request()->userAgent();
});
sidikfaha
  • 139
  • 3
  • 12
3

Use Agent

which let you detect user browser and platform and also browser's/platform's version

https://medium.com/@panjeh/laravel-detector-mobile-browser-name-version-platform-device-robot-crawler-user-language-8499bee7607c

In the new version you can also detect the robots.

panjeh
  • 1,310
  • 1
  • 14
  • 11
3

With latest version of BrowserDetect package, you can get browser information as below:

Install using below command:

composer require hisorange/browser-detect

Once installed, add dependency in config/app.php file.

Add below line in providers array.

hisorange\BrowserDetect\ServiceProvider::class,

Add below line in alias array.

'BrowserDetect' => hisorange\BrowserDetect\Facade::class,

Include package in any class file as below to use:

use BrowserDetect;

After this you can access BrowserDetect methods as explained in below package link:

BrowserDetect package link

Khushbu
  • 655
  • 7
  • 17
2

In addition to the explanations from here, in order to detect the Rest API consumers like Postman and Insomnia, i merged with this answer and ended up having the following source code that performed better in my scenario

Route::get('browser', function () {

//create new agent instance
$agent = new Jenssegers\Agent\Agent();

//check if agent is robot
if ($agent->isRobot()) {
    return $agent->robot();
}
//if agent is not robot then get agent browser and platform like Chrome in Linux
$agent = $agent->browser() . " in " . $agent->platform();
//if agent browser and platform not obtained, then we check the agent technology
if ($agent == ' in ') {
    $agent =  request()->header('User-Agent');
}
return $agent;});

So from the code above, i can detect browser, platform, robot and rest consumers like Postman and Insomnia.

Angelwise
  • 101
  • 1
  • 5