douxia6163 2016-05-18 07:53
浏览 1946
已采纳

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

  • 写回答

4条回答 默认 最新

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀