<?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>code-diesel &#187; agents</title>
	<atom:link href="http://www.codediesel.com/tag/agents/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Detecting user agents in php</title>
		<link>http://www.codediesel.com/pear/detecting-user-agents-in-php/</link>
		<comments>http://www.codediesel.com/pear/detecting-user-agents-in-php/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 13:48:29 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[browser]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[agents]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1660</guid>
		<description><![CDATA[how to detect browser agents in php]]></description>
			<content:encoded><![CDATA[<p>Every time you use your browser to access a website a User-Agent header is sent to the respective server.<br />
Detecting user agents on the server can be useful for many reasons. </p>
<p>1. Browsers Quirks – Despite the standardization in browsers, there will remain some quirks in various browsers that you will need to iron out on a regular basis.<br />
2. Personalize Content – It may be required to deliver different type of content depending on the browser type (although it is usually not recommended); whether mobile or otherwise.<br />
3. Illegal Access – Prevent bandwidth hogging bots and poorly programmed clients from downloading your content.<br />
<span id="more-1660"></span><br />
Below is a sample header for my site when using Firefox.</p>

<div class="wp_codebox"><table><tr id="p16601"><td class="code" id="p1660code1"><pre class="text" style="font-family:monospace;">http://www.codediesel.com/
&nbsp;
GET / HTTP/1.1
Host: www.codediesel.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6)
Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive</pre></td></tr></table></div>

<p>PHP already has a builtin function &#8211; <a target="_blank" href="http://us2.php.net/manual/en/function.get-browser.php">get_browser()</a> that lets you determine the capabilities of the user&#8217;s browser, but it requires a browscap.ini to be installed, which sadly is not bundled with PHP, and also the function can slow down your php script. The other way to get a user agent is to use php server variables like below,</p>

<div class="wp_codebox"><table><tr id="p16602"><td class="code" id="p1660code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this post we will use the <a target="_blank" href="http://pear.php.net/package/net_useragent_detect/redirected">Net_UserAgent_Detect</a> Pear package to determine user agents.</p>
<h4>Installation</h4>
<p>Net_UserAgent_Detect being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p16603"><td class="code" id="p1660code3"><pre class="text" style="font-family:monospace;">pear install Net_UserAgent_Detect-2.5.1</pre></td></tr></table></div>

<h4>Usage</h4>
<p>Usage is quite simple. For example the following code:</p>

<div class="wp_codebox"><table><tr id="p16604"><td class="code" id="p1660code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getUserAgent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>will return the following string on Firefox 3.0.6: This can be quite useful if you need to log the users browser or OS type.</p>

<div class="wp_codebox"><table><tr id="p16605"><td class="code" id="p1660code5"><pre class="text" style="font-family:monospace;">Mozilla/5.0 (Windows; U; Windows NT 5.1; 
en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 FirePHP/0.2.4</pre></td></tr></table></div>

<p>getUserAgent() is only one of the many methods available in the class.</p>

<div class="wp_codebox"><table><tr id="p16606"><td class="code" id="p1660code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Net/UserAgent/Detect.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Browser String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getBrowserString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OS String: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getOSString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Javascript: '</span><span style="color: #339933;">.</span>Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">getFeature</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Some other useful functions to directly test the type of browser are:<br />
isIE()<br />
isNavigator( )<br />
isNetscape( )</p>
<p>So to detect if the user is running Firefox:</p>

<div class="wp_codebox"><table><tr id="p16607"><td class="code" id="p1660code7"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$browser</span> <span style="color: #339933;">=</span> Net_UserAgent_Detect<span style="color: #339933;">::</span><span style="color: #004000;">isNetscape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/detecting-user-agents-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

