This is find to web browser and OS platform using PHP. preg_match() function using a identify to browser and OS platform .it's is manual coding to display a browser and OS details.simple coding to find browser.
getenv("HTTP_USER_AGENT");
<?php
echo getenv( "HTTP_USER_AGENT" );
?>
echo getenv( "HTTP_USER_AGENT" );
?>
HTTP_USER_AGENT - This also is important when using the get_browser() function for finding out
output:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
preg_match();
<?php
$viewer = getenv( "HTTP_USER_AGENT" );
if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
else
{
echo "undefined";
}
echo $browser;
?>
$viewer = getenv( "HTTP_USER_AGENT" );
if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
else
{
echo "undefined";
}
echo $browser;
?>
preg_match() — Perform a regular expression match
if it's match to display a output else disaply undefined
output:
Mozilla
Source Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Simple coding for finding web browser and OS Paltform</title>
</head>
<?php
$viewer = getenv( "HTTP_USER_AGENT" );
$browser = "An unidentified browser";
if( preg_match( "/MSIE/i", "$viewer" ) )
{
$browser = "Internet Explorer";
}
else if( preg_match( "/Chrome/i", "$viewer" ) )
{
$browser = "Chrome";
}
else if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
$platform = "An unidentified OS!";
if( preg_match( "/Windows/i", "$viewer" ) )
{
$platform = "Windows!";
}
else if ( preg_match( "/Linux/i", "$viewer" ) )
{
$platform = "Linux!";
}
echo("You are using $browser on $platform");
?>
<body>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Simple coding for finding web browser and OS Paltform</title>
</head>
<?php
$viewer = getenv( "HTTP_USER_AGENT" );
$browser = "An unidentified browser";
if( preg_match( "/MSIE/i", "$viewer" ) )
{
$browser = "Internet Explorer";
}
else if( preg_match( "/Chrome/i", "$viewer" ) )
{
$browser = "Chrome";
}
else if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
$platform = "An unidentified OS!";
if( preg_match( "/Windows/i", "$viewer" ) )
{
$platform = "Windows!";
}
else if ( preg_match( "/Linux/i", "$viewer" ) )
{
$platform = "Linux!";
}
echo("You are using $browser on $platform");
?>
<body>
</body>
</html>
Very Useful code snippet
ReplyDeletebut can we use strstr function instead of preg_match since execution time for processing string through regular expressions will be overhead
and strstr function will just match pattern of strings
Looking forward for your reply