<?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>Open Source Web Thoughts &#187; php alternative cache</title>
	<atom:link href="http://blog.dewaldbotha.co.za/tag/php-alternative-cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dewaldbotha.co.za</link>
	<description></description>
	<lastBuildDate>Fri, 25 Nov 2011 09:17:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>the down-low on mobile device detection</title>
		<link>http://blog.dewaldbotha.co.za/2009/02/19/the-down-low-on-mobile-device-detection/</link>
		<comments>http://blog.dewaldbotha.co.za/2009/02/19/the-down-low-on-mobile-device-detection/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 14:28:09 +0000</pubDate>
		<dc:creator>dewaldbotha</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[devicealtas]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[mobile device detection]]></category>
		<category><![CDATA[php alternative cache]]></category>
		<category><![CDATA[php apc]]></category>
		<category><![CDATA[wurfl]]></category>

		<guid isPermaLink="false">http://blog.dewaldbotha.co.za/2009-02-19/the-down-low-on-mobile-device-detection/</guid>
		<description><![CDATA[so &#8211; you say you want to detect which mobile devices hit your site? &#8211; in the past, this has been a bit of an issue, but lately &#8211; with really nice projects available out there such as WURFL or DeviceAtlas, you are able to concentrate harder on other issues, instead of having to write <a href="http://blog.dewaldbotha.co.za/2009/02/19/the-down-low-on-mobile-device-detection/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>so &#8211; you say you want to detect which mobile devices hit your site? &#8211; in the past, this has been a bit of an issue, but lately &#8211; with really nice projects available out there such as <a href="http://wurfl.sourceforge.net/" title="WURFL" target="_blank">WURFL</a> or <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a>, you are able to concentrate harder on other issues, instead of having to write a complete library of your own.</p>
<p>so for this, i&#8217;ve decided on <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a>.  just head on over to <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a> and open a developers account &#8211; you will get a one year developer&#8217;s license to play around to see how cool it is.</p>
<p>after registering &#8211; click on the <a href="http://deviceatlas.com/downloads" title="download DeviceAtlas" target="_blank">downloads</a> link &#8211; then go to the php example and download the source files.  after de-tar-and-un-zipping the file, just extract the contents.  at the moment all we are really interested in, is the <em><strong>Mobi/Mtld/DA </strong></em>directory and its contents &#8211; also create a <em><strong>json</strong></em> directory and drop the <em><strong>Sample.jso</strong></em>n file inside.</p>
<p>dump everything into a web directory somewhere. <img src='http://blog.dewaldbotha.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span id="more-16"></span>create a <em><strong>deviceatlas.php</strong></em> file in the working directory root and put the following inside.</p>
<pre lang="php">//includes the DeviceAtlas API

include 'Mobi/Mtld/DA/Api.php';//fetch the previously populated apc cache instance into a variable

$tree = apc_fetch('tree');

//Get the properties from a variable containing a JSON tree loaded from cache

$properties = Mobi_Mtld_DA_Api::getProperties($tree, $_SERVER['HTTP_USER_AGENT']);

//Output the phone properties for your viewing pleasure

print_r($properties);</pre>
<p>&#8216;hhmmm, that&#8217;s it?&#8217;, you might ask, and if you were clever you would have noticed something strange in the code above.</p>
<p>yip, that is pretty much it &#8211; but you also need caching.  the <em><strong>apc_fetch(&#8216;tree&#8217;)</strong></em> command in the code above actually fetches the json file already dumped into a &#8216;tree&#8217; cache instance in <a href="http://www.php.net/manual/en/intro.apc.php" title="PHP APC Cache" target="_blank">PHP APC</a> (Alternative PHP Cache).</p>
<p>why <a href="http://www.php.net/manual/en/intro.apc.php" title="PHP APC Cache" target="_blank">PHP APC</a>, you may also ask.  well, besides using the overstated <a href="http://www.danga.com/memcached/" title="Memcached" target="_blank">memcached</a> (<a href="http://www.phpbuilder.com/board/archive/index.php/t-10346692.html" title="Memcache vs APC" target="_blank">you might want to read this</a>), there are some methods to the madness.  with <a href="http://www.php.net/manual/en/intro.apc.php" title="PHP APC Cache" target="_blank">PHP APC</a>, the cache is shared on a server.  it might not be accessible from other servers, but this is not really a con in my mind.</p>
<p>if you wish to scale your application later one and add another server, there is going to be some issues, you might say &#8211; but there will also be issues if you have millions of users hit one central instance of <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a>.  so, having one instance of <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a> per server might not be too much of an issue.  also when it comes to updating the json file, you might want to consider the <a href="http://en.wikipedia.org/wiki/Single_point_of_failure" title="Single Point of Failure" target="_blank">single point of failure</a> issue of just having one server.</p>
<p>so have a couple of servers, and just write some scripts to update the single instances of <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a>, rather than updating one, already busy server which still needs a cache update.</p>
<p>but, back to business.</p>
<p>first you have to install <a href="http://www.php.net/manual/en/intro.apc.php" title="PHP APC Cache" target="_blank">PHP APC</a>.  i found <a href="http://www.howtoforge.com/apc-php5-apache2-debian-etch" title="Apc Installation guide" target="_blank">this handy guide</a> on the net, which helped me install it on my <a href="http://en.wikipedia.org/wiki/Debian" title="Debian" target="_blank">debian</a> system and integrate it with <a href="http://en.wikipedia.org/wiki/Apache_server" title="Apache" target="_blank">apache</a> and <a href="http://en.wikipedia.org/wiki/Php5" title="PHP 5" target="_blank">php5</a> &#8211; there is also a guide at the bottom for <a href="http://en.wikipedia.org/wiki/Red_hat" title="Red Hat" target="_blank">fedora</a> based machines, so don&#8217;t fret my <a href="http://en.wikipedia.org/wiki/Linux" title="Linux" target="_blank">linux</a> loving friends <img src='http://blog.dewaldbotha.co.za/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>you can create a php script to run the following &#8211; which should cache your <a href="http://deviceatlas.com/" title="DeviceAtlas" target="_blank">DeviceAtlas</a> tree into <a href="http://www.php.net/manual/en/intro.apc.php" title="PHP APC Cache" target="_blank">PHP APC</a>.</p>
<pre lang="php">//ads the sample json data from DeviceAtlas

apc_add('tree', Mobi_Mtld_DA_Api::getTreeFromFile("json/Sample.json"));</pre>
<p>from the first code example, the output might look something like this (depending on your phone)</p>
<p>Array<br />
(<br />
[wmv] =&gt; 0<br />
[vendor] =&gt; Nokia<br />
[mobileDevice] =&gt; 1<br />
[memoryLimitMarkup] =&gt; 357000<br />
[memoryLimitDownload] =&gt; 61440<br />
[midiMonophonic] =&gt; 1<br />
[midiPolyphonic] =&gt; 1<br />
[mpeg4] =&gt; 1<br />
[3gpp] =&gt; 1<br />
[drmOmaForwardLock] =&gt; 1<br />
[drmOmaCombinedDelivery] =&gt; 1<br />
[drmOmaSeparateDelivery] =&gt; 1<br />
[markup.xhtmlMp10] =&gt; 1<br />
[markup.xhtmlBasic10] =&gt; 1<br />
[image.Jpg] =&gt; 1<br />
[image.Png] =&gt; 1<br />
[amr] =&gt; 1<br />
[mp3] =&gt; 1<br />
[aac] =&gt; 1<br />
[h263Type0InVideo] =&gt; 1<br />
[gprs] =&gt; 1<br />
[edge] =&gt; 1<br />
[image.Gif87] =&gt; 1<br />
[uriSchemeTel] =&gt; 0<br />
[qcelp] =&gt; 0<br />
[hsdpa] =&gt; 0<br />
[amrInVideo] =&gt; 1<br />
[3gpp2] =&gt; 0<br />
[displayColorDepth] =&gt; 18<br />
[model] =&gt; N70<br />
[https] =&gt; 1<br />
[image.Gif89a] =&gt; 0<br />
[umts] =&gt; 0<br />
[displayWidth] =&gt; 176<br />
[displayHeight] =&gt; 208<br />
[markup.xhtmlMp11] =&gt; 0<br />
[markup.xhtmlMp12] =&gt; 0<br />
[csd] =&gt; 0<br />
[hscsd] =&gt; 0<br />
[id] =&gt; 204255<br />
[_matched] =&gt; NokiaN70<br />
[_unmatched] =&gt; -1/5.0609.2.0.1 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Link/6.3.1.13.0<br />
)</p>
<p><a href="http://deviceatlas.com/properties" title="DeviceAtlas properties" target="_blank">you can view the attribute explanations here </a></p>
<p>now you can use the phone&#8217;s specific properties to your advantage.  you also might want to look at the <a href="https://addons.mozilla.org/en-US/firefox/addon/59" title="User agent switcher" target="_blank">user agent switcher add-on</a> for firefox for testing purposes.</p>
<p>that&#8217;s it.  now every once in a while you can just update the json file, delete the current cache (<a href="http://www.php.net/manual/en/function.apc-delete.php" title="APC Delete Cache" target="_blank">http://www.php.net/manual/en/function.apc-delete.php</a>) and bob&#8217;s your device detecting, apc caching uncle.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dewaldbotha.co.za/2009/02/19/the-down-low-on-mobile-device-detection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

