PHP Laravel:如何获取客户端浏览器/设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37293444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:09:10  来源:igfitidea点击:

PHP Laravel : How to get client browser/ Device?

phplaravellaravel-5

提问by Hola

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 ?

我正在构建一个 Laravel 应用程序,我想在其中跟踪客户端浏览器的详细信息,例如浏览器名称。我如何使用 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:

我用过这个 $device= $request->header('User-Agent');但是当我 dd() 输出时,我得到了这样的东西:

"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?

如何获取实际的浏览器详细信息?

采纳答案by Javid Aliyev

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:

在 Composer 更新/安装后,将服务提供者添加到您的 app.php:

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

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

将别名添加到 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)

必须使用个人配置,只发布包的配置文件,(plugins.php也用这个发布)

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

详情:https: //github.com/hisorange/browser-detect

回答by dahngeek

I ended using the faster, and simpler way:

我结束了使用更快、更简单的方法:

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

Hope it helps someone!

希望它可以帮助某人!

回答by panjeh

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

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.

在新版本中,您还可以检测机器人。

回答by Khushbu

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

使用最新版本的BrowserDetect软件包,您可以获得如下浏览器信息:

Install using below command:

使用以下命令安装:

composer require hisorange/browser-detect

作曲家需要 Hisorange/浏览器检测

Once installed, add dependency in config/app.phpfile.

安装后,在config/app.php文件中添加依赖项。

Add below line in providers array.

在提供者数组中添加以下行。

hisorange\BrowserDetect\ServiceProvider::class,

hisorange\BrowserDetect\ServiceProvider::class,

Add below line in alias array.

在别名数组中添加以下行。

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

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

Include package in any class file as below to use:

在任何类文件中包含包,如下所示:

use BrowserDetect;

使用浏览器检测;

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

在此之后,您可以访问BrowserDetect方法,如以下包链接中所述:

BrowserDetect package link

BrowserDetect包链接

回答by Angelwise

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

除了这里的解释之外,为了检测像 Postman 和 Insomnia 这样的 Rest API 消费者,我与这个答案合并,最终得到了以下在我的场景中表现更好的源代码

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.

所以从上面的代码中,我可以检测到浏览器、平台、机器人和其他消费者,比如 Postman 和 Insomnia。