<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Adventures in PHP / DHTML / CSS and MySQL</title>
	<atom:link href="http://pureform.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pureform.wordpress.com</link>
	<description>Adventures in PHP / DHTML / CSS and MySQL</description>
	<lastBuildDate>Thu, 02 May 2013 06:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pureform.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Adventures in PHP / DHTML / CSS and MySQL</title>
		<link>http://pureform.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pureform.wordpress.com/osd.xml" title="Adventures in PHP / DHTML / CSS and MySQL" />
	<atom:link rel='hub' href='http://pureform.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Make prototype.js&#8217;s toQueryString method php safe</title>
		<link>http://pureform.wordpress.com/2009/07/09/make-prototype-jss-toquerystring-method-php-safe/</link>
		<comments>http://pureform.wordpress.com/2009/07/09/make-prototype-jss-toquerystring-method-php-safe/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 22:49:35 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[toQueryString]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=79</guid>
		<description><![CDATA[LAST UPDATED APRIL 12, 2011 One hurdle I came across today is Prototype&#8217;s Hash.toQueryString() method, which turns a hash into a query string [bet you couldn't have guessed that ;-)]. The wall I ran into was how it parses arrays within the hash, it would take something like this [in JSON]: {&#034;someArray&#034;: [1,2,3]} &#8230; and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=79&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-weight:bold;color:#cc3333;"><em>LAST UPDATED APRIL 12, 2011</em></span></p>
<p>One hurdle I came across today is Prototype&#8217;s <a href="http://www.prototypejs.org/api/hash/toQueryString" target="_blank"><code>Hash.toQueryString()</code></a> method, which turns a hash into a query string [bet you couldn't have guessed that ;-)]. The wall I ran into was how it parses arrays within the hash, it would take something like this [in JSON]:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
{&#034;someArray&#034;: [1,2,3]}</span>

</code></pre>
<p> &#8230; and run it through <code>toQueryString()</code>, it returns:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
someArray=1&amp;someArray=2&amp;someArray=3</span>

</code></pre>
<p>&#8230; which is incredibly useless for PHP scripts attempting to consume this query string. PHP will effectively set the String &#8220;someArray&#8221; to 1, then set it to 2, then set it to 3. Not what we expected. What we PHP devs want is this instead:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
someArray[]=1&amp;someArray[]=2&amp;someArray[]=3</span>

</code></pre>
<p>&#8230; which when consumed by PHP will set an Array called &#8220;someArray&#8221; to the values 1, 2 and 3.</p>
<p><strong>Teh codes</strong></p>
<p>I didn&#8217;t find a single result while trying to google this, and I doubt the prototype guys have much interest in changing this given their Ruby on Rails background [which parses the query string just fine, without the &#034;[]&#034;], so I wrote a quick hack to remedy this situation. Paste this at the BOTTOM of your prototype.js file:</p>
<p><strong>For prototype 1.6.x:</strong></p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
var _toQueryStringAppendBrackets = true;
Hash.prototype.toQueryString = function() {
    function toQueryPair(key,value) {
        if (Object.isUndefined(value)) return key;
        return key + &#034;=&#034; + encodeURIComponent(String.interpret(value));
    }

    return this.inject([],function(results,pair) {
        var key = encodeURIComponent(pair.key), values = pair.value;

        if (values &amp;&amp; typeof values === &#034;object&#034;) {
            if (Object.isArray(values)) {
                key += (_toQueryStringAppendBrackets === true) ? &#034;[]&#034; : &#034;&#034;; // Uses global, dirty... I know :-)
                return results.concat(values.map(toQueryPair.curry(key)));
            }
        } else {
            results.push(toQueryPair(key,values));
        }
        return results;
    }).join(&#034;&amp;&#034;);
}
</span>
</code></pre>
<p><strong>If your using prototype 1.7, this this instead.</strong></p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
Hash.prototype.toQueryString = function() {
    function toQueryPair(key,value) {
        if (Object.isUndefined(value)) { return key; }
        return key + "=" + encodeURIComponent(String.interpret(value));
    }
    return this.inject([], function(results, pair) {
        var key = encodeURIComponent(pair.key), values = pair.value;
        if (values &amp;&amp; typeof values == 'object') {
            if (Object.isArray(values)) {
                var queryValues = [],k='';
                for (var i = 0, len = values.length, value; i &lt; len; i++) {
                    value = values[i];
                    k = key+((_toQueryStringAppendBrackets === true) ? &quot;[]&quot; : &quot;&quot;); // Uses global, dirty... I know :-)
                    queryValues.push(toQueryPair(k,value));
                }
                return results.concat(queryValues);
            }
        } else results.push(toQueryPair(key, values));
        return results;
    }).join(&#039;&amp;&#039;);
}
</span>
</code></pre>
<p>I had to copy <code>toQueryPair()</code> into this function because it is a &#8220;private&#8221; method of Hash, and not accessible from this scope [correct me if I'm wrong]. Since <code>toQueryString()</code> is used internally by prototype by things like Ajax&#8217;s <code>parameters</code> option &#8212; and probably various other libraries written for prototype &#8212; I set the var <code>_toQueryStringAppendBrackets</code> to be available globally in case you need to switch this functionality on and off during script execution.</p>
<p>As always, if you have a more &#8220;JS ninja&#8221; way of solving this problem, please comment here and I will update this post [I'm especially turned off by having to include <code>toQueryPair()</code> again, and by not being able to use <code>Object.extend()</code> -- FYI]. Any comments which are critical but not constructive will be removed. Why? Because I hate when people do that and it doesn&#8217;t make the internet better. Don&#8217;t like my rules? Don&#8217;t post anything.</p>
<br />Posted in JavaScript, PHP, Prototype Tagged: JavaScript, PHP, Prototype, toQueryString <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=79&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2009/07/09/make-prototype-jss-toquerystring-method-php-safe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Binding your setTimeout methods in JavaScript using Prototype</title>
		<link>http://pureform.wordpress.com/2009/04/20/binding-your-settimeout-methods-in-javascript-using-prototype/</link>
		<comments>http://pureform.wordpress.com/2009/04/20/binding-your-settimeout-methods-in-javascript-using-prototype/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 20:59:06 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[DHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=51</guid>
		<description><![CDATA[Never mind, use this instead :-) www.prototypejs.org/api/function/delay Two features which are indispensable in Prototype are it&#8217;s bind and bindAsEventListener functions. I&#8217;m not going to write about what they can do for you, but do know that they are indispensable to OO JavaScript for callbacks, lambda functions, closures etc&#8230; One place where I desperately wanted to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=51&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Never mind, use this instead :-)</p>
<p><a href="http://www.prototypejs.org/api/function/delay" target="_blank">www.prototypejs.org/api/function/delay</a></p>
<div style="text-decoration:line-through;">Two features which are indispensable in <a href="http://www.prototypejs.org/" target="_blank">Prototype</a> are it&#8217;s <code>bind</code> and <code>bindAsEventListener</code> functions. I&#8217;m not going to write about <a href="http://www.prototypejs.org/api/function/bind" target="_blank">what they can do for you</a>, but do know that they are <strong>indispensable</strong> to OO JavaScript for callbacks, lambda functions, closures etc&#8230; One place where I desperately wanted to use <code>bind()</code>ing, as well as have the ability to call methods within an instantiated Class, was with <code>setTimeout()</code> callbacks. Here&#8217;s the solution I came up with:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#007700;">var </span><span style="color:#0000BB;">_Class </span><span style="color:#007700;">= Class.</span><span style="color:#0000BB;">create</span><span style="color:#007700;">({
    </span><span style="color:#0000BB;">initialize</span><span style="color:#007700;">: function() {
        </span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">_someVar </span><span style="color:#007700;">= </span><span style="color:#0000BB;">0</span><span style="color:#007700;">;
    },
    </span><span style="color:#0000BB;">function1</span><span style="color:#007700;">: function() {
        </span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">_someVar</span><span style="color:#007700;">++;
        </span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">function3</span><span style="color:#007700;">();
    },
    </span><span style="color:#0000BB;">function2</span><span style="color:#007700;">: function() {
        var </span><span style="color:#0000BB;">_lambda </span><span style="color:#007700;">= </span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">function1</span><span style="color:#007700;">.</span><span style="color:#0000BB;">bind</span><span style="color:#007700;">(</span><span style="color:#0000BB;">this</span><span style="color:#007700;">);
        </span><span style="color:#0000BB;">setTimeout</span><span style="color:#007700;">(</span><span style="color:#0000BB;">_lambda</span><span style="color:#007700;">,</span><span style="color:#0000BB;">10</span><span style="color:#007700;">);
    },
    </span><span style="color:#0000BB;">function3</span><span style="color:#007700;">: function() {
        </span><span style="color:#0000BB;">alert</span><span style="color:#007700;">(</span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">_someVar</span><span style="color:#007700;">);
    }
});

var object = new </span><span style="color:#0000BB;">_Class</span><span style="color:#007700;">;
object.</span><span style="color:#0000BB;">function3</span><span style="color:#007700;">(); </span><span style="color:#FF8000;">// 0</span>
<span style="color:#007700;">object.</span><span style="color:#0000BB;">function2</span><span style="color:#007700;">(); </span><span style="color:#FF8000;">// 1
</span></span>
</code></pre>
<p>Basically, what you do is assign the method you want to call [without the parenthesis ()] to a variable [in this case, <code>_lambda</code>], and finally <code>bind()</code> it&#8230;</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#007700;">var </span><span style="color:#0000BB;">_lambda </span><span style="color:#007700;">= </span><span style="color:#0000BB;">this</span><span style="color:#007700;">.</span><span style="color:#0000BB;">function1</span><span style="color:#007700;">.</span><span style="color:#0000BB;">bind</span><span style="color:#007700;">(</span><span style="color:#0000BB;">this</span><span style="color:#007700;">);</span>
</span>
</code></pre>
<p>Then, simply assign <code>_lambda</code> to <code>setTimeout()</code>.</p>
<p>If you don&#8217;t <code>bind()</code> <code><strong>this</strong></code> to the method, it will assume that the <code><strong>this</strong></code> you&#8217;re referring to is <code><strong>window</strong></code>, and not your <code>_Class</code> instance.</p>
<p><em>As always, please comment with any constructive contributions below. I know this isn&#8217;t probably the best way to implement this, so if you know a better way, please comment!</em></div>
<br />Posted in DHTML, JavaScript Tagged: bind, JavaScript, OOP, Prototype, setTimeout <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=51&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2009/04/20/binding-your-settimeout-methods-in-javascript-using-prototype/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Memcache &amp; MySQL PHP Session Handler</title>
		<link>http://pureform.wordpress.com/2009/04/08/memcache-mysql-php-session-handler/</link>
		<comments>http://pureform.wordpress.com/2009/04/08/memcache-mysql-php-session-handler/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 22:36:01 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[Memcache]]></category>
		<category><![CDATA[mysql+]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[write through cache]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=38</guid>
		<description><![CDATA[LAST UPDATED MAY 17, 2009 I have recently read Cal Henderson&#8217;s book, Building Scalable Web Sites, and was inspired 6 ways from Sunday on just about every approach I take to web development. I highly recommend you purchase this book, and read through it asap&#8230; it is a must read [even though it was published [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=38&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-weight:bold;color:#cc3333;"><em>LAST UPDATED MAY 17, 2009</em></span></p>
<p>I have recently read Cal Henderson&#8217;s book, <a href="http://oreilly.com/catalog/9780596102357/" target="_blank">Building Scalable Web Sites</a>, and was inspired 6 ways from Sunday on just about every approach I take to web development. I highly recommend you purchase this book, and read through it asap&#8230; it is a must read [even though it was published back in 2006]. My friend <a href="http://twitter.com/kastner" target="_blank">Erik Kastner</a> recommended it to me, and now I&#8217;m recommending it to you :-)</p>
<p>Anyways, back on topic&#8230; One of the major concepts I picked up was that of Write Through Caches. Write Through Caches are explained in depth all throughout the internetS, so I leave you with this: <a href="http://tinyurl.com/cgeobs">tinyurl.com/cgeobs</a>. This script also solves the scalability issue you will experience when you move your web site / application to more than one web server. If you put a Memcache and MySQL daemon on one box and have all your web servers connect to it, your sessions are in one centralized place [though, this doesn't account for fail over].</p>
<p>I also built in a simple check to see if the session data had changed before writing it to the DB. Most of the time it doesn&#8217;t, so this should offer some more performance.</p>
<p>This was written for PHP5, if you&#8217;re still using PHP4, <a href="http://gophp5.org/" target="_blank">click here</a>.</p>
<p><em>This script assumes you have already connected to your database.</em></p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">class </span><span style="color:#0000BB;">SessionHandler </span><span style="color:#007700;">{
        public </span><span style="color:#0000BB;">$lifeTime</span><span style="color:#007700;">;
        public </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">;
        public </span><span style="color:#0000BB;">$initSessionData</span><span style="color:#007700;">;

        function </span><span style="color:#0000BB;">__construct</span><span style="color:#007700;">() {
            </span><span style="color:#FF8000;"># Thanks, inf3rno
            </span><span style="color:#0000BB;">register_shutdown_function</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;session_write_close&#34;</span><span style="color:#007700;">);

            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache </span><span style="color:#007700;">= new </span><span style="color:#0000BB;">Memcache</span><span style="color:#007700;">;
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime </span><span style="color:#007700;">= </span><span style="color:#0000BB;">intval</span><span style="color:#007700;">(</span><span style="color:#0000BB;">ini_get</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;session.gc_maxlifetime&#34;</span><span style="color:#007700;">));
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">initSessionData </span><span style="color:#007700;">= </span><span style="color:#0000BB;">null</span><span style="color:#007700;">;
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">connect</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;127.0.0.1&#34;</span><span style="color:#007700;">,</span><span style="color:#0000BB;">11211</span><span style="color:#007700;">);

            return </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">open</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$savePath</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$sessionName</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$sessionID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">session_id</span><span style="color:#007700;">();
            if (</span><span style="color:#0000BB;">$sessionID </span><span style="color:#007700;">!== </span><span style="color:#DD0000;">&#34;&#34;</span><span style="color:#007700;">) {
                </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">initSessionData </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">read</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
            }

            return </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">close</span><span style="color:#007700;">() {
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime </span><span style="color:#007700;">= </span><span style="color:#0000BB;">null</span><span style="color:#007700;">;
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache </span><span style="color:#007700;">= </span><span style="color:#0000BB;">null</span><span style="color:#007700;">;
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">initSessionData </span><span style="color:#007700;">= </span><span style="color:#0000BB;">null</span><span style="color:#007700;">;

            return </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">read</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$data </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">get</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
            if (</span><span style="color:#0000BB;">$data </span><span style="color:#007700;">=== </span><span style="color:#0000BB;">false</span><span style="color:#007700;">) {
                </span><span style="color:#FF8000;"># Couldn't find it in MC, ask the DB for it

                </span><span style="color:#0000BB;">$sessionIDEscaped </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_real_escape_string</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
                </span><span style="color:#0000BB;">$r </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SELECT `sessionData` FROM `tblsessions` WHERE `sessionID`='$sessionIDEscaped'&#34;</span><span style="color:#007700;">);
                if (</span><span style="color:#0000BB;">is_resource</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">) &amp;&amp; (</span><span style="color:#0000BB;">mysql_num_rows</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">) !== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">)) {
                    </span><span style="color:#0000BB;">$data </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">,</span><span style="color:#0000BB;">0</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;sessionData&#34;</span><span style="color:#007700;">);
                }

                </span><span style="color:#FF8000;"># Refresh MC key: [Thanks Cal :-)]
                </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">set</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$data</span><span style="color:#007700;">,</span><span style="color:#0000BB;">false</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime</span><span style="color:#007700;">);
            }

            </span><span style="color:#FF8000;"># The default miss for MC is (bool) false, so return it
            </span><span style="color:#007700;">return </span><span style="color:#0000BB;">$data</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">write</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$data</span><span style="color:#007700;">) {
            </span><span style="color:#FF8000;"># This is called upon script termination or when session_write_close() is called, which ever is first.
            </span><span style="color:#0000BB;">$result </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">set</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$data</span><span style="color:#007700;">,</span><span style="color:#0000BB;">false</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime</span><span style="color:#007700;">);

            if (</span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">initSessionData </span><span style="color:#007700;">!== </span><span style="color:#0000BB;">$data</span><span style="color:#007700;">) {
                </span><span style="color:#0000BB;">$sessionID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_real_escape_string</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
                </span><span style="color:#0000BB;">$sessionExpirationTS </span><span style="color:#007700;">= (</span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime </span><span style="color:#007700;">+ </span><span style="color:#0000BB;">time</span><span style="color:#007700;">());
                </span><span style="color:#0000BB;">$sessionData </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_real_escape_string</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$data</span><span style="color:#007700;">);

                </span><span style="color:#0000BB;">$r </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;REPLACE INTO `tblsessions` (`sessionID`,`sessionExpirationTS`,`sessionData`) VALUES('$sessionID',$sessionExpirationTS,'$sessionData')&#34;</span><span style="color:#007700;">);
                </span><span style="color:#0000BB;">$result </span><span style="color:#007700;">= </span><span style="color:#0000BB;">is_resource</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">);
            }

            return </span><span style="color:#0000BB;">$result</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">destroy</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">) {
            </span><span style="color:#FF8000;"># Called when a user logs out...
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">delete</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
            </span><span style="color:#0000BB;">$sessionID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_real_escape_string</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sessionID</span><span style="color:#007700;">);
            </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;DELETE FROM `tblsessions` WHERE `sessionID`='$sessionID'&#34;</span><span style="color:#007700;">);

            return </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
        }

        function </span><span style="color:#0000BB;">gc</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$maxlifetime</span><span style="color:#007700;">) {
            </span><span style="color:#FF8000;"># We need this atomic so it can clear MC keys as well...
            </span><span style="color:#0000BB;">$r </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SELECT `sessionID` FROM `tblsessions` WHERE `sessionExpirationTS`&lt;&#34; </span><span style="color:#007700;">. (</span><span style="color:#0000BB;">time</span><span style="color:#007700;">() - </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">lifeTime</span><span style="color:#007700;">));
            if (</span><span style="color:#0000BB;">is_resource</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">) &amp;&amp; ((</span><span style="color:#0000BB;">$rows </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_num_rows</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">)) !== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">)) {
                for (</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">=</span><span style="color:#0000BB;">0</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">&lt;</span><span style="color:#0000BB;">$rows</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">++) {
                    </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">destroy</span><span style="color:#007700;">(</span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;sessionID&#34;</span><span style="color:#007700;">));
                }
            }

            return </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
        }
    }

    </span><span style="color:#0000BB;">ini_set</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;session.gc_maxlifetime&#34;</span><span style="color:#007700;">,</span><span style="color:#0000BB;">60 </span><span style="color:#007700;">* </span><span style="color:#0000BB;">30</span><span style="color:#007700;">); </span><span style="color:#FF8000;"># 30 minutes
    </span><span style="color:#0000BB;">session_set_cookie_params</span><span style="color:#007700;">(</span><span style="color:#0000BB;">0</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;/&#34;</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;.myapp.com&#34;</span><span style="color:#007700;">,</span><span style="color:#0000BB;">false</span><span style="color:#007700;">,</span><span style="color:#0000BB;">true</span><span style="color:#007700;">);
    </span><span style="color:#0000BB;">session_name</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;MYAPPSESSION&#34;</span><span style="color:#007700;">);
    </span><span style="color:#0000BB;">$sessionHandler </span><span style="color:#007700;">= new </span><span style="color:#0000BB;">SessionHandler</span><span style="color:#007700;">();
    </span><span style="color:#0000BB;">session_set_save_handler</span><span style="color:#007700;">(array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;open&#34;</span><span style="color:#007700;">),array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;close&#34;</span><span style="color:#007700;">),array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;read&#34;</span><span style="color:#007700;">),array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;write&#34;</span><span style="color:#007700;">),array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;destroy&#34;</span><span style="color:#007700;">),array (&amp;</span><span style="color:#0000BB;">$sessionHandler</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;gc&#34;</span><span style="color:#007700;">));
    </span><span style="color:#0000BB;">session_start</span><span style="color:#007700;">();
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>And here&#8217;s the SQL for the DB portion of the Session Handler:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
CREATE TABLE `tblsessions` (
    `sessionID` VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
    `sessionExpirationTS` INT(10) UNSIGNED NOT NULL,
    `sessionData` TEXT COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`sessionID`),
    KEY `sessionExpirationTS` (`sessionExpirationTS`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
</span>
</code></pre>
<p>As always, I welcome any and all CONSTRUCTIVE criticism. If you have something to add, or a bug fix or any suggestions I fully welcome them here. Trolls need not apply.</p>
<br />Posted in Memcache, mysql+, PHP Tagged: mysql+, PHP, scalability, sessions, write through cache <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=38&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2009/04/08/memcache-mysql-php-session-handler/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Image Resize Calculator Function</title>
		<link>http://pureform.wordpress.com/2009/01/08/simple-image-resize-calculator-function/</link>
		<comments>http://pureform.wordpress.com/2009/01/08/simple-image-resize-calculator-function/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 01:11:47 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[GD]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=35</guid>
		<description><![CDATA[Here is a simple function to calculate the width and height of an image given the constraints you want the image to fit in. This is great for creating thumbnails which need to fit into a specific height / width. To call the function, pass in the current height / width of the image you&#8217;d [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=35&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Here is a simple function to calculate the width and height of an image given the constraints you want the image to fit in. This is great for creating thumbnails which need to fit into a specific height / width.</p>
<p>To call the function, pass in the current height / width of the image you&#8217;d like to resize along with the constrain dimensions of the final product. In this case, I&#8217;d like the thumbnail to <em>fit within</em> the constraints 125px X 125px. So, no matter what the dimensions returned will be within those confines.</p>
<p>If you don&#8217;t pass along a <code>$constraintWidth</code> or  <code>$constraintHeight</code> [or pass them as <code>0</code>], they will be set to the <code>$width</code> / <code>$height</code>.</p>
<p>Also, all parameters are assumed to be of the strict type <strong><code>int</code></strong>.</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">getImageResizeDimensions</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$width</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$height</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">= </span><span style="color:#0000BB;">0</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">= </span><span style="color:#0000BB;">0</span><span style="color:#007700;">) {
        </span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">= (</span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">=== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">) ? </span><span style="color:#0000BB;">$width </span><span style="color:#007700;">: </span><span style="color:#0000BB;">$constraintWidth</span><span style="color:#007700;">;
        </span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">= (</span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">=== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">) ? </span><span style="color:#0000BB;">$height </span><span style="color:#007700;">: </span><span style="color:#0000BB;">$constraintHeight</span><span style="color:#007700;">;

        if ((</span><span style="color:#0000BB;">$width </span><span style="color:#007700;">&gt; </span><span style="color:#0000BB;">$constraintWidth</span><span style="color:#007700;">) || (</span><span style="color:#0000BB;">$height </span><span style="color:#007700;">&gt; </span><span style="color:#0000BB;">$constraintHeight</span><span style="color:#007700;">)) {
            while ((</span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">$width</span><span style="color:#007700;">) || (</span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">$height</span><span style="color:#007700;">)) {
                if (</span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">$width</span><span style="color:#007700;">) {
                    </span><span style="color:#0000BB;">$height </span><span style="color:#007700;">= </span><span style="color:#0000BB;">intval</span><span style="color:#007700;">(</span><span style="color:#0000BB;">floor</span><span style="color:#007700;">(((</span><span style="color:#0000BB;">$constraintWidth </span><span style="color:#007700;">* </span><span style="color:#0000BB;">$height</span><span style="color:#007700;">) / </span><span style="color:#0000BB;">$width</span><span style="color:#007700;">)));
                    </span><span style="color:#0000BB;">$width </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$constraintWidth</span><span style="color:#007700;">;
                }
                if (</span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">$height</span><span style="color:#007700;">) {
                    </span><span style="color:#0000BB;">$width </span><span style="color:#007700;">= </span><span style="color:#0000BB;">intval</span><span style="color:#007700;">(</span><span style="color:#0000BB;">floor</span><span style="color:#007700;">(((</span><span style="color:#0000BB;">$constraintHeight </span><span style="color:#007700;">* </span><span style="color:#0000BB;">$width</span><span style="color:#007700;">) / </span><span style="color:#0000BB;">$height</span><span style="color:#007700;">)));
                    </span><span style="color:#0000BB;">$height </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$constraintHeight</span><span style="color:#007700;">;
                }
            }
        }

        </span><span style="color:#FF8000;"># Some super short || skinny images will return 0 for these. Make sure that doesn't happen!
        </span><span style="color:#007700;">if (</span><span style="color:#0000BB;">$width </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">1</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$width </span><span style="color:#007700;">= </span><span style="color:#0000BB;">1</span><span style="color:#007700;">;
        }
        if (</span><span style="color:#0000BB;">$height </span><span style="color:#007700;">&lt; </span><span style="color:#0000BB;">1</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$height </span><span style="color:#007700;">= </span><span style="color:#0000BB;">1</span><span style="color:#007700;">;
        }

        return array (</span><span style="color:#0000BB;">$width</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$height</span><span style="color:#007700;">);
    }

    </span><span style="color:#0000BB;">$imageURI </span><span style="color:#007700;">= </span><span style="color:#DD0000;">&#034;/path/to/image.jpg&#034;</span><span style="color:#007700;">;
    list (</span><span style="color:#0000BB;">$width</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$height</span><span style="color:#007700;">) = </span><span style="color:#0000BB;">getimagesize</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$imageURI</span><span style="color:#007700;">);
    list (</span><span style="color:#0000BB;">$newWidth</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$newHeight</span><span style="color:#007700;">) = </span><span style="color:#0000BB;">getImageResizeDimensions</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$width</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$height</span><span style="color:#007700;">,</span><span style="color:#0000BB;">125</span><span style="color:#007700;">,</span><span style="color:#0000BB;">125</span><span style="color:#007700;">);

    echo </span><span style="color:#DD0000;">&#034;newWidth: $newWidth\n&#034;</span><span style="color:#007700;">;
    echo </span><span style="color:#DD0000;">&#034;newHeight: $newHeight\n&#034;</span><span style="color:#007700;">;
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<br />Posted in GD, PHP  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=35&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2009/01/08/simple-image-resize-calculator-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Setting up your localhost as example.com</title>
		<link>http://pureform.wordpress.com/2008/06/14/setting-up-your-localhost-as-examplecom/</link>
		<comments>http://pureform.wordpress.com/2008/06/14/setting-up-your-localhost-as-examplecom/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 22:39:40 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[example.com]]></category>
		<category><![CDATA[hosts]]></category>
		<category><![CDATA[linux lamp]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[static ip]]></category>
		<category><![CDATA[wamp]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=15</guid>
		<description><![CDATA[Often times when people install WAMP [or LAMP] stacks on their machines, they&#8217;ll commonly use http://localhost/ to access their local server. I did so for years until I started working with dynamic sub domains and needed to use something other than localhost to access my server. Example.com [or example.org, example.net] is a special domain as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=15&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Often times when people install <a href="http://en.wikipedia.org/wiki/WAMP" target="_blank">WAMP</a> [or <a href="http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29" target="_blank">LAMP</a>] stacks on their machines, they&#8217;ll commonly use <code><a href="http://localhost/" rel="nofollow">http://localhost/</a></code> to access their local server. I did so for years until I started working with dynamic sub domains and needed to use something other than <code>localhost</code> to access my server.</p>
<p>Example.com [or example.org, example.net] is a special domain as defined in <a href="http://www.rfc.net/rfc2606.html" target="_blank">RFC 2606</a> used for, you guessed it, examples. These domains will never be available to the public, so there&#8217;s no need to worry about not being able to access the actual site in the future.</p>
<p><strong>Setting this up involves three main parts:</strong></p>
<ol>
<li>Set up a static IP on your machine.</li>
<li>Configuring Apache.</li>
<li>Editing your host file.</li>
</ol>
<p>&nbsp;</p>
<p><strong>Set up your static IP</strong><br />
There&#8217;s really no need to go into this. There are a ton of sites out there to help you set up your static IP:<br />
 &#8211; <a href="http://www.google.com/search?q=%22static+ip%22+windows" target="_blank">Windows</a><br />
 &#8211; <a href="http://www.google.com/search?q=%22static+ip%22+os+x" target="_blank">Appletosh</a><br />
 &#8211; <a href="http://www.google.com/search?q=%22static+ip%22+linux" target="_blank">Linux</a></p>
<p>&nbsp;</p>
<p><strong>Apache Config</strong><br />
Everyone will have their <code>httpd.conf</code> file set up differently, so use this as an example of how to accomplish this. Keep in mind this file fragment should appear at the <strong>bottom</strong> of <code>httpd.conf</code>. I have also included my SSL layer here as well. It&#8217;s not important for most installations, but I included to show how to use it with example.com.</p>
<p>The main theory behind this is to set up everything under a <a href="http://httpd.apache.org/docs/2.2/vhosts/examples.html" target="_blank">VirtualHost</a>:<br />
<strong>Note:</strong> <code>192.168.1.23</code> is my machine&#8217;s static IP, so be sure to replace it with YOUR static IP</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
# apache #
    NameVirtualHost *:80

# Dir structure #
    Listen 192.168.1.23:80
    ServerName 192.168.1.23:80

    &lt;VirtualHost *:80&gt;
        DocumentRoot &#34;S:/apache/htdocs/website1.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website2.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website3.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website4.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs&#34;
        ServerName www.example.com:80
    &lt;/VirtualHost&gt;

# PMA #
    &lt;VirtualHost *:80&gt;
        DocumentRoot &#34;S:/apache/htdocs/sql&#34;
        ServerName sql.example.com:80
    &lt;/VirtualHost&gt;

# SSL -- Don't include this if you don't use SSL #
    LoadModule ssl_module modules/mod_ssl.so
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    SSLMutex default
    SSLSessionCache none

    Listen www.example.com:443

    &lt;VirtualHost *:443&gt;
        DocumentRoot &#34;S:/apache/htdocs/website1.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website2.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website3.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs/website4.com/&#34;
#       DocumentRoot &#34;S:/apache/htdocs&#34;
        ServerName www.example.com:443

        SSLEngine On
        SSLCertificateFile S:/apache/conf/pfs.crt
        SSLCertificateKeyFile S:/apache/conf/pfs.key
        SetEnvIf User-Agent &#34;.*MSIE.*&#34; nokeepalive ssl-unclean-shutdown
        ErrorLog logs/ssl_www.example.com-error.log
    &lt;/VirtualHost&gt;
</span>
</code></pre>
<p><em>Some directories are commented out, this allows me to run multiple sites on the server root. I just un-comment the site I want to work on, comment out the rest, restart the server and I&#8217;m good to go.</em></p>
<p>&nbsp;</p>
<p><strong>Host Files</strong><br />
Host files are like mini, stripped-down DNS servers that sit on your machine. This is where you tell your computer, &#8220;When I type in <a href="http://www.example.com" rel="nofollow">http://www.example.com</a>, I want it to go to my local server, not the internet&#8221;. Once again, this is something widely available on the net:</p>
<p> &#8211; <a href="http://www.google.com/search?q=%22host+file%22+windows" target="_blank">Windows</a><br />
 &#8211; <a href="http://www.google.com/search?q=%22host+file%22+os+x" target="_blank">Appletosh</a><br />
 &#8211; <a href="http://www.google.com/search?q=%22host+file%22+linux" target="_blank">Linux</a></p>
<p>Once you have your host files editable, add this to the bottom [don't over-write existing entries!] &#8230; also be sure to replace: <code>192.168.1.23</code> with YOUR static IP:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
192.168.1.23    localhost
192.168.1.23    example.com
192.168.1.23    www.example.com
192.168.1.23    sql.example.com
</span>
</code></pre>
<p>Using your machine&#8217;s static IP over <code>127.0.0.1</code> will allow other machines on your net work to connect to your server via <a href="http://www.example.com" rel="nofollow">http://www.example.com</a> &#8230; but you&#8217;ll need to edit <strong>those</strong> machine&#8217;s host files as well .. just use the same info from above on all your machines. Also make sure your firewall allows communication on Apache&#8217;s ports [80, 443].</p>
<p><strong>You will more than likely have to restart your web browser or possibly your computer to get this to work.</strong></p>
<p><strong>DNS</strong><br />
Using a DNS server would eliminate the host file layer of this guide, but since I don&#8217;t use one, it&#8217;s not in this tutorial &#8230; but it would be ideal for larger companies or offices [after all, it's just me, myself and I in my company].</p>
<p>One thing I will openly admit about this tutorial is that I have very little experience configuring apache. everything you see here I found around the internet and played with it until it worked. With that said, if you are more knowledgeable with apache config and have something to add, PLEASE DO SO! :-)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=15&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/06/14/setting-up-your-localhost-as-examplecom/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Memcache with MySQL and PHP</title>
		<link>http://pureform.wordpress.com/2008/05/21/using-memcache-with-mysql-and-php/</link>
		<comments>http://pureform.wordpress.com/2008/05/21/using-memcache-with-mysql-and-php/#comments</comments>
		<pubDate>Wed, 21 May 2008 20:37:44 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[Memcache]]></category>
		<category><![CDATA[mysql+]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[memcahed]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=13</guid>
		<description><![CDATA[Memcache is a great caching tool available for nearly every scripting or programming environment. I use it with PHP to speed up some applications I have written by completely avoiding asking the database for information. I wanted a very clean way of implementing this in my various projects, with as little change to the existing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=13&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/" target="_blank">Memcache</a> is a great caching tool available for nearly every scripting or programming environment. I use it with PHP to speed up some applications I have written by completely avoiding asking the database for information. I wanted a very clean way of implementing this in my various projects, with as little change to the existing code as possible. Since memcache is an OBJECT caching system, you can&#8217;t simply drop your <a href="http://us.php.net/mysql_query" target="_blank">mysql_query</a> resource into memcache, ask for it at another time, and get your results back &#8230; Which is how I had originally thought it worked the first time I read about it. The good news is that it can also store Strings, Integers, Floats, Arrays etc &#8230; Below I have a few other things I&#8217;ve learned whilst using memcache as well as the caching script I use for database queries.</p>
<p><strong>What I&#8217;ve learned:</strong></p>
<ol>
<li>memcache is <strong>great</strong> for storing slow queries that return small data sets [1 - 50 results, depending on the average row weight]</li>
<li>memcache is <strong>not so great</strong> for any query that returns large data sets [100 - &#8734;, depending on the average row weight]</li>
</ol>
<p>&#8230; in fact, I&#8217;ve found that memcache can occasionally be slower than running high-yield queries again [that is, if you have your MySQL server caching queries as well]. It all really boils down to benchmarking the scripts yourself: test every situation with and without the cache! [the more realistic the DB load, the better]</p>
<p>Caching is faster? Yay! Cache it!<br />
DB query is faster? Yay! DON&#8217;T cache it!</p>
<p>So, basically, this isn&#8217;t a plug-and-play solution for ALL slow page loads / queries, just some of them.</p>
<p>Here&#8217;s the code I use to cache MySQL queries:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#FF8000;"># Connect to memcache:
    </span><span style="color:#007700;">global </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">$memcache </span><span style="color:#007700;">= new </span><span style="color:#0000BB;">Memcache</span><span style="color:#007700;">;

    </span><span style="color:#FF8000;"># Gets key / value pair into memcache ... called by mysql_query_cache()
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">getCache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$key</span><span style="color:#007700;">) {
        global </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">;
        return (</span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">) ? </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">get</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$key</span><span style="color:#007700;">) : </span><span style="color:#0000BB;">false</span><span style="color:#007700;">;
    }

    </span><span style="color:#FF8000;"># Puts key / value pair into memcache ... called by mysql_query_cache()
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">setCache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$key</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$object</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$timeout </span><span style="color:#007700;">= </span><span style="color:#0000BB;">60</span><span style="color:#007700;">) {
        global </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">;
        return (</span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">) ? </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">set</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$key</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$object</span><span style="color:#007700;">,</span><span style="color:#0000BB;">MEMCACHE_COMPRESSED</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$timeout</span><span style="color:#007700;">) : </span><span style="color:#0000BB;">false</span><span style="color:#007700;">;
    }

    </span><span style="color:#FF8000;"># Caching version of mysql_query()
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">mysql_query_cache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$linkIdentifier </span><span style="color:#007700;">= </span><span style="color:#0000BB;">false</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$timeout </span><span style="color:#007700;">= </span><span style="color:#0000BB;">60</span><span style="color:#007700;">) {
        if ((</span><span style="color:#0000BB;">$cache </span><span style="color:#007700;">= </span><span style="color:#0000BB;">getCache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">md5</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;mysql_query&#34; </span><span style="color:#007700;">. </span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">))) !== </span><span style="color:#0000BB;">false</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$cache </span><span style="color:#007700;">= </span><span style="color:#0000BB;">false</span><span style="color:#007700;">;
            </span><span style="color:#0000BB;">$r </span><span style="color:#007700;">= (</span><span style="color:#0000BB;">$linkIdentifier </span><span style="color:#007700;">!== </span><span style="color:#0000BB;">false</span><span style="color:#007700;">) ? </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$linkIdentifier</span><span style="color:#007700;">) : </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">);
            if (</span><span style="color:#0000BB;">is_resource</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">) &amp;&amp; ((</span><span style="color:#0000BB;">$rows </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_num_rows</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">)) !== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">)) {
                for (</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">=</span><span style="color:#0000BB;">0</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">&lt;</span><span style="color:#0000BB;">$rows</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">++) {
                    </span><span style="color:#0000BB;">$fields </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_num_fields</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">);
                    </span><span style="color:#0000BB;">$row </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_fetch_array</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">);
                    for (</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">=</span><span style="color:#0000BB;">0</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">&lt;</span><span style="color:#0000BB;">$fields</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">++) {
                        if (</span><span style="color:#0000BB;">$i </span><span style="color:#007700;">=== </span><span style="color:#0000BB;">0</span><span style="color:#007700;">) {
                            </span><span style="color:#0000BB;">$columns</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">] = </span><span style="color:#0000BB;">mysql_field_name</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">);
                        }
                        </span><span style="color:#0000BB;">$cache</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">][</span><span style="color:#0000BB;">$columns</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">]] = </span><span style="color:#0000BB;">$row</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$j</span><span style="color:#007700;">];
                    }
                }
                if (!</span><span style="color:#0000BB;">setCache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">md5</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;mysql_query&#34; </span><span style="color:#007700;">. </span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">),</span><span style="color:#0000BB;">$cache</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$timeout</span><span style="color:#007700;">)) {
                    </span><span style="color:#FF8000;"># If we get here, there isn't a memcache daemon running or responding
                </span><span style="color:#007700;">}
            }
        }
        return </span><span style="color:#0000BB;">$cache</span><span style="color:#007700;">;
    }
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>The function mysql_query_cache() will return an array filled with the results. Since I don&#8217;t use this for large result sets, I don&#8217;t free the MySQL resource &#8230; you may want to <a href="http://us2.php.net/mysql_free_result" target="_blank">free the resource</a> after it&#8217;s been used if you get larger data sets.</p>
<p>Like I had mentioned before, I wanted the code to be as easy-to-use as possible when using it. So, I&#8217;ve set up a before and after test scenario showing how to retrofit your code with the new caching code:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    $sql </span><span style="color:#007700;">= </span><span style="color:#DD0000;">&#34;
        SELECT `dataID`, `dataTitle`
        FROM `tbldata`
        WHERE `dataTypeID` BETWEEN 2 AND 2093
        AND `dataStatusID` IN (1,2,3,4)
        AND `dataTitle` LIKE '%something%'
        ORDER BY `dataDate` DESC
        LIMIT 10
    &#34;</span><span style="color:#007700;">;

    </span><span style="color:#FF8000;"># Before: [without memcache]
    </span><span style="color:#0000BB;">$rSlowQuery </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">);
    </span><span style="color:#FF8000;"># $rSlowQuery is a MySQL resource
    </span><span style="color:#0000BB;">$rows </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_num_rows</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">);
    for (</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">=</span><span style="color:#0000BB;">0</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">&lt;</span><span style="color:#0000BB;">$rows</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">++) {
        </span><span style="color:#0000BB;">$dataID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">intval</span><span style="color:#007700;">(</span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;dataID&#34;</span><span style="color:#007700;">));
        </span><span style="color:#0000BB;">$dataTitle </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;dataTitle&#34;</span><span style="color:#007700;">);

        echo </span><span style="color:#DD0000;">&#34;&lt;a href=\&#34;/somewhere/$dataID\&#34;&gt;$dataTitle&lt;/a&gt;&lt;br /&gt;\n&#34;</span><span style="color:#007700;">;
    }


    </span><span style="color:#FF8000;"># After: [with memcache]
    </span><span style="color:#0000BB;">$rSlowQuery </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query_cache</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$sql</span><span style="color:#007700;">);
    </span><span style="color:#FF8000;"># $rSlowQuery is an array
    </span><span style="color:#0000BB;">$rows </span><span style="color:#007700;">= </span><span style="color:#0000BB;">count</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">);
    for (</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">=</span><span style="color:#0000BB;">0</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">&lt;</span><span style="color:#0000BB;">$rows</span><span style="color:#007700;">;</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">++) {
        </span><span style="color:#0000BB;">$dataID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">intval</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">][</span><span style="color:#DD0000;">&#34;dataID&#34;</span><span style="color:#007700;">]);
        </span><span style="color:#0000BB;">$dataTitle </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$rSlowQuery</span><span style="color:#007700;">[</span><span style="color:#0000BB;">$i</span><span style="color:#007700;">][</span><span style="color:#DD0000;">&#34;dataTitle&#34;</span><span style="color:#007700;">];

        echo </span><span style="color:#DD0000;">&#34;&lt;a href=\&#34;/somewhere/$dataID\&#34;&gt;$dataTitle&lt;/a&gt;&lt;br /&gt;\n&#34;</span><span style="color:#007700;">;
    }

</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>Easy, huh? Run <a href="http://us3.php.net/print_r">print_r()</a> on the returned array to get an idea of how the array is structured if need be.</p>
<p>As always, if you have a better, more efficient, objective, more adaptable solution than mine, please leave a comment! I am 100% open to constructive criticism :-)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=13&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/05/21/using-memcache-with-mysql-and-php/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Use one DB connection on your custom wordpress install</title>
		<link>http://pureform.wordpress.com/2008/05/11/use-one-db-connection-on-your-custom-wordpress-install/</link>
		<comments>http://pureform.wordpress.com/2008/05/11/use-one-db-connection-on-your-custom-wordpress-install/#comments</comments>
		<pubDate>Mon, 12 May 2008 01:30:50 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[mysql+]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[mysql php wordpress]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=12</guid>
		<description><![CDATA[Like most people who use WordPress, I have hacked a few installs 6 ways from Sunday for some of my projects. While digging through my hacks I had noticed that the script was connecting to my database TWICE per client &#8230; once for my website it&#8217;s self, then once for WordPress&#8217; needs. This seemed like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=12&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Like most people who use WordPress, I have hacked a few installs 6 ways from Sunday for some of my projects. While digging through my hacks I had noticed that the script was connecting to my database TWICE per client &#8230; once for my website it&#8217;s self, then once for WordPress&#8217; needs. This seemed like an awful waste especially considering how much traffic the site can get &#8230; so why not <em>share</em> the connection resource [while effectively DOUBLING the amount of people who can connect to your blog -- since <code>2 - 1 = 1</code>]??</p>
<p>This is how I hacked WordPress to reuse an existing DB connection &#8230;</p>
<p><strong>WordPress, meet my web site</strong><br />
I started with the root of the script, <code>./index.php</code>, and added in my global include file [<code>.gfl</code> == global function library, which, among other things, connects to the DB for me]. This way no matter what WordPress does on the front end, it will also have all my web site&#8217;s functions as well.</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
</span><span style="color:#007700;">require(</span><span style="color:#DD0000;">&#34;../global.gfl&#34;</span><span style="color:#007700;">);
</span><span style="color:#FF8000;">/* Short and sweet */
</span><span style="color:#0000BB;">define</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#39;WP_USE_THEMES&#39;</span><span style="color:#007700;">, </span><span style="color:#0000BB;">true</span><span style="color:#007700;">);
require(</span><span style="color:#DD0000;">&#39;./wp-blog-header.php&#39;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>&nbsp;</p>
<p><strong>Now play nice!</strong><br />
After changing my <a href="http://php.net/mysql_connect">mysql_connect</a> resource to a global variable:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">global </span><span style="color:#0000BB;">$dbResource</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">$dbResource </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_connect</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;host&#34;</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;user&#34;</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;pass&#34;</span><span style="color:#007700;">);
    </span><span style="color:#0000BB;">mysql_select_db</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;db&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>I did a quick search for the string &#34;<a href="http://php.net/mysql_connect">mysql_connect</a>&#34; within my WordPress directory I found it living in <code>./wp-includes/wp-db.php</code>. I changed &#8230;</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">__construct</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$dbuser</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbpassword</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbname</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbhost</span><span style="color:#007700;">) {
        </span><span style="color:#FF8000;"># Code omitted

        </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">dbh </span><span style="color:#007700;">= @</span><span style="color:#0000BB;">mysql_connect</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$dbhost</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbuser</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbpassword</span><span style="color:#007700;">, </span><span style="color:#0000BB;">true</span><span style="color:#007700;">);

        </span><span style="color:#FF8000;"># Code omitted
    </span><span style="color:#007700;">}
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>&#8230; to &#8230;</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">function </span><span style="color:#0000BB;">__construct</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$dbuser</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbpassword</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbname</span><span style="color:#007700;">, </span><span style="color:#0000BB;">$dbhost</span><span style="color:#007700;">, </span><span style="color:#0000BB;">true</span><span style="color:#007700;">) {
        global </span><span style="color:#0000BB;">$dbResource</span><span style="color:#007700;">;

        if (</span><span style="color:#0000BB;">is_resource</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$dbResource</span><span style="color:#007700;">)) {
            </span><span style="color:#FF8000;"># For the front-end ...
            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">dbh </span><span style="color:#007700;">= </span><span style="color:#0000BB;">$dbResource</span><span style="color:#007700;">;
        } else {
            </span><span style="color:#FF8000;"># For wp-admin ...
            # Code omitted

            </span><span style="color:#0000BB;">$this</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">dbh </span><span style="color:#007700;">= @</span><span style="color:#0000BB;">mysql_connect</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$dbhost</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$dbuser</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$dbpassword</span><span style="color:#007700;">);

            </span><span style="color:#FF8000;"># Code omitted
        </span><span style="color:#007700;">}
    }
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>Simple enough!</p>
<p>Comments, critiques, suggestions, recommendations, changes, etc etc etc are always welcome!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=12&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/05/11/use-one-db-connection-on-your-custom-wordpress-install/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Make your website completely UTF-8 friendly</title>
		<link>http://pureform.wordpress.com/2008/03/23/make-your-website-completely-utf-8-friendly/</link>
		<comments>http://pureform.wordpress.com/2008/03/23/make-your-website-completely-utf-8-friendly/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 06:59:32 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[mysql+]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[UTF-8]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[Localization]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=11</guid>
		<description><![CDATA[LAST UPDATED JUNE 15, 2009 Running an Internationalization / Localization [or i18n / L10n] friendly website can be tricky, and sometimes downright maddening for those who haven&#8217;t yet delved into the world of Unicode. Allowing your users to post in whichever language and / or characters of their choice to your site is crucial for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=11&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-weight:bold;color:#cc3333;"><em>LAST UPDATED JUNE 15, 2009</em></span></p>
<p>Running an Internationalization / Localization [or i18n / L10n] friendly website can be tricky, and sometimes downright maddening for those who haven&#8217;t yet delved into the world of Unicode. Allowing your users to post in whichever language and / or characters of their choice to your site is crucial for any modern website.</p>
<p>Here are a few things I have very painfully learned over the last 5 or so years on this topic &#8230; specifically with PHP and MySQL.</p>
<p>There are hundreds of character sets representing most of the languages on Earth, usually one per geographic location [Latin, Cyrillic, Greek, Arabic, Korean, Chinese etc...]. One character set that covers all of these is <code>UTF-8</code>. So how can you put &#8216;<code>UTF-8</code>&#8216; to practical use? Easy &#8230; here&#8217;s how I&#8217;ve done it:</p>
<p>&nbsp;</p>
<p><strong>Headers! Get your headers!</strong><br />
The most important area to implement <code>UTF-8</code> is in your <code>charset</code> header within your outgoing HTML headers. This tells the browser that you have multi-byte characters in your HTML and you&#8217;d like it do display them as such [and not as the default <code>ISO-8859-1</code>].<br />
To do this, put this at the very top of your PHP scripts [with the headers and before any HTML is echoed]:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    header</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;Content-Type: text/html; charset=utf-8&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span></code></pre>
<p>And this in your HTML &lt;head&gt; section:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    </span><span style="color:#007700;">echo </span><span style="color:#DD0000;">&#34;&lt;meta http-equiv=\&#34;Content-Type\&#34; content=\&#34;text/html; charset=utf-8\&#34; /&gt;\n&#34;</span><span style="color:#007700;">;
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>&nbsp;</p>
<p><strong>MySQL / UTF-8 love</strong><br />
The second most important thing is to make sure your database is also <code>UTF-8</code> friendly. Be sure to set all your table / column collations [char / text] to <code>utf8_unicode_ci</code>. This tells MySQL to treat this data as UTF-8.</p>
<p>Once you&#8217;ve done that, you&#8217;ll need to tell PHP to connect to the MySQL daemon under a <code>UTF-8</code> connection [otherwise the default is <code>latin1</code> ... and your data will be stored in MySQL as such -- no good!]. Run this right after you connect to MySQL:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SET NAMES &#39;utf8&#39;&#34;</span><span style="color:#007700;">);</span><span style="color:#0000BB;">
    mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SET CHARACTER SET utf8&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>&nbsp;</p>
<p><strong>Multibyte fun</strong><br />
Last, take advantage of PHP&#8217;s <a href="http://www.php.net/manual/en/ref.mbstring.php" target="_blank">Multibyte String Functions</a>! Oftentimes this is as easy as prefixing your string comparison functions with <code>mb_</code>. But, before you start using these functions you&#8217;ll need to tell PHP which character set to use [once again!] because the default is <code>ISO-8859-1</code>:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    mb_internal_encoding</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;UTF-8&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>&nbsp;</p>
<p><strong>Forms</strong><br />
One often neglected method is ensuring that the data the server gets is UTF-8 encoded. One way to try and do this with HTML forms is to include the <code>accept-charset</code> attribute in your form tag. I say &#8220;try&#8221; because it&#8217;s just a suggestion to the client which submits the form. Be aware that some clients may not pay much attention to the attribute, especially older browsers. [Thanks to <a href="http://htmlentities.blogspot.com/" target="_blank">Alejandro</a> for the heads up :-)]</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
&lt;form action=&#34;/action&#34; method=&#34;post&#34; accept-charset=&#34;utf-8&#34;&gt;</span>
</code>
</pre>
<p>Also see here: <a href="http://www.w3schools.com/TAGS/att_form_accept_charset.asp" target="_blank">www.w3schools.com/TAGS/att_form_accept_charset.asp</a>.</p>
<p>If you&#8217;ve gotten this far you should see some dramatic improvements to your web site&#8217;s accessibility and usability, drawing in users from around the world.</p>
<p><strong>NOTE:</strong> This is a work in progress and I fully welcome any new ideas to this cocktail of methods. If you have anything to add, PLEASE DO SO!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=11&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/03/23/make-your-website-completely-utf-8-friendly/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Never use ORDER BY RAND() again!</title>
		<link>http://pureform.wordpress.com/2008/03/05/never-use-order-by-rand-again/</link>
		<comments>http://pureform.wordpress.com/2008/03/05/never-use-order-by-rand-again/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 00:27:59 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[mysql+]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[order by rand]]></category>
		<category><![CDATA[random record]]></category>
		<category><![CDATA[random result]]></category>
		<category><![CDATA[random row]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/?p=10</guid>
		<description><![CDATA[I have been guilty of using ORDER BY RAND() in MySQL queries to return random records from time to time, but everyone knows to avoid it like the plague. For those who agree with me, aside from using it on very small tables [1 - 500 records], I have an elegant workaround using PHP. &#60;?php [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=10&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I have been guilty of using <strong>ORDER BY RAND()</strong> in MySQL queries to return random records from time to time, but everyone knows to avoid it like the plague. For those who agree with me, aside from using it on very small tables [1 - 500 records], I have an elegant workaround using PHP.</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    $condition </span><span style="color:#007700;">= </span><span style="color:#0000BB;">true</span><span style="color:#007700;">;
    while (</span><span style="color:#0000BB;">$condition</span><span style="color:#007700;">) {
        </span><span style="color:#0000BB;">$randID </span><span style="color:#007700;">= </span><span style="color:#0000BB;">rand</span><span style="color:#007700;">(</span><span style="color:#0000BB;">1</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$totalRecordsInTblExample</span><span style="color:#007700;">);
        </span><span style="color:#0000BB;">$r </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SELECT * FROM `tblexample` WHERE `exampleID`=$randID LIMIT 1&#34;</span><span style="color:#007700;">);
        if (</span><span style="color:#0000BB;">mysql_num_rows</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">) == </span><span style="color:#0000BB;">1</span><span style="color:#007700;">) {
            </span><span style="color:#0000BB;">$condition </span><span style="color:#007700;">= </span><span style="color:#0000BB;">false</span><span style="color:#007700;">;
        }
    }
    </span><span style="color:#0000BB;">$exampleTitle </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">,</span><span style="color:#0000BB;">0</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;exampleTitle&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>The very first thing we need to know is how many records there are within the table in question, if the table is MyISAM, you&#8217;re in luck. Getting the total number of records is quick and inexpensive since MyISAM keeps an index of how many records a given table contains:</p>
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    $r </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_query</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#34;SELECT COUNT(*) AS `count` FROM `tblexample`&#34;</span><span style="color:#007700;">);
    </span><span style="color:#0000BB;">$numRecords </span><span style="color:#007700;">= </span><span style="color:#0000BB;">mysql_result</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$r</span><span style="color:#007700;">,</span><span style="color:#0000BB;">0</span><span style="color:#007700;">,</span><span style="color:#DD0000;">&#34;count&#34;</span><span style="color:#007700;">);
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
<p>However, don&#8217;t attempt this on an InnoDB table! InnoDB would need to count each and every row every time, which <em>would</em> be expensive on large tables. I keep a reference of record counts in a MyISAM table [for stats purposes], so I query that table to get the record count instead.</p>
<p>Once we have determined how many records are in the table, we can use PHP to [duh] select a random number between 1 and the total number of records.</p>
<p>Once we have that random number, query the DB to see if the record exists, and if not try again. Since the column in the where clause is the Primary Key [I assume], the lookups will be very fast and cheap.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=10&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/03/05/never-use-order-by-rand-again/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
		<item>
		<title>Installing memcache on Windows for PHP</title>
		<link>http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/</link>
		<comments>http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/#comments</comments>
		<pubDate>Thu, 10 Jan 2008 20:43:05 +0000</pubDate>
		<dc:creator>PureForm</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/</guid>
		<description><![CDATA[LAST UPDATED SEPTEMBER 11, 2010 Installing memcache on Windows XP / Vista is kind of like voodoo for those of us who are not disciplined with compiling code from source. I initially attempted to install memcache a few months ago after reading a few articles about how much performance it can pump into your web [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=9&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><span style="font-weight:bold;color:#cc3333;"><em>LAST UPDATED SEPTEMBER 11, 2010</em></span></p>
<p>Installing <a href="http://www.danga.com/memcached/" target="_blank">memcache</a> on Windows XP / Vista is kind of like voodoo for those of us who are not disciplined with compiling code from source. I initially attempted to install memcache a few months ago after reading a few articles about how much performance it can pump into your web application. The problem is that memcache was written with Linux in mind, not windows. So you can&#8217;t download any installers or exe files from memcache&#8217;s site for windows &#8230; which leaves people like me, who use WAMP stacks to develop applications, out in the cold.</p>
<p>So after a few hours of Googling I found a cocktail of methods and files to get memcache to work for win32.</p>
<p><strong>A few things about memcache:</strong><br />
Memcache is a <a href="http://en.wikipedia.org/wiki/Daemon_(computer_software)" target="_blank">daemon</a>, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to <a href="http://www.php.net/manual/en/function.Memcache-connect.php" target="_blank">connect to it</a>, just like MySQL.</p>
<p>Think of memcache as the <code>$_SESSION</code> variable for PHP, but instead of it working on a per-user basis, it runs over the entire application &#8212; like MySQL. In fact, you can use memcache as you session handler for PHP.</p>
<p>&nbsp;</p>
<p><strong>This is how I got memcache to work on my windows machine:</strong></p>
<ol>
<li>Download memcache from <a href="http://code.jellycan.com/memcached/" target="_blank">code.jellycan.com/memcached/</a> [grab the 'win32 binary' version]</li>
<li>Install memcache as a service:
<ul>
<li>Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, <em>memcached.exe</em>] &#8211; thanks to <a href="http://www.guildlaunch.com/" target="_blank">Stephen</a> for the heads up on the new version</li>
<li>If you&#8217;re running Vista, right click on <strong>memcached.exe</strong> and click Properties. Click the Compatibility tab. Near the bottom you&#8217;ll see <strong>Privilege Level</strong>, check &#8220;Run this program as an administrator&#8221;.</li>
<li>Install the service using the command: <strong>c:\memcached\memcached.exe -d install</strong> from the command line</li>
<li>Start the server from the Microsoft Management Console or by running one of the following commands: <strong>c:\memcached\memcached.exe -d start</strong>, or <strong>net start &#034;memcached Server&#034;</strong></li>
</ul>
</li>
</ol>
<p>&nbsp;</p>
<p><strong>Now that you have memcache installed, you&#8217;ll have to tie it in with PHP in order to use it.</strong></p>
<ol>
<li>
		Check your php extensions directory [should be something like: <strong>C:\php\ext</strong>] for <em>php_memcache.dll</em><br />
		If you don&#8217;t have any luck finding it, try looking at one of these sites:<br />
		- <a href="http://downloads.php.net/pierre/" target="_blank">downloads.php.net/pierre/</a> [thanks to <a href="http://gemal.dk/" target="_blank">Henrik Gemal</a>]<br />
		- <a href="http://pecl4win.php.net/ext.php/php_memcache.dll" target="_blank">pecl4win.php.net/ext.php/php_memcache.dll</a> [currently down]<br />
		- <a href="http://www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll" target="_blank">www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll</a> for PHP 5.2.*<br />
		- <a href="http://kromann.info/download.php?strFolder=php5_1-Release_TS&amp;strIndex=PHP5_1" target="_blank">kromann.info/download.php?strFolder=php5_1-Release_TS&amp;strIndex=PHP5_1</a> for PHP 5.1.* [thanks, Rich]
	</li>
<li>Now find your php.ini file [default location for XP Pro is <em>C:\WINDOWS\php.ini</em>] and add this line to the extensions list:
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
extension=php_memcache.dll</span>

</code></pre>
</li>
<li>Restart apache</li>
<li>Run this code to test the installation: [found on <a href="http://www.php.net/memcache" target="_blank">www.php.net/memcache</a>]
<pre style="background:#eeeeee;overflow:auto;border:4px solid #dddddd;font-size:1em;padding:0 15px;">
<code><span style="color:#000000;">
<span style="color:#0000BB;">&lt;?php
    $memcache </span><span style="color:#007700;">= new </span><span style="color:#0000BB;">Memcache</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">connect</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#034;localhost&#034;</span><span style="color:#007700;">,</span><span style="color:#0000BB;">11211</span><span style="color:#007700;">); </span><span style="color:#FF8000;"># You might need to set &#034;localhost&#034; to &#034;127.0.0.1&#034;

    </span><span style="color:#007700;">echo </span><span style="color:#DD0000;">&#034;Server's version: &#034; </span><span style="color:#007700;">. </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">getVersion</span><span style="color:#007700;">() . </span><span style="color:#DD0000;">&#034;&lt;br /&gt;\n&#034;</span><span style="color:#007700;">;

    </span><span style="color:#0000BB;">$tmp_object </span><span style="color:#007700;">= new </span><span style="color:#0000BB;">stdClass</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">$tmp_object</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">str_attr </span><span style="color:#007700;">= </span><span style="color:#DD0000;">&#034;test&#034;</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">$tmp_object</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">int_attr </span><span style="color:#007700;">= </span><span style="color:#0000BB;">123</span><span style="color:#007700;">;

    </span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">set</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#034;key&#034;</span><span style="color:#007700;">,</span><span style="color:#0000BB;">$tmp_object</span><span style="color:#007700;">,</span><span style="color:#0000BB;">false</span><span style="color:#007700;">,</span><span style="color:#0000BB;">10</span><span style="color:#007700;">);
    echo </span><span style="color:#DD0000;">&#034;Store data in the cache (data will expire in 10 seconds)&lt;br /&gt;\n&#034;</span><span style="color:#007700;">;

    echo </span><span style="color:#DD0000;">&#034;Data from the cache:&lt;br /&gt;\n&#034;</span><span style="color:#007700;">;
    </span><span style="color:#0000BB;">var_dump</span><span style="color:#007700;">(</span><span style="color:#0000BB;">$memcache</span><span style="color:#007700;">-&gt;</span><span style="color:#0000BB;">get</span><span style="color:#007700;">(</span><span style="color:#DD0000;">&#034;key&#034;</span><span style="color:#007700;">));
</span><span style="color:#0000BB;">?&gt;</span>
</span>
</code></pre>
</li>
</ol>
<p>If you see anything but errors, you are now using memcache!</p>
<p><strong>EDIT</strong></p>
<p>Memcached, by default, loads with 64mb of memory for it&#8217;s use which is low for most applications. To change this to something else, navigate to <strong>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server</strong> in your registry, find the <strong>ImagePath</strong> entry and change it to look something like this:</p>
<p><em>&#8220;C:\memcached\memcached.exe&#8221; -d runservice -m 512</em></p>
<p>Now when you start the service via net start &#8220;memcached Server&#8221;, it will run with 512mb of memory at it&#8217;s disposal.</p>
<p><strong>EDIT</strong></p>
<p>[Thanks to <a href="#comment-516">Travis</a>]<br />
If anyone is wondering what other options can be set (other than the memory limit), run “memcached -help” in a command prompt window. Then modify the ImagePath command line per this article with the desired switches and values.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pureform.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pureform.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pureform.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pureform.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pureform.wordpress.com&#038;blog=2417257&#038;post=9&#038;subd=pureform&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/feed/</wfw:commentRss>
		<slash:comments>167</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1f8259e0e51b4768a7b0c4941c406df7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">PureForm</media:title>
		</media:content>
	</item>
	</channel>
</rss>
