<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dauidus Design &#187; PHP</title>
	<atom:link href="http://www.dauidusdesign.com/category/web-design/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dauidusdesign.com</link>
	<description>Web design, graphic design and professional photography from Dave Winter</description>
	<lastBuildDate>Wed, 30 Jan 2013 19:09:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Simple Server-Side Browser Detection with PHP</title>
		<link>http://www.dauidusdesign.com/server-side-detection/</link>
		<comments>http://www.dauidusdesign.com/server-side-detection/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 20:14:27 +0000</pubDate>
		<dc:creator>Dave Winter</dc:creator>
				<category><![CDATA[Browser Detection]]></category>
		<category><![CDATA[Freelancers]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[alternate content]]></category>
		<category><![CDATA[css code]]></category>
		<category><![CDATA[css hacks]]></category>
		<category><![CDATA[custom content]]></category>
		<category><![CDATA[device performance]]></category>
		<category><![CDATA[header php]]></category>
		<category><![CDATA[internet explorer]]></category>
		<category><![CDATA[mobile devices]]></category>

		<guid isPermaLink="false">http://www.dauidusdesign.com/?p=1032</guid>
		<description><![CDATA[<em>Ever wish you could make a webpage behave according to the browser it is being viewed with?  In many cases this can be done with some well-placed CSS code.  But, how about those situations when CSS can't help us out?   CSS hacks and tricks won't let you target Firefox, Chrome or mobile devices.  So, do you just stay away from using custom content in those browsers?  

Enter PHP browser detection!  With just a little bit of code, your site can reach its maximum potential in every browser, and even in mobile devices.</em>]]></description>
				<content:encoded><![CDATA[<p><img class="blogtop" title="Wordpress" src="http://www.dauidusdesign.com/wp-content/uploads/2011/06/php.jpg" title="Server-Side Browser Detection with PHP in WordPress" alt="Server-Side Browser Detection with PHP in WordPress" width="200" height="200" /></p>
<p><span class="dropcap">E</span><em>ver wish you could make a webpage behave according to the browser it is being viewed with?  In many cases this can be done with some well-placed CSS code.  But, how about those situations when CSS can&#8217;t help us out?   CSS hacks and tricks won&#8217;t let you target Firefox, Chrome or mobile devices.  So, do you just stay away from using custom content in those browsers?  </p>
<p>Enter PHP browser detection!  With just a little bit of code, your site can reach its maximum potential in every browser, and even in mobile devices.</em>  </p>
<p>Some common uses for browser detection include:</p>
<ul style="margin: 0 0 0 40px;">
<li>- avoiding choppy JavaScript transitions by serving different versions of the script according to browser or device performance</li>
<li>- serving alternate content to iPhone browsers where Flash would otherwise be used</li>
<li>- getting rid of elements that cause rendering problems in some browsers but aren&#8217;t essential</li>
<li>- automatically populating a form with the current browser and version for simple debugging purposes</li>
</ul>
<p></p>
<p style="text-align: left;">To get an idea of usage, load this page in Chrome or Safari and take a look at the header.  Now load it in Internet Explorer.  Those subtle differences you see in the header allow the code to run more smoothly in mobile devices and IE.  And though it doesn&#8217;t look exactly the same, I&#8217;ve had no problems with branding the recognizable animation as my handle.  Enough talk &#8212; let&#8217;s get to it, then.</p>
<h4 style="color: #0099cc;" >The Basic Example Code</h4>
<p>Here is an implementation of this code as it appears in the header.php of this site:</p>
<pre class="brush: php; highlight: [7,10,14]; html-script: true; title: ; notranslate">
&lt;!-- Top Menu --&gt;
    &lt;div id=&quot;logo&quot;&gt;&lt;a href=&quot;&lt;?php bloginfo('url'); ?&gt;&quot;&gt;&lt;img src=&quot;&lt;?php bloginfo('template_directory'); ?&gt;/images/logo.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

        &lt;?php 
	    $user_agent = $_SERVER['HTTP_USER_AGENT'];

		if (preg_match('/iphone/i', $user_agent))
		    wp_nav_menu( array('menu' =&gt; 'topnavie' ));		

		elseif (preg_match('/MSIE/i',$u_agent) &amp;&amp; !preg_match('/Opera/i',$u_agent)) 			
		    wp_nav_menu( array('menu' =&gt; 'topnavie' ));
		} 							
			
		else {
		    wp_nav_menu( array('menu' =&gt; 'topnav' ));
			print
			'&lt;a href=&quot;http://twitter.com/dauidus&quot; target=&quot;_blank&quot;&gt;&lt;div id=&quot;bird&quot;&gt;&lt;/div&gt;&lt;/a&gt;';	
		};
	?&gt;   
&lt;!-- --&gt;
</pre>
<p>As you can see here, I&#8217;m using a single line of code that tells the server to check the user agent.  Then we just employ normal PHP <em>if</em> statements to define what should be displayed in each browser.  First, I check for both the iPhone and IE and serve a stripped-down version of my site menu.  Then, I serve everything else a more beefed-up version of that same menu.  Of course, this is all pretty basic so far.</p>
<h4 style="color: #0099cc;" >Going a Bit Deeper</h4>
<p>But what if you need to target other browsers?  Here&#8217;s the code to target some other common browsers without any additional formatting:</p>
<pre class="brush: php; highlight: [7,12,18,23,28]; html-script: true; title: ; notranslate">
&lt;?php

// check what browser the visitor is using
  $user_agent = $_SERVER['HTTP_USER_AGENT'];

// This bit differentiates IE from Opera
if(preg_match('/MSIE/i',$u_agent) &amp;&amp; !preg_match('/Opera/i',$u_agent)) 
    { 
      print
        'This is Internet Explorer. (Insert joke here)';        
    } 
    elseif(preg_match('/mozilla/i',$u_agent) &amp;&amp; !preg_match('/compatible/', $userAgent)) 
    { 
      print
        'This is FireFox.';
    } 
// let Chrome be recognized as Safari
    elseif(preg_match('/webkit/i',$u_agent)) 
    { 
      print
        'This is either Chrome or Safari.';
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
      print
        'This is Opera. Like to live on the edge, huh?';
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
      print
        'This is Netscape, and you really need to upgrade.';
    } 

?&gt;
</pre>
<p>Now, there are a few things to be explained here, even before we really get into the code.  The opera browser has had many problems in the past regarding <em>user agent spoofing</em>.  This spoofing was necessary because the MSN and IE browsers allowed developers to only serve content for their software (which was, at the time, considered to be top-notch).  Pretty much, this was IE&#8217;s attempt at sabotaging the web into using their browser out of necessity, not choice (tisk, tisk, IE).  In order for that content to be visible in Opera, their developers found it necessary to spoof their user agent to look and act like IE.  Now, this really hasn&#8217;t been too much of a problem since 2005, when Opera 8.02 was released with the option to use its own user agent string (and finally grew up as a big-boy browser&#8230; sort-of)  But as developers, we must remember that content needs to be served to the widest possible audience (as some companies won&#8217;t allow their employees to upgrade software).  </p>
<p>So, why did I make you sit through that winded explanation?  Take a look again at that first line of code above.  Notice that we not only detect the IE browser, but also conditionally detect for &#8220;if not opera.&#8221;  This should protect against all that nasty user agent spoofing for those who run versions of Opera younger than 8.02.  But, that&#8217;s not all.  Notice that we also later detect for the opera user agent with no other conditionals.  This should take care of all those later versions of Opera which don&#8217;t run into the spoofing stuff.</p>
<p>Still with me?  Good, because we&#8217;re not quite done with the explanations.  It&#8217;s not really spoofing, but Chrome and Safari both use the &#8216;webkit&#8217; user agent (very familiar to anyone whose written CSS3).  So, using our script, Chrome will always be recognized as Safari (the default webkit browser).  This really shouldn&#8217;t be a problem if you are just wanting your content to be served identically across browsers.  But, if you&#8217;re looking to provide different content for those two browsers, this solution will not work for you.  </p>
<h4 style="color: #0099cc;" >Detecting Browser Versions</h4>
<p>So, what about those instances when you really need to serve alternate content to specific versions of browsers (can you say IE6)?  </p>
<pre class="brush: php; highlight: [7,12]; html-script: true; title: ; notranslate">
&lt;?php

// check what browser the visitor is using
  $user_agent = $_SERVER['HTTP_USER_AGENT'];

// This bit differentiates IE from Opera
if(preg_match('/(?i)MSIE [1-6]/',$u_agent) &amp;&amp; !preg_match('/Opera/i',$u_agent) &amp;&amp; ) {
    // if IE&lt;=6
        print
        'This is IE6 or less (the browsers of infamous dooooom!)';
}
else {
    // if IE&gt;6
        print
        'This is IE7 or greater.';
}

?&gt;
</pre>
<p>So, what we&#8217;ve done here is added the major versions to our &#8216;preg_match&#8217; detection string.  In this case, we will check for IE versions 1-6 and serve some content for those.  If that doesn&#8217;t match the version, we can assume the user is on IE7 or above and serve them alternate content for those browser versions.  </p>
<p>So there you have it&#8230; simple browser detection using nothing but a few lines of PHP.  In a small number of cases, this script can produce false positives (for example, if IE is being run in compatibility mode).  In those cases, it may be best to run your site through a CMS and use a pre-packaged plugin.  I know there are a few for WordPress, but most have far more code than is necessary for most uses.  If you have found one of these plugins, let me know about it in the comments.</p>
<p>Caio!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dauidusdesign.com/server-side-detection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
