Get useragent in laravel 5 php controller

I am using laravel 5.0. How to get user agent from $ request in controller?

public function index(Request $request) { $useragent = $request->userAgent; // ???? return $useragent; } 
+5
source share
2 answers

You can use one of the following methods:

 $ua = $request->server('HTTP_USER_AGENT'); $ua = $request->header('User-Agent'); 
+13
source

I think you can use this code

 $user_agent = $request->header('User-Agent'); $bname = 'Unknown'; $platform = 'Unknown'; //First get the platform? if (preg_match('/linux/i', $user_agent)) { $platform = 'linux'; } elseif (preg_match('/macintosh|mac os x/i', $user_agent)) { $platform = 'mac'; } elseif (preg_match('/windows|win32/i', $user_agent)) { $platform = 'windows'; } echo $platform; echo "<br>"; // Next get the name of the useragent yes seperately and for good reason if(preg_match('/MSIE/i',$user_agent) && !preg_match('/Opera/i',$user_agent)) { $bname = 'Internet Explorer'; $ub = "MSIE"; } elseif(preg_match('/Firefox/i',$user_agent)) { $bname = 'Mozilla Firefox'; $ub = "Firefox"; } elseif(preg_match('/Chrome/i',$user_agent)) { $bname = 'Google Chrome'; $ub = "Chrome"; } elseif(preg_match('/Safari/i',$user_agent)) { $bname = 'Apple Safari'; $ub = "Safari"; } elseif(preg_match('/Opera/i',$user_agent)) { $bname = 'Opera'; $ub = "Opera"; } elseif(preg_match('/Netscape/i',$user_agent)) { $bname = 'Netscape'; $ub = "Netscape"; } echo $bname; 

this code may be helpful. or another laravel package https://github.com/antonioribeiro/tracker or https://github.com/jenssegers/agent

+1
source

Source: https://habr.com/ru/post/1245930/


All Articles