We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,633 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Browser Detection

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.

6
Contributors
27
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
30
Views
Question
Answered
davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2
deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

You can use get_browser(): http://php.net/manual/en/function.get-browser.php
But the client can spoof the user agent, so don't trust what you get and always sanitize the data, because an XSS attack can be performed.

cereal
Veteran Poster
1,144 posts since Aug 2007
Reputation Points: 344
Solved Threads: 221
Skill Endorsements: 22

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);
?>
davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

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.

emclondon
Junior Poster in Training
94 posts since Mar 2010
Reputation Points: 48
Solved Threads: 4
Skill Endorsements: 0

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

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

you can use javascript

navigator.userAgent
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

Well, what's the complete codes ?

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

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.

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

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:	
	}

?>
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

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>
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

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 ?

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

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

vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

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

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

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 ?

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

It's probably an error in Chrome/front-page.php, check your quotes in that file.

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

What's wrong with the quotes ?

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

The file is probably missing one. Check it!

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Nothing is missing:

I have this file in each folder:

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

davy_yg
Master Poster
730 posts since May 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page rendered in 0.1157 seconds using 2.73MB