Start New Discussion within our Web Development Community

Hello,

How to program browser detection in php ?

I would like to detect the browser automatically, then use if browser = ... then ... else if browser = ... then .... endif

How to do so ?

Thanks.

Hi, where to place the codes? I would like to place a long codes such as if browser = (IE) then long-IE-php-html-css-codes else if browser = (firefox) then long-firefox-php-html-css-codes.

<?php
function getBrowser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
    
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
    
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }
    
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
    
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
} 

// now try it
$ua=getBrowser();
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
print_r($yourbrowser);
?>

based on the code you posted, all you need to do is to include the file in your php page and perform a simple switch to get the result you are after. something like this:

yourpage.php

include('get_browser.php');
	$browser=getBrowser();
	switch ($browser['name'])
	{
		case 'Google Chrome':
			echo "you are using chrome";
			break;
		case 'Internet Explorer':
			echo "you are using ie";
			break;
		case 'Mozilla Firefox':
				echo "you are using firefox";
			break;		
		default:
			break;		
	}

now just a reminder that, if you are looking just to change the CSS based on the browser, this is not the way to do it.

Not only change the css, but also change the HTML and php based on the browser. How to do so ?

Well, where to place the:

if browser = (IE) then long-IE-php-html-css-codes else if browser = (firefox) then long-firefox-php-html-css-codes ?

The link only shows the Js codes.

you know what you can use @emclodon code, it is much better

<?php

	include('get_browser.php');
	$browser=getBrowser();
	switch ($browser['name'])
	{
		case 'Google Chrome':
			include('your_google_chrome_page.php'); // your separate google chrome page
			break;
		case 'Internet Explorer':
			include('your_ie_page.php'); // your separate ie page
			break;
		case 'Mozilla Firefox':
			include('your_mozilla_page.php'); //your separate mozilla page
			break;		
		default:	
	}

?>

or you can put condition on every includes

<html>
	<head>
	<?php
	
	include('get_browser.php');
	$browser=getBrowser();

	?>
	
	<?php
	
	switch ($browser['name'])
	{
		case 'Google Chrome':
			echo '<link rel="stylesheet" type="text/css" href="/your_google_chrome_style.css" />';
			echo '<script type="text/javascript" src="/your_google_chrome_javascript.js"></script>';
			break;
		case 'Internet Explorer':
			echo '<link rel="stylesheet" type="text/css" href="/your_ie_style.css" />';
			echo '<script type="text/javascript" src="/your_ie_javascript.js"></script>';
			break;
		case 'Mozilla Firefox':
			echo '<link rel="stylesheet" type="text/css" href="/your_mozilla_style.css" />';
			echo '<script type="text/javascript" src="/your_mozilla_javascript.js"></script>';
		        break;
		default:
	}
	
	?>
	</head>
	<body>
	<?php

	switch ($browser['name'])
	{
		case 'Google Chrome':
			echo '<p>google chrome content</p>';
			break;
		case 'Internet Explorer':
			echo '<p>Internet Explorer content</p>';
			break;
		case 'Mozilla Firefox':
			echo '<p>Mozilla Firefox content</p>';
			break;
		default:
	}
	
	?>
	</body>
	</html>

front-page.php

<?php

	include('get_browser.php');
	$browser=getBrowser();
	switch ($browser['name'])
	{
		case 'Google Chrome':
			include('Chrome/front-page.php'); // your separate google chrome page
			break;
		case 'Internet Explorer':
			include('IE/front-page.php'); // your separate ie page
			break;
		case 'Mozilla Firefox':
			include('Mozilla/front-page.php'); // your separate mozilla page
			break;		
		default:
			break;		
	}

?>

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\wordpress\wp-content\themes\ocklaw\IE\front-page.php on line 42

line42: include('Mozilla/front-page.php'); // your separate mozilla page

How to fix it ?

no the error is in front-page.php that inside the IE folder, what is on line 42?

line42: include('Mozilla/front-page.php'); // your separate mozilla page

front-page.php

case 'Google Chrome':
	      include('Chrome/front-page.php'); // your separate google chrome page
		break;
	case 'Internet Explorer':

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\wordpress\wp-content\themes\ocklaw\IE\front-page.php on line 42

line 42: case 'Internet Explorer':

Why is it ?

Nothing is missing:

I have this file in each folder:

Chrome : front-page.php
IE : front-page.php
Mozzila : front-page.php

This question has already been answered. Start a new discussion instead.