Online Forum To Support Coders With Technical Assistance - Read Write Execute

How to get user agent in php

Ankit Agrawal
Ankit Agrawal
Published on August 20, 2018 · 1 min read

User agent is a string that help us to identify the current browser. we can find the current browser name by using following code.


$myAgent = getUserAgent($_SERVER['HTTP_USER_AGENT']);

function getUserAgent($aUserAgent)
{
	$aBrowser = "";
	if (strpos($aUserAgent, 'Opera') || strpos($aUserAgent, 'OPR/'))
	{
		$aBrowser = 'Opera';
	}
	elseif(strpos($aUserAgent, 'Edge'))
	{
		$aBrowser = 'Edge';
	}
	elseif (strpos($aUserAgent, 'Chrome'))
	{
		$aBrowser = 'Chrome';
	}
	elseif (strpos($aUserAgent, 'Safari'))
	{
		$aBrowser = 'Safari';
	}
	elseif (strpos($aUserAgent, 'Firefox'))
	{
		$aBrowser = 'Firefox';
	}
	elseif (strpos($aUserAgent, 'MSIE'))
	{
		$aBrowser = 'Internet Explorer';
	}

	return $aBrowser;
}

Was this article helpful?
Ankit Agrawal

Ankit Agrawal

Author & Senior Engineer at CodeExecute

Writing practical engineering guides, debugging real-world bottlenecks, and creating free tools for developer productivity.

← Previous Article Fetch facebook profile data using javascript Next Article → Add some text after your wordpress content