2016-05-18 07:53 阅读 1.0k

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

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');
            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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    doukanmang3687 doukanmang3687 2016-05-18 08:22

    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(
        // ...
        // ...

    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:

    点赞 评论 复制链接分享
  • dptgpyl61857413 dptgpyl61857413 2018-06-22 17:17

    I ended using the faster, and simpler way:


    Hope it helps someone!

    点赞 评论 复制链接分享
  • doutang7383 doutang7383 2019-02-21 15:40

    Use Agent

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

    In the new version you can also detect the robots.

    点赞 评论 复制链接分享
  • dongpo1599 dongpo1599 2019-08-02 09:53

    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.

    点赞 评论 复制链接分享