Reputation: 321
I am using laravel 5.0. How do I get the user agent from $request in a controller ?
public function index(Request $request)
{
$useragent = $request->userAgent; // ????
return $useragent;
}
Upvotes: 25
Views: 48190
Reputation: 1435
From laravel 5.4 you can do:
$request->userAgent();
or
request()->userAgent();
Upvotes: 5
Reputation: 461
Use this way it's work for me
echo Request::server('HTTP_USER_AGENT');
OR
echo $_SERVER['HTTP_USER_AGENT']
Upvotes: 5
Reputation: 1473
Outside of controller can try:
$server = \Request::server();
echo $server['HTTP_USER_AGENT']
Upvotes: 0
Reputation: 91
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;
may be it is usefull this code . or another try laravel package https://github.com/antonioribeiro/tracker or https://github.com/jenssegers/agent
Upvotes: 6
Reputation: 4297
You can use either of following ways:
$ua = $request->server('HTTP_USER_AGENT');
$ua = $request->header('User-Agent');
Upvotes: 51