PHP Laravel : How to get client browser/ Device?

后端 未结 5 616
野趣味
野趣味 2021-01-18 05:22

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         


        
5条回答
  •  粉色の甜心
    2021-01-18 05: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.

提交回复
热议问题