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

<channel>
	<title>Brettic.us &#187; Brett</title>
	<atom:link href="http://brettic.us/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettic.us</link>
	<description>Web devlopment and life in general</description>
	<lastBuildDate>Tue, 13 Dec 2011 02:59:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP 5.2 and 5.3 side-by-side on Apache and Ubuntu 10.10</title>
		<link>http://brettic.us/2010/10/31/php-5-2-and-5-3-on-apache/</link>
		<comments>http://brettic.us/2010/10/31/php-5-2-and-5-3-on-apache/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 07:14:37 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=510</guid>
		<description><![CDATA[<br/>Painlessly run version 5.2.x and 5.3.x on the same apache port simultaneously. Several persons have indicated that they could not get this working per my instructions below. For a more thorough explanation, see Chris Breidert&#8217; s follow-up blog post. One of these days, when I get more time, I&#8217;ll go through a vanilla installation and add any additional [...]]]></description>
			<content:encoded><![CDATA[<br/><h4>Painlessly run version 5.2.x and 5.3.x on the same apache port simultaneously.</h4>
<blockquote><p>Several persons have indicated that they could not get this working per my instructions below. For a more thorough explanation, see <a href="http://blog.breidert.net/php-5-2-and-php-5-3-for-drupal-applications-under-apache-on-ubuntu-10-04-lts/" target="_blank">Chris Breidert&#8217; s follow-up blog post</a>. One of these days, when I get more time, I&#8217;ll go through a vanilla installation and add any additional instructions, etc.</p></blockquote>
<p>I&#8217;d just upgraded to Ubuntu 10.10 on my PC. However, the default PHP package was version 5.3.x and I needed 5.2.x. Some projects needed the newest version, others would be crippled by it. While looking for a way to have two versions of PHP running simultaneously on Ubuntu 10.10, I struck success.</p>
<p>In this post, I&#8217;ll explain step-by-step how you too can  build a development platform where you can work on PHP 5.2.x and 5.3.x projects simultaneously&#8211;without <em>ever</em> having to restart Apache.<span id="more-510"></span></p>
<h2>Why?</h2>
<p>My primary development computer is a Macbook Pro, but sometimes I like to change things up and work from my PC installed with the latest version of Ubuntu (10.10 at the time of writing.) Ubuntu 10.10 (A.K.A Maverick Meerkat) comes with PHP 5.3.x (currently 5.3.3.)</p>
<p>While using version 5.3.x, I noticed that some of my development projects were broken (like my website that needs curl!) On the other hand, I have another development project that is &#8220;cutting-edge&#8221; and must run PHP 5.3.x. I couldn&#8217;t just downgrade to PHP 5.2.x (which seemed to be the bulk of solutions offered for my predicament.)</p>
<p>Switching versions is easy on my Mac, thanks to MAMP. I just swap versions through the MAMP control panel. However, I really wanted the same flexibility on Ubuntu. Not only did I find a solution, I found one that I prefer to MAMP because I can run either version <strong>simultaneously</strong>. Here&#8217;s how I did it.</p>
<h2>Step 1: Get PHP source code.</h2>
<p>Download the most current version of PHP 5.2.x (5.2.14 at the time of writing.)</p>
<p><a href="http://us3.php.net/downloads.php" target="_blank">http://us.php.net/downloads.php</a></p>
<p>You can download this to anywhere like ~/src or wherever. Extract it with tar xzvf php-file (for .tar.gz variant) or tar xjvf php-file  (for .tar.bz variant) depending on which version you downloaded. Change directories into the folder you just extracted.</p>
<h2>Step 2: Install PHP development libraries.</h2>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">sudo</span> <span class="kw2">apt-get</span> <span class="kw2">install</span> libxml2-dev libmysqlclient-dev libcurl4-gnutls-dev libcurl4-openssl-dev libpng12-dev libjpeg62-dev</pre></div></div>
<p>Credit to <a href="http://webpagedeveloper.me/blog/ubuntu-1004-lucid-lamp-server-running-php-52-and-53" target="_blank">Dave Parrish</a> for the dev packages and configure options, plus most of the other information here.</p>
<h2>Step 3: Configure and make.</h2>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash">.<span class="sy0">/</span>configure <span class="re5">--prefix</span>=<span class="sy0">/</span>opt<span class="sy0">/</span>php5.2 \
<span class="re5">--with-config-file-path</span>=<span class="sy0">/</span>opt<span class="sy0">/</span>php5.2 \
<span class="re5">--with-mysqli</span> \
<span class="re5">--with-mysql</span> \
<span class="re5">--with-curl</span> \
<span class="re5">--with-gd</span> \
<span class="re5">--with-jpeg</span> \
<span class="re5">--with-jpeg-dir</span> \
<span class="re5">--enable-cli</span> \
<span class="re5">--enable-fastcgi</span> \
<span class="re5">--enable-discard-path</span> \
<span class="re5">--enable-force-cgi-redirect</span></pre></div></div>
<div>If everything works, go ahead and make the file.</div>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">make</span> <span class="sy0">&amp;&amp;</span> <span class="kw2">sudo</span> <span class="kw2">make</span> <span class="kw2">install</span></pre></div></div>
<div>You should end up with php files in /opt/php5.2 if everything went without errors.</div>
<h2>Step 4: Setup fast-cgi.</h2>
<p>Just install the Debian package.</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">sudo</span> <span class="kw2">apt-get</span> <span class="kw2">install</span> libapache2-mod-fastcgi</pre></div></div>
<p>To enable the module, run:</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">sudo</span> a2enmod fastcgi</pre></div></div>
<p>Restart Apache.</p>
<p><strong> </strong></p>
<h2>Step 5: Enable mod_actions.</h2>
<p>mod_actions was already available for me to use. I simply had to enable it:</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">sudo</span> a2enmod actions</pre></div></div>
<p>Restart Apache.</p>
<h2>Step 6: Setup php5-cgi wrapper.</h2>
<p>Create a /usr/lib/cgi-bin/php5-cgi file:</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="co0">#!/bin/sh</span>
<span class="re2">PHP_FCGI_CHILDREN</span>=<span class="nu0">1</span>
<span class="kw3">export</span> PHP_FCGI_CHILDREN
<span class="re2">PHP_FCGI_MAX_REQUESTS</span>=<span class="nu0">5000</span>
<span class="kw3">export</span> PHP_FCGI_MAX_REQUESTS
<span class="kw3">exec</span> <span class="sy0">/</span>opt<span class="sy0">/</span>php5.2<span class="sy0">/</span>bin<span class="sy0">/</span>php-cgi</pre></div></div>
<p>Also ripped off from <a href="http://webpagedeveloper.me/blog/ubuntu-1004-lucid-lamp-server-running-php-52-and-53" target="_blank">Dave Parrish</a>.</p>
<p>Make your wrapper executable:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="kw2">sudo</span> <span class="kw2">chmod</span> +x <span class="sy0">/</span>usr<span class="sy0">/</span>lib<span class="sy0">/</span>cgi-bin<span class="sy0">/</span>php5-cgi</pre></div></div>
<h2>Step 7: Make an Include file for VirtualHosts.</h2>
<p>I made a simple include file under /etc/apache2/ called php52.conf:</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash"><span class="co0">#include for sites that still need to run at php 5.2.x</span>
&nbsp;
ScriptAlias <span class="sy0">/</span>php5-cgi <span class="sy0">/</span>usr<span class="sy0">/</span>lib<span class="sy0">/</span>cgi-bin<span class="sy0">/</span>php5-cgi
&nbsp;
Action application<span class="sy0">/</span>x-httpd-php5 <span class="sy0">/</span>php5-cgi
AddHandler application<span class="sy0">/</span>x-httpd-php5 .php .php5 .php4 .php3 .phtml</pre></div></div>
<p>Credit to <a href="http://ubuntuforums.org/showthread.php?t=159301&amp;page=2#td_post_2625095" target="_blank">jameso</a> for this pearl.</p>
<h2>Step 8: Include php52.conf in all VirtualHosts where PHP 5.2.x is needed.</h2>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="apache">&lt;<span class="kw3">VirtualHost</span> *.80&gt;
&nbsp;
<span class="co1"># ...</span>
&nbsp;
<span class="co1">#needs to run version 5.2.x of PHP</span>
<span class="kw1">Include</span> php52.conf
&nbsp;
&lt;/<span class="kw3">VirtualHost</span>&gt;</pre></div></div>
<p>That&#8217;s it! Now I can simply insert my Include directive for any websites that I&#8217;m developing that are not ready for PHP 5.3.</p>
<blockquote><p><strong>Important</strong>. I am using VirtualHost configurations. This allows me to leave PHP 5.3.x enabled globally via the Apache module and turn on PHP 5.2.x specifically for each vhost. If you develop in the typical way of accessing a website via http://localhost/someproject, consider using the Apache Directory directive instead. It should work, but I haven&#8217;t tested.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/10/31/php-5-2-and-5-3-on-apache/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>CodeIgniter Form Validation: How to allow inline and config rules simultaneously</title>
		<link>http://brettic.us/2010/09/13/codeigniter-form-validation-mix-rule/</link>
		<comments>http://brettic.us/2010/09/13/codeigniter-form-validation-mix-rule/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 02:23:01 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=489</guid>
		<description><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/>CodeIgniter Form Validation Continually using the CodeIgniter MVC Framework has allowed me to experience many of it&#8217;s numerous &#8220;pros.&#8221; However, some times, I also hit a snag with one or more of it&#8217;s infrequent &#8221;cons.&#8221;  Truthfully, it&#8217;s put together ﻿overwhelmingly well (and much credit is due to the Ellis Labs crew for helping to make a PHP developer&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/><h2>CodeIgniter Form Validation</h2>
<p>Continually using the CodeIgniter MVC Framework has allowed me to experience many of it&#8217;s numerous &#8220;pros.&#8221; However, some times, I also hit a snag with one or more of it&#8217;s infrequent &#8221;cons.&#8221;  Truthfully, it&#8217;s put together ﻿overwhelmingly well (and much credit is due to the Ellis Labs crew for helping to make a PHP developer&#8217;s life a little easier.) Unfortunately, some libraries just do not work the way one (okay, me!) would expect them to work. Perhaps they are just a bit too inflexible for me. For example, I&#8217;ve never been a huge fan of CodeIgniter&#8217;s Form Validation library, and now I have yet another reason to scorn it somewhat. Admittedly, I&#8217;m probably being a bit too severe on this one. However, I feel annoyed in situations where lack of proper documentation costs me precious time (I say this in light of CodeIgniter&#8217;s, otherwise, excellent documentation.) Luckily, my perceived notion of how CodeIgniter&#8217;s  Form Validation Library should work can be accomplished, once again, with the kind of simple modification that keeps me coming back to CodeIgniter time and again.<span id="more-489"></span></p>
<h2>The problem</h2>
<p>After using a 3rd party library for CAPTCHA&#8217;s today, I noticed that the rules in the included application/config/form_validation.php file (hereby referred to as the &#8220;config file&#8221;) were not running. My other rules that I had defined inline (inside the controller) were working, but it seemed to ignore the rules in the config file completely. When I looked into the the form validation core library, I noticed that when rules are set from within the config file, they are only evaluated if no rules were set inline (set via $this-&gt;form_validation-&gt;set_rules(&#8230;)&#8230;) In other words, you must set no rules inline if you want your config file rules to work. This behavior took me off guard a bit because I could not see it documented in the manual (if anyone feels the need to enlighten me, please do.) Also, it makes importing 3rd party code in this manner potentially confusing (I have never used the config file approach before.) I decided that it made more sense to be able to use both types of rules simultaneously. Thus, I wrote an extended version of the Form Validation library.</p>
<h2>The Code</h2>
<p>This extended version of the Form Validation library allows me to mix both inline and config rules. If inline rules by the same field name are set, these will override the settings in the config file:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">application/libraries/MY_form_validation.php</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
<span class="co4">/**
 * Extension class to override Core Form Validation behavior.
 *
 * @author bmillett
 */</span>
<span class="kw2">class</span> MY_Form_validation <span class="kw2">extends</span> CI_Form_validation <span class="br0">&#123;</span>
    <span class="co1">// --------------------------------------------------------------------</span>
&nbsp;
    <span class="co4">/**
     * Run the Validator
     *
     * This function does all the work.
     *
     * Modified by Brett Millett:
     *  Provided option to remove the config or inline only restriction on
     *  rules. This version will process config rules first and then any
     *  inline rules that exist after. This has the benefit of allowing
     *  inline rules to overwite config rules by the same key.
     *
     * @access	public
     * @return	bool
     */</span>
    <span class="kw2">function</span> run<span class="br0">&#40;</span><span class="re0">$group</span> <span class="sy0">=</span> <span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$combine_conf_inline</span> <span class="sy0">=</span> <span class="kw4">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$combine_conf_inline</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">//only perform if we have both field and config rules.</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span>_field_data<span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span> <span class="sy0">&amp;&amp;</span> <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span>_config_rules<span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="co1">// Is there a validation rule for the particular URI being accessed?</span>
                <span class="re0">$uri</span> <span class="sy0">=</span> <span class="br0">&#40;</span><span class="re0">$group</span> <span class="sy0">==</span> <span class="st_h">''</span><span class="br0">&#41;</span> ? <a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">CI</span><span class="sy0">-&gt;</span><span class="me1">uri</span><span class="sy0">-&gt;</span><span class="me1">ruri_string</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="st_h">'/'</span><span class="br0">&#41;</span> <span class="sy0">:</span> <span class="re0">$group</span><span class="sy0">;</span>
&nbsp;
                <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$uri</span> <span class="sy0">!=</span> <span class="st_h">''</span> AND <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span>_config_rules<span class="br0">&#91;</span><span class="re0">$uri</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$config_rules</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span>_config_rules<span class="br0">&#91;</span><span class="re0">$uri</span><span class="br0">&#93;</span><span class="sy0">;</span>
                <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                    <span class="re0">$config_rules</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span>_config_rules<span class="sy0">;</span>
                <span class="br0">&#125;</span>
&nbsp;
                <span class="co1">// only set the rule if it has not already been set inline.</span>
                <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$config_rules</span> <span class="kw1">as</span> <span class="re0">$row</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span>_field_data<span class="br0">&#91;</span><span class="re0">$row</span><span class="br0">&#91;</span><span class="st_h">'field'</span><span class="br0">&#93;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
                        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rules</span><span class="br0">&#40;</span><span class="re0">$row</span><span class="br0">&#91;</span><span class="st_h">'field'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re0">$row</span><span class="br0">&#91;</span><span class="st_h">'label'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re0">$row</span><span class="br0">&#91;</span><span class="st_h">'rules'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="br0">&#125;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
        <span class="co1">//run parent version last, so field rules will  override config ones and update</span>
        <span class="kw1">return</span> parent<span class="sy0">::</span><span class="me2">run</span><span class="br0">&#40;</span><span class="re0">$group</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<h2>Examples</h2>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">application/config/form_validation.php</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
&nbsp;
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'BASEPATH'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st_h">'No direct script access allowed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="re0">$config</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>
    <span class="st_h">'mycontroller/index'</span> <span class="sy0">=&gt;</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>
        <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>
            <span class="st_h">'field'</span> <span class="sy0">=&gt;</span> <span class="st_h">'recaptcha_response_field'</span><span class="sy0">,</span>
            <span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="st_h">'lang:recaptcha_field_name'</span><span class="sy0">,</span>
            <span class="st_h">'rules'</span> <span class="sy0">=&gt;</span> <span class="st_h">'required|callback__check_captcha'</span>
        <span class="br0">&#41;</span>
    <span class="br0">&#41;</span>
<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>
<p>To use both combined, just call run() with an optional 2nd parameter:</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">application/controllers/mycontroller.php</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">class</span> Mycontroller <span class="kw2">extends</span> Controller<span class="br0">&#123;</span>
	<span class="kw2">function</span> index<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">library</span><span class="br0">&#40;</span><span class="st_h">'form_validation'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">library</span><span class="br0">&#40;</span><span class="st_h">'recaptcha'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">lang</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="br0">&#40;</span><span class="st_h">'recaptcha'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="re0">$data</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="co1">//get recaptcha html</span>
            <span class="re0">$data</span><span class="br0">&#91;</span><span class="st_h">'recaptcha_html'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">recaptcha</span><span class="sy0">-&gt;</span><span class="me1">get_html</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">form_validation</span><span class="sy0">-&gt;</span><span class="me1">set_rules</span><span class="br0">&#40;</span><span class="st_h">'name'</span><span class="sy0">,</span> <span class="st_h">'Your Name'</span><span class="sy0">,</span> <span class="st_h">'trim|required'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="co1">// call with new optional 2nd param to combine inline and config file rules.</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">form_validation</span><span class="sy0">-&gt;</span><span class="me1">run</span><span class="br0">&#40;</span><span class="st_h">''</span><span class="sy0">,</span> <span class="kw4">TRUE</span><span class="br0">&#41;</span> <span class="sy0">===</span> <span class="kw4">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">view</span><span class="br0">&#40;</span><span class="st_h">'captcha_form'</span><span class="sy0">,</span> <span class="re0">$data</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">view</span><span class="br0">&#40;</span><span class="st_h">'captcha_form_success'</span><span class="sy0">,</span> <span class="re0">$data</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/09/13/codeigniter-form-validation-mix-rule/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting the GET back in CodeIgniter!</title>
		<link>http://brettic.us/2010/08/24/getting-the-get-back-into-codeigniter/</link>
		<comments>http://brettic.us/2010/08/24/getting-the-get-back-into-codeigniter/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 06:36:17 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=382</guid>
		<description><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/>Ever wonder why you can't use query strings with CodeIgniter? This post will explain to some degree, but more importantly, show you how to parse query strings and how to get the GET back in CodeIgniter.]]></description>
			<content:encoded><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/><h4>Ever wonder why you can&#8217;t use query strings with CodeIgniter? This post will explain to some degree, but more importantly, show you how to parse query strings and how to get the GET back in CodeIgniter!</h4>
<p>I&#8217;ve been using CodeIgniter for a couple years now. It&#8217;s small, it&#8217;s simple, it&#8217;s powerful. Admittedly, it&#8217;s not perfect (what framework is?) but it&#8217;s a wonderful way to slap together a website using <a href="http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller">MVC design principles</a> and PHP. I do have one big complaint, however.</p>
<p><span id="more-382"></span></p>
<h3>The Hated $_GET global</h3>
<p>The CodeIgniter developers have some strange obsession with killing all GET data. It appears, from the manual at least, that they <em>strongly</em> discourage you from using query strings. For example, the following manual entry states:</p>
<blockquote><p><strong>By default</strong>, URLs in CodeIgniter are designed to be search-engine and human friendly. <strong>Rather than using the standard &#8220;query string&#8221; approach</strong> to URLs that is synonymous with dynamic systems, <strong>CodeIgniter uses a segment-based approach</strong>:</p></blockquote>
<p>You&#8217;ll notice that I have highlighted the words, &#8220;<strong>By default</strong>,&#8230;&#8221; because you <em>can</em>, in fact, turn on query strings in CodeIgniter, however there is an important trade-off to note (besides saying goodbye to those pretty URL&#8217;s:)</p>
<blockquote><p>Please note: If you are using query strings <strong>you will have to build your own URLs, rather than utilizing the URL helpers</strong> (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.</p></blockquote>
<p>Okay, admittedly, it&#8217;s not a big deal to build your own URL&#8217;s. I mean we used to do that all the time, right? However, what if you have used the &#8220;default method&#8221; of the segment-based approach and suddenly you need to pass GET values to your CodeIgniter application? Let&#8217;s also say that you&#8217;ve used many of those helpful URL functions, etc. Now, for the cost of enabling query strings, in the manner indicated by the manual, for that one page, you&#8217;ve got plenty of re-factoring to do! Not cool.</p>
<p>I know this sounds like a fantasy scenario, but it&#8217;s real. <em>It happened to me</em>. On two different occasions, working with two different payment gateway API&#8217;s, I suddenly had to provide postback URL&#8217;s. In both instances, the postback URL&#8217;s that were directed to my website, attached query-string variables to the URL in order to provide the relevant data (receipt number, etc.) Obviously, this is something I had no control over because I had to adhere to the API convention and they didn&#8217;t offer alternatives for &#8220;segment-based&#8221; Web frameworks!  I realize that I could have used a standalone script to handle these requests, but that could be potentially confusing for future developers (including myself after a few months) because the standalone script would reside outside of the framework. Also, I have based the entire application on the framework. The framework keeps my code organized in a way. I don&#8217;t want to have to use scripts that do not adhere to my conventions. I want to use the framework for all my code! More importantly, what about my models, libraries, helpers, etc? The framework shouldn&#8217;t force you to make the decision not to use it. It should, at least, provide a way to use a long-establish standard without compromising one of the best benefits of the framework.</p>
<p>I can understand disabling the $_GET super global (and query-string usage) by default when the whole framework is  geared towards segment-based URL&#8217;s. What I can&#8217;t understand is why they  make it so hard to enable query strings for situations when you might  actually need them. There is no simple configuration Boolean variable you can flip on and off!</p>
<p>So, why can&#8217;t we just specify a controller/method URI and tack on a query string? Admittedly, this gets a little complex to depend on with mod_rewrite and other factors outside of the control of the CodeIgniter developers. However, no attempt has been made to allow for the possibility. For an explanation, let&#8217;s go back to the manual:</p>
<blockquote><p>The security filtering function is called automatically when a new controller is invoked. It does the following:</p>
<ul>
<li>Destroys the global GET array. <strong>Since CodeIgniter does not utilize GET strings, there is no reason to allow it.</strong></li>
<li>Destroys all global variables in the event register_globals is turned on.</li>
</ul>
</blockquote>
<p>Okay, <em>clearly</em> there <em>is</em> a reason to allow it! I think we can tally up the real reasons behind destroying query string data to a matter of programmer laziness! I know that sounds harsh, but the manual clearly states (to paraphrase,) &#8220;Let&#8217;s not bother with storing GET data anywhere in the CodeIgniter super object because we don&#8217;t use it anyway. Phew, don&#8217;t have to worry about that anymore!&#8221;</p>
<h3>Let&#8217;s put it back! Methods for enabling $_GET in CodeIgniter</h3>
<p>Luckily, there are a few ways we can use GET variables without having to give up the segment-based approach (and all those nice URL functions!)</p>
<h3>Method 1: Extend The Input Class</h3>
<p>If you want to enable codeigniter to get query string variables globally, you can do it with a few easy steps:</p>
<ol>
<li>Create a MY_Input class in application/libraries</li>
<li>Update your application/config/config.php file: Change $config['uri_protocol']  to  &#8220;PATH_INFO&#8221;</li>
<li>If you are using mod_rewrite to hide index.php, make sure you have a slash in your rule after index.php: RewriteRule ^(.*)$ index.php/$1 [L]</li>
</ol>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">application/libraries/MY_Input.php</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="php">&nbsp;</pre></div></div>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Changes to application/config/config.php</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="coMULTI">/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string. The default setting of &quot;AUTO&quot; works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/</span>
<span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'uri_protocol'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st0">&quot;PATH_INFO&quot;</span><span class="sy0">;</span></pre></div></div>
<h3>When using mod_rewrite</h3>
<p>If you&#8217;re hiding index.php with mod_rewrite, make sure you use a slash after index.php. I could never make this work with PATH_INFO configured until I pulled out the question mark (?) in my RewriteRule.</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Slash only. Change ?/  to / if there.</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="apache"><span class="kw1">RewriteRule</span> ^(.*)$ index.php/$<span class="nu0">1</span> [L]</pre></div></div>
<h3>Using the get method of the Input class</h3>
<p>The nice part about this method is that you can refer to get variables using the input class. This has the advantage of easily utilizing the cross-site-scripting tools in the Input class (and keeping with the &#8220;CodeIgniter way&#8221; of doing things.)</p>
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Use the get method of the Input class</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="php">&nbsp;</pre></div></div>
<h3>Conclusion</h3>
<p>I had to do some &#8220;tinkering&#8221; to get this method to work; but in the end, it&#8217;s a fairly simple hack that requires minimal changes.</p>
<p>Our next method requires no configuration changes nor extended libraries whatsoever.</p>
<h3>Method 2: Use parse_str to Grab GET data out of REQUEST_URI</h3>
<p>This method should, perhaps, be subtitled, &#8220;The Quick and Dirty Method&#8221; as it&#8217;s extremely easy to implement: one line of code.</p>
<p>Despite what the manual states, CodeIgniter fails to destroy all global variables. For example, $_SERVER['REQUEST_URI']   shows what was given from the browser as the URL path. (Example: /controller/method?foo=bar.)</p>
<p>Having this data, we can use PHP&#8217;s parse_str function to parse $_SERVER['REQUEST_URI'] and rebuild the $_GET array. We also have to utilize substr to strip away the /controller/method part. Otherwise, we&#8217;ll end up with a $_GET key similar to &#8220;/controller/method?foo&#8221; when we just want our associative array key to be &#8220;foo.&#8221; It&#8217;s important to note that we can still use the fabulous Input class to filter our query-string data (it&#8217;s just not quite as pretty) against cross-site-scripting attacks.</p>
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Use parse_str to rebuild $_GET</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="php">&nbsp;</pre></div></div>
<h3>My preference because it&#8217;s a &#8220;genuine hack&#8221;</h3>
<p>I should note that this is the method I used for my payment gateway postbacks. In my particular situation, this was the best fit because I needed query-string variables for a single URL in the entire site. I really didn&#8217;t want to have to extend CodeIgniter or change my Apache settings for one URL (I&#8217;m fine with segment-based URL&#8217;s as long as they&#8217;re in my control.) I just put this one line of code in my controller method where it was needed.</p>
<p>If you don&#8217;t want the bother of dropping this code into each one of your controller methods, you can easily incorporate this into a helper function and make a simple function call any time you need $_GET. If you want to enable rebuilding $_GET globally, you can also take advantage of setting this code up in a pre_controller hook.</p>
<p>The best part of this method is that it works with CodeIgniter out-of-the-box. No configuration changes or extended libraries should be necessary. Always a &#8220;plus&#8221; in my book.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/08/24/getting-the-get-back-into-codeigniter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google Analytics Hook for CodeIgniter</title>
		<link>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/</link>
		<comments>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 22:37:16 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=351</guid>
		<description><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/>So, you finished that CodeIgniter Website last month. It was a tedious project and you&#8217;re glad it&#8217;s over! But wait! Now, your client informs you that you&#8217;re missing all the tracking code! The client also mentions he wants to use the popular, free Analytics tools from Google. This is gonna require inserting some javascript into [...]]]></description>
			<content:encoded><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/><p>So, you finished that CodeIgniter Website last month. It was a tedious project and you&#8217;re glad it&#8217;s over! But wait! Now, your client informs you that you&#8217;re missing all the tracking code! The client also mentions he wants to use the popular, free Analytics tools from Google. This is gonna require inserting some javascript into all of your Web pages. Not a problem if you used a global footer in your views. A potentially huge pain otherwise. Either way, if you&#8217;re looking for a very simple way to globally implement Analytics code in your application, CodeIgniter hooks can make your life much easier. I&#8217;m going to show you how to take advantage of the &#8216;display_override&#8217; hook point in CodeIgniter to get the job done fast!<span id="more-351"></span></p>
<p>To pull this off, you&#8217;ll need to slightly modify two configuration files (config.php and hooks.php) and then create another with a &#8220;hook&#8221; function (google_analytics.php.)</p>
<p>First, create a file named google_analytics.php and place it inside your application/hooks folder. We&#8217;ll reference this file later when we do our hooks configuration from within application/config/hooks.php.</p>
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">google_analytics.php</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span> <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'BASEPATH'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st_h">'No direct script access allowed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span><a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st_h">'insert_ga_script'</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">function</span> insert_ga_script<span class="br0">&#40;</span><span class="re0">$account_id</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="coMULTI">/***NOTE: This hook replaces the _display
        * method in the Output Class. I have
        * not made provisions for caching nor compression!
        */</span>
&nbsp;
        <span class="re0">$CI</span> <span class="sy0">=&amp;</span> get_instance<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">output</span><span class="sy0">-&gt;</span><span class="me1">get_output</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">//keep performance templates in...</span>
        <span class="re0">$elapsed</span> <span class="sy0">=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">benchmark</span><span class="sy0">-&gt;</span><span class="me1">elapsed_time</span><span class="br0">&#40;</span><span class="st_h">'total_execution_time_start'</span><span class="sy0">,</span> <span class="st_h">'total_execution_time_end'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'{elapsed_time}'</span><span class="sy0">,</span> <span class="re0">$elapsed</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$memory</span>	 <span class="sy0">=</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span><span class="st_h">'memory_get_usage'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> ? <span class="st_h">'0'</span> <span class="sy0">:</span> <a href="http://www.php.net/round"><span class="kw3">round</span></a><span class="br0">&#40;</span><a href="http://www.php.net/memory_get_usage"><span class="kw3">memory_get_usage</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">/</span><span class="nu0">1024</span><span class="sy0">/</span><span class="nu0">1024</span><span class="sy0">,</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">.</span><span class="st_h">'MB'</span><span class="sy0">;</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'{memory_usage}'</span><span class="sy0">,</span> <span class="re0">$memory</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// keep profiler working</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">output</span><span class="sy0">-&gt;</span><span class="me1">enable_profiler</span> <span class="sy0">==</span> <span class="kw4">TRUE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">load</span><span class="sy0">-&gt;</span><span class="me1">library</span><span class="br0">&#40;</span><span class="st_h">'profiler'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="co1">// If the output data contains closing &lt;/body&gt; and &lt;/html&gt; tags</span>
            <span class="co1">// we will remove them and add them back after we insert the profile data</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&quot;|&lt;/body&gt;.*?&lt;/html&gt;|is&quot;</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                <span class="re0">$output</span>  <span class="sy0">=</span> <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;|&lt;/body&gt;.*?&lt;/html&gt;|is&quot;</span><span class="sy0">,</span> <span class="st_h">''</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">profiler</span><span class="sy0">-&gt;</span><span class="me1">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="st_h">'&lt;/body&gt;&lt;/html&gt;'</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span>  <span class="br0">&#123;</span>
                <span class="re0">$output</span> <span class="sy0">.=</span> <span class="re0">$CI</span><span class="sy0">-&gt;</span><span class="me1">profiler</span><span class="sy0">-&gt;</span><span class="me1">run</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="co1">// if no account information just return the output</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$account_id</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// keep _output methods working...</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">,</span> <span class="st_h">'_output'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$CI</span><span class="sy0">-&gt;</span>_output<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="kw1">echo</span> <span class="re0">$output</span><span class="sy0">;</span>  <span class="co1">// Send it to the browser!</span>
            <span class="br0">&#125;</span>
            <span class="kw1">return</span> <span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <a href="http://www.php.net/ob_start"><span class="kw3">ob_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="sy1">?&gt;</span>
    &lt;!--Begin Google Analytics Site Code--&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
    document.write(unescape(&quot;%3Cscript src='&quot; + gaJsHost + &quot;google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E&quot;));
    &lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    try {
      var pageTracker = _gat._getTracker(&quot;<span class="sy1">&lt;?=</span><span class="re0">$account_id</span><span class="sy1">?&gt;</span>&quot;);
      pageTracker._initData();
      pageTracker._trackPageview();
    } catch(err) {}
    &lt;/script&gt;
    &lt;!--End Google Analytics Site Code--&gt;
        <span class="kw2">&lt;?php</span>
        <span class="re0">$script</span> <span class="sy0">=</span> <a href="http://www.php.net/ob_get_clean"><span class="kw3">ob_get_clean</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="co1">//insert the script</span>
        <span class="re0">$output</span> <span class="sy0">=</span> <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st_h">'&lt;/html&gt;'</span><span class="sy0">,</span> <span class="re0">$script</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es1">\n</span>&lt;/html&gt;&quot;</span><span class="sy0">,</span> <span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// keep _output methods working...</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/method_exists"><span class="kw3">method_exists</span></a><span class="br0">&#40;</span><span class="re0">$CI</span><span class="sy0">,</span> <span class="st_h">'_output'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$CI</span><span class="sy0">-&gt;</span>_output<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            <span class="kw1">echo</span> <span class="re0">$output</span><span class="sy0">;</span>  <span class="co1">// Send it to the browser!</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<p>The function above slips Analytics javsacript code into the CodeIgniter output buffer. It finds the closing html tag in CodeIgniter&#8217;s output and replaces it with our script. It then puts the closing html tag back into the output string. Simple and effective; because your views generally output html, and this method will only insert Analytics code if a closing html tag is found (your rss feeds, etc. are safe.)</p>
<p>Now, how do we intercept CodeIgniter&#8217;s output before rendering it to the browser? This is where hooks come into play. CodeIgniter hooks provide a way to extend the framework without messing with the CodeIgniter core code (which has many advantages I won&#8217;t go into during this post.) CodeIgniter defines &#8220;hook points&#8221; to allow you to specify where you want to throw your &#8220;monkey wrench.&#8221; Our &#8220;gilded wrench&#8221; is going to be thrown into CodeIgniter&#8217;s display or output proccess.</p>
<p>To demonstrate, let&#8217;s edit our application/config/hooks.php file. We leave &#8216;class&#8217; empty because we only need a function (in case you wondered.) We specify &#8216;googlea.php&#8217; under &#8216;filename.&#8217; We specify our &#8216;function&#8217; (in our case &#8216;insert_track_js.&#8217;)  Finally, we have one parameter to send to our function: the value of the google analytics account number (yours will vary of course.) We hook into the &#8216;display_override&#8217; hook point to override CodeIgniter&#8217;s default output (display) process. When you are done, your application/config/hooks.php should look similar to:</p>
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">hooks.php</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>  <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="sy0">!</span> <a href="http://www.php.net/defined"><span class="kw3">defined</span></a><span class="br0">&#40;</span><span class="st_h">'BASEPATH'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a><span class="br0">&#40;</span><span class="st_h">'No direct script access allowed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="coMULTI">/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define &quot;hooks&quot; to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/hooks.html
|
*/</span>
&nbsp;
<span class="co1">// google analytics insertion</span>
<span class="re0">$hook</span><span class="br0">&#91;</span><span class="st_h">'display_override'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span>
    <span class="st_h">'class'</span>    <span class="sy0">=&gt;</span> <span class="st_h">''</span><span class="sy0">,</span>
    <span class="st_h">'function'</span> <span class="sy0">=&gt;</span> <span class="st_h">'insert_ga_script'</span><span class="sy0">,</span>
    <span class="st_h">'filename'</span> <span class="sy0">=&gt;</span> <span class="st_h">'google_analytics.php'</span><span class="sy0">,</span>
    <span class="st_h">'filepath'</span> <span class="sy0">=&gt;</span> <span class="st_h">'hooks'</span><span class="sy0">,</span>
    <span class="st_h">'params'</span>   <span class="sy0">=&gt;</span> <span class="st_h">'UA-*******-**'</span>
<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="coMULTI">/* End of file hooks.php */</span>
<span class="coMULTI">/* Location: ./system/application/config/hooks.php */</span></pre></div></div>
<p>Finally, we just need to enable hooks in application/config/config.php. Look for the &#8216;enable_hooks&#8217; line:</p>
<div id="wpshdo_21" class="wp-synhighlighter-outer"><div id="wpshdt_21" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#config.php"></a><a id="wpshat_21" class="wp-synhighlighter-title" href="#config.php"  onClick="javascript:wpsh_toggleBlock(21)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#config.php" onClick="javascript:wpsh_code(21)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#config.php" onClick="javascript:wpsh_print(21)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_21" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="re0">$config</span><span class="br0">&#91;</span><span class="st_h">'enable_hooks'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw4">TRUE</span><span class="sy0">;</span></pre></div></div>
<p>Set the value to TRUE as shown above. You&#8217;re done!</p>
<p>For more information on hooks, see the <a href="http://codeigniter.com/user_guide/general/hooks.html">CodeIgniter Manual</a> or <a href="http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-profiling-benchmarking-hooks">this recent tutorial from Net Tuts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/08/09/google-analytics-hook-codeigniter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to install PHP Zip Extension for MAMP 1.9</title>
		<link>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/</link>
		<comments>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 05:07:41 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=337</guid>
		<description><![CDATA[<br/>Foreword Lately, when attempting to install extensions for MAMP, I&#8217;ve had a difficult time making my extensions work properly because my Darwin ports settings seem to override. Fortunately, I recently found a blog post with instructions that worked. There are a few steps that are different, however, for getting this to work with version 1.9. [...]]]></description>
			<content:encoded><![CDATA[<br/><h2>Foreword</h2>
<p>Lately, when attempting to install extensions for MAMP, I&#8217;ve had a difficult time making my extensions work properly because my <a href="http://serverfault.com/questions/114398/trouble-installing-php-memcache-extension" target="_blank">Darwin ports settings</a> seem to override. Fortunately, I recently found a blog post with instructions that worked. There are a few steps that are different, however, for getting this to work with version 1.9. Therefore, I decided to post the original instructions with those modifications here.<span id="more-337"></span></p>
<h2>Acknowledgments</h2>
<p>The following instructions are modified for MAMP version 1.9 and originally posted by Saverio at <a href="http://developers.enormego.com/view/enabling_php_s_zip_extension_in_mamp" target="_blank">http://developers.enormego.com</a>.</p>
<h2>Instructions</h2>
<ol>
<li><a href="http://sourceforge.net/projects/mamp/files/mamp/1.9/MAMP_components_1.9.dmg/download" target="_blank">Download MAMP&#8217;s &#8220;Source&#8221; package, located here</a>. If the link doesn&#8217;t work, look for and download the MAMP_components_1.9.dmg file located on this page: <a href="http://sourceforge.net/projects/mamp/files/" target="_blank">http://sourceforge.net/projects/mamp/files</a></li>
<li>Mount the DMG image and look for the PHP version that you&#8217;re using (php-5.2.13 or php-5.3.2) and unzip that archive. (Use the Archive Utility. Mine gets extracted to my Downloads folder.)</li>
<li>Open terminal and cd to the PHP directory that you just unzipped (/Users/you/Downloads most likely.)</li>
<li>Run the following command in terminal:<br />
<strong>./configure &#8211;prefix=/tmp/php</strong></li>
<li>Wait for the configure script to finish and then run:<br />
<strong>export PATH=/tmp/php/bin:$PATH</strong></li>
<li>Now, cd to the zip extension directory:<br />
<strong>cd ext/zip</strong></li>
<li>Run:<br />
<strong>phpize</strong></li>
<li>Run:<br />
<strong>./configure</strong></li>
<li>Wait for it to finish and then run:<br />
<strong>make</strong></li>
<li>Wait for it to finish and then run:<br />
<strong>sudo make install</strong> &#8212; (You&#8217;ll need to type your administrative password.)</li>
<li>After it&#8217;s done running, you&#8217;ll see something like this:<br />
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20060613/</li>
<li>Copy the zip.so extension file from that directory to your PHP installation&#8217;s extension directory:<br />
<strong>cp /usr/lib/php/extensions/no-debug-non-zts-20060613/zip.so /Applications/MAMP/bin/php5.2/lib/php/extensions/no-debug-non-zts-20060613/</strong></li>
</ol>
<h2>Update</h2>
<p>As of <a title="MAMP" href="http://blog-en.mamp.info/2010/10/mamp-1-9-3-released.html" target="_blank">MAMP version 1.9.3</a>, the zip extension is included. Thus, the easiest way to get it, is to upgrade. <strong>Consider this post DEPRECATED</strong>!</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/07/12/how-to-install-php-zip-extension-in-mamp-1-9/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zenoss: IT Monitoring for the overworked</title>
		<link>http://brettic.us/2010/06/24/zenoss-it-monitoring-for-the-overworked/</link>
		<comments>http://brettic.us/2010/06/24/zenoss-it-monitoring-for-the-overworked/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 19:19:15 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=321</guid>
		<description><![CDATA[<br/>I&#8217;ve been with my current company for 10+ years (at the time of writing.) It&#8217;s been a roller-coaster ride with ownership changing hands several times. But I&#8217;ve managed to hang on and I&#8217;m still riding even after the most recent change in drivers. Unfortunately, many have fallen out over the years and that leaves me [...]]]></description>
			<content:encoded><![CDATA[<br/><p><a href="http://brettic.us/wp-content/uploads/2010/06/zenoss-network-monitoring.png"><img class="alignleft size-full wp-image-323" title="zenoss-network-monitoring" src="http://brettic.us/wp-content/uploads/2010/06/zenoss-network-monitoring.png" alt="zenoss network monitoring" width="73" height="73" /></a>I&#8217;ve been with my current company for 10+ years (at the time of writing.) It&#8217;s been a roller-coaster ride with ownership changing hands several times. But I&#8217;ve managed to hang on and I&#8217;m still riding even after the most recent change in drivers. Unfortunately, many have fallen out over the years and that leaves me to do two jobs: Web programmer and IT Administrator. Finding time to do both jobs well can be a problem to say the least.<span id="more-321"></span></p>
<h3>Nagios is for IT Teams only</h3>
<p>The last network cut-over I did was back in November of 2009 (and lasted through December.) After getting all of our services set up, I did some research and decided to give nagios a spin. What I found is that it&#8217;s very time consuming to install and configure. Furthermore, I ran out of time and had to leave much of the network monitoring incomplete. I just didn&#8217;t have the time to configure each machine with all those wonderful plugins, etc.</p>
<h3>Desperation is the Mother of Necessity</h3>
<p>Last week, a relatively significant Website that a partner company hosts in our network went down (well, it&#8217;s their network also,  but I&#8217;m kinda the head overseer since they too are very small now. This is an odd relationship really.) After dusting off my ruby-on-rails deployment skills to attempt to find out why rails had fails (bad pun!) I found that a postgres database had stopped responding because its server&#8217;s /var partition had filled up. Had I had the time to totally incorporate nagios, I would have gotten a timely email or SMS stating that a partition was nearly full. It&#8217;s been several months since i dabbled with nagios, but it seems like configurations like this took some time to get up and running. So, this time, I went searching for a new IT monitoring solution. As luck would have it, I was recently listening to a podcast on the TWiT networked called <a title="FLOSS Weekly" href="http://twit.tv/floss" target="_blank">FLOSS Weekly</a>. As a guest, they had the community manager for <a title="Zenoss" href="http://community.zenoss.org/docs/DOC-2614" target="_blank">zenoss</a> on that episode. He mentioned that zenoss is easy to configure, so I took a look. I also took a look at others (opennms, zabbix to name a few.) In the end, there were two things I liked about the prospect of zenoss that swayed me in that direction:</p>
<ol>
<li>Python/Zope (in case I ever needed to look at the source. I know python to an extent.)</li>
<li>No client software to install just native snmp.</li>
</ol>
<p>The latter one was the clincher. Remember that I said time was not a luxury I had? So far zenoss has performed this role amazingly well.</p>
<h3>Zenoss to the Rescue</h3>
<p>Zenoss has a pretty recent<a title="zenoss vmware appliance" href="http://www.zenoss.com/download/links#softwareappliance" target="_blank"> </a><a title="zenoss vmware appliance" href="http://www.zenoss.com/download/links#softwareappliance" target="_blank">vmware player appliance</a> that includes a linux OS with zenoss installed and configured. I just happened to have a vmware ESXi server. I did a little research, and all you need to do to convert from vmware player to vmware ESX(i) is to use a single command (this requires turning on ssh on your ESXi box in case you have the free version like us. Otherwise there&#8217;s vCli or something else with a name I can&#8217;t recall ):</p>
<p><code>vmkfstools -i zenoss_player_file.vmdk zenoss_new.vmdk</code></p>
<p>Make sure you change the filenames accordingly. After that, I created a virtual server (Redhat 32bit.) I gave it  1 GB of RAM and a couple of processors. Be sure to select the option to use an existing disk from the datastore (I got mine in there in the first place using  wget via SSH console in case you wondered.)</p>
<p>Finally, I booted the new server and changed the defaults. I now had a fully-functioning zenoss core server with minimal effort on my part.</p>
<h3>SNMP Client Configurations</h3>
<p>This part was easy to implement but I struggled to find the best way to go about it. Ultimately, I ended up using the config file from a screencast tutorial. <a title="Zenos Server monitoring" href="http://castix.wordpress.com/2009/03/02/installing-zenoss-and-monitoring-server-via-snmp/">This screencast from Castix was a big help</a>. I made a slight change to &#8220;lock down&#8221; SNMP a bit better so just my zenoss server can access SNMP data:</p>
<p><code>## sec.name source community<br />
com2sec notConfigUser 10.77.1.29 public<br />
com2sec notConfigUser localhost public</code></p>
<p>&#8230;where 10.77.1.29 is the IP for my zenoss server.</p>
<p>On my Debian boxes I had to make a slight change to the /etc/defaults/snmp file to allow SNMP services to be exposed to remote hosts (see the screencast link above.) For my CentOS boxen, I noticed that I all had to do was replace the snmpd.conf file.</p>
<p>I did a simple Device Add and everything useful to know about the box is automatically gathered and displayed. From there you just setup your alert rules and you now have IT monitoring. Easy by comparison!</p>
<p>I&#8217;m still learning a thing or two in my spare time but so far, zenoss is just great! For now, I&#8217;m a new zenoss convert.</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/06/24/zenoss-it-monitoring-for-the-overworked/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Form Validation Class using PHP 5.3</title>
		<link>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/</link>
		<comments>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 06:22:35 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=312</guid>
		<description><![CDATA[<br/>Currently, I am working on a minimal MVC framework for a somewhat ambitious project. I chose to host this website using the latest and greatest: PHP-FPM on nginx running PHP 5.3. After realizing that I needed a form validation library, I came up with one that is fairly easy to use and somewhat extensible. This [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Currently, I am working on a minimal MVC framework for a somewhat ambitious project. I chose to host this website  using the latest and greatest: <a href="http://php-fpm.org/">PHP-FPM</a> on <a href="http://nginx.org/en/">nginx</a> running PHP 5.3. After realizing that I needed a form validation library, I came up with one that is fairly easy to use and somewhat extensible. This is the first time I have been able to use Closures in PHP code so maybe I am just a little bit geeky-giddy.</p>
<p><span id="more-312"></span></p>
<h2>The Code</h2>
<p>My only real criteria when I began writing is that I wanted to use method chaining so I could &#8220;stack up&#8221; rules on a given post variable. Here is what I ended up with:</p>
<div id="wpshdo_22" class="wp-synhighlighter-outer"><div id="wpshdt_22" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_22"></a><a id="wpshat_22" class="wp-synhighlighter-title" href="#codesyntax_22"  onClick="javascript:wpsh_toggleBlock(22)" title="Click to show/hide code block">formvalidator.php</a></td><td align="right"><a href="#codesyntax_22" onClick="javascript:wpsh_code(22)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_22" onClick="javascript:wpsh_print(22)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_22" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
&nbsp;
<span class="kw2">class</span> FormValidator <span class="kw2">extends</span> Library_Base <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">private</span> <span class="re0">$messages</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">private</span> <span class="re0">$errors</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">private</span> <span class="re0">$rules</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">private</span> <span class="re0">$fields</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">private</span> <span class="re0">$haspostdata</span> <span class="sy0">=</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
&nbsp;
    <span class="coMULTI">/*** ADD NEW RULE FUNCTIONS BELOW THIS LINE ***/</span>
&nbsp;
    <span class="co4">/**
     * email
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> email<span class="br0">&#40;</span><span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s is an invalid email address.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$email</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/filter_var"><span class="kw3">filter_var</span></a><span class="br0">&#40;</span><span class="re0">$email</span><span class="sy0">,</span> FILTER_VALIDATE_EMAIL<span class="br0">&#41;</span> <span class="sy0">===</span> <span class="kw4">FALSE</span> <span class="br0">&#41;</span> ? <span class="kw4">FALSE</span> <span class="sy0">:</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
    <span class="co4">/**
     * required
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> required<span class="br0">&#40;</span><span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s is required.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="kw4">FALSE</span> <span class="sy0">:</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * numbersonly
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> numeric<span class="br0">&#40;</span><span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must consist of numbers only.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st_h">'/^[-]?[0-9.]+$/'</span><span class="sy0">,</span> <span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> minlength<span class="br0">&#40;</span><span class="re0">$minlen</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must be at least '</span> <span class="sy0">.</span> <span class="re0">$minlen</span> <span class="sy0">.</span> <span class="st_h">' characters or longer.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$minlen</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/strlen"><span class="kw3">strlen</span></a><span class="br0">&#40;</span><a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&lt;</span> <span class="re0">$minlen</span> <span class="br0">&#41;</span> ? <span class="kw4">FALSE</span> <span class="sy0">:</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> maxlength<span class="br0">&#40;</span><span class="re0">$maxlen</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must be no longer than '</span> <span class="sy0">.</span> <span class="re0">$maxlen</span> <span class="sy0">.</span> <span class="st_h">' characters.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$maxlen</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/strlen"><span class="kw3">strlen</span></a><span class="br0">&#40;</span><a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="re0">$maxlen</span> <span class="br0">&#41;</span> ? <span class="kw4">FALSE</span> <span class="sy0">:</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     *
     * @param int $len
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> length<span class="br0">&#40;</span><span class="re0">$len</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must be exactly '</span> <span class="sy0">.</span> <span class="re0">$len</span> <span class="sy0">.</span> <span class="st_h">' characters in length.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$len</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/strlen"><span class="kw3">strlen</span></a><span class="br0">&#40;</span><a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="re0">$len</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     *
     * @param string $field
     * @param string $label
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> matches<span class="br0">&#40;</span><span class="re0">$field</span><span class="sy0">,</span> <span class="re0">$label</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must match '</span> <span class="sy0">.</span> <span class="re0">$label</span> <span class="sy0">.</span> <span class="st_h">'.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$matchvalue</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getval</span><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$matchvalue</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$matchvalue</span> <span class="sy0">==</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$string</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     *
     * @param string $field
     * @param string $label
     * @param string $message
     * @return FormValidator
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> not_matches<span class="br0">&#40;</span><span class="re0">$field</span><span class="sy0">,</span> <span class="re0">$label</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="st_h">'%s must not match '</span> <span class="sy0">.</span> <span class="re0">$label</span> <span class="sy0">.</span> <span class="st_h">'.'</span> <span class="sy0">:</span> <span class="re0">$message</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$matchvalue</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getval</span><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="kw4">__FUNCTION__</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$string</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$matchvalue</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$matchvalue</span> <span class="sy0">==</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$string</span> <span class="br0">&#41;</span> ? <span class="kw4">FALSE</span> <span class="sy0">:</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="coMULTI">/*** ADD NEW RULE FUNCTIONS ABOVE THIS LINE ***/</span>
&nbsp;
    <span class="co4">/**
     * callback
     * @param string $name
     * @param mixed $function
     * @param string $message
     * @return FormValidator
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> callback<span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="re0">$function</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/is_callable"><span class="kw3">is_callable</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// set rule and function</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="re0">$function</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">elseif</span> <span class="br0">&#40;</span> <a href="http://www.php.net/is_string"><span class="kw3">is_string</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="sy0">,</span> <span class="st_h">'callback'</span><span class="br0">&#41;</span> <span class="sy0">!==</span> <span class="kw4">FALSE</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// we can parse this as a regexp. set rule function accordingly.</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="sy0">,</span> <span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
            <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            <span class="co1">// just set a rule function to check equality.</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set_rule</span><span class="br0">&#40;</span><span class="re0">$name</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span> <span class="kw2">use</span> <span class="br0">&#40;</span> <span class="re0">$function</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">return</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$value</span> <span class="sy0">===</span> <span class="br0">&#40;</span>string<span class="br0">&#41;</span><span class="re0">$function</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
            <span class="br0">&#125;</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * validate
     * @param string $key
     * @param string $label
     * @return bool
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> validate<span class="br0">&#40;</span><span class="re0">$key</span><span class="sy0">,</span> <span class="re0">$label</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="co1">// do not attempt to validate when no post data is present</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">haspostdata</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// set up field name for error message</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$label</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">fields</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$label</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
            <span class="co1">// try each rule function</span>
            <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">rules</span> <span class="kw1">as</span> <span class="re0">$rule</span> <span class="sy0">=&gt;</span> <span class="re0">$function</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_callable"><span class="kw3">is_callable</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">getval</span><span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">===</span> <span class="kw4">FALSE</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">register_error</span><span class="br0">&#40;</span><span class="re0">$rule</span><span class="sy0">,</span> <span class="re0">$key</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        <span class="co1">// reset rules</span>
                        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">rules</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        <span class="kw1">return</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">register_error</span><span class="br0">&#40;</span><span class="re0">$rule</span><span class="sy0">,</span> <span class="re0">$key</span><span class="sy0">,</span> <span class="st_h">'Invalid function for rule'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">rules</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="kw1">return</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
                <span class="br0">&#125;</span>
            <span class="br0">&#125;</span>
            <span class="co1">// reset rules</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">rules</span> <span class="sy0">=</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="kw1">return</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * has_errors
     * @return bool
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> has_errors<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">errors</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * set_error_message
     * @param string $rule
     * @param string $message
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> set_error_message<span class="br0">&#40;</span><span class="re0">$rule</span><span class="sy0">,</span> <span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">messages</span><span class="br0">&#91;</span><span class="re0">$rule</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$message</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * get_error
     * @param string $field
     * @return string
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> get_error<span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">errors</span><span class="br0">&#91;</span><span class="re0">$field</span><span class="br0">&#93;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * get_all_errors
     * @return array
     */</span>
&nbsp;
    <span class="kw2">public</span> <span class="kw2">function</span> get_all_errors<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">errors</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="coMULTI">/*public function __set($key, $value) {
        $this-&gt;messages[$key] = $value;
    }
&nbsp;
    public function __get($key) {
        return $this-&gt;messages[$key];
    }*/</span>
&nbsp;
    <span class="co4">/**
     * getval
     * @param string $key
     * @return mixed
     */</span>
&nbsp;
    <span class="kw2">private</span> <span class="kw2">function</span> getval<span class="br0">&#40;</span><span class="re0">$key</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/isset"><span class="kw3">isset</span></a> <span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="re0">$_POST</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span> <span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * register_error
     * @param string $rule
     * @param string $key
     * @param string $msg_override
     */</span>
&nbsp;
    <span class="kw2">private</span> <span class="kw2">function</span> register_error<span class="br0">&#40;</span><span class="re0">$rule</span><span class="sy0">,</span> <span class="re0">$key</span><span class="sy0">,</span> <span class="re0">$msg_override</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$message</span> <span class="sy0">=</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$msg_override</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> ? <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">messages</span><span class="br0">&#91;</span><span class="re0">$rule</span><span class="br0">&#93;</span> <span class="sy0">:</span> <span class="re0">$msg_override</span><span class="sy0">;</span>
        <span class="re0">$field</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">fields</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
            <span class="re0">$message</span> <span class="sy0">=</span> <span class="st_h">'%s has an error.'</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/empty"><span class="kw3">empty</span></a> <span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
            <span class="re0">$field</span> <span class="sy0">=</span> <span class="st0">&quot;Field with the name of '<span class="es4">$key</span>'&quot;</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">errors</span><span class="br0">&#91;</span><span class="re0">$key</span><span class="br0">&#93;</span> <span class="sy0">=</span> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="re0">$message</span><span class="sy0">,</span> <span class="re0">$field</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="co4">/**
     * set_rule
     * @param string $rule
     * @param closure $function
     * @param string $message
     */</span>
&nbsp;
    <span class="kw2">private</span> <span class="kw2">function</span> set_rule<span class="br0">&#40;</span><span class="re0">$rule</span><span class="sy0">,</span> <span class="re0">$function</span><span class="sy0">,</span> <span class="re0">$message</span><span class="sy0">=</span><span class="st_h">''</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="co1">// do not attempt to validate when no post data is present</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">haspostdata</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_callable"><span class="kw3">is_callable</span></a><span class="br0">&#40;</span><span class="re0">$function</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">rules</span><span class="br0">&#91;</span><span class="re0">$rule</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$function</span><span class="sy0">;</span>
                <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.php.net/empty"><span class="kw3">empty</span></a><span class="br0">&#40;</span><span class="re0">$message</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">messages</span><span class="br0">&#91;</span><span class="re0">$rule</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$message</span><span class="sy0">;</span>
                <span class="br0">&#125;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<blockquote><p>Please note: This only works for forms submitted with POST. However, it can be easily modified to work with GET also.</p></blockquote>
<h2>Usage</h2>
<p>Now, let&#8217;s say that I have a form and I want to validate an email field and set up a password. The password must be at least 10 characters and the password confirmation field must match the password field. On top of that, I have a simple captcha where I just ask the user to add two integers that equal 10. With my class, I only need to write the following code:</p>
<div id="wpshdo_23" class="wp-synhighlighter-outer"><div id="wpshdt_23" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_23"></a><a id="wpshat_23" class="wp-synhighlighter-title" href="#codesyntax_23"  onClick="javascript:wpsh_toggleBlock(23)" title="Click to show/hide code block">Usage</a></td><td align="right"><a href="#codesyntax_23" onClick="javascript:wpsh_code(23)" title="Show code only"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_23" onClick="javascript:wpsh_print(23)" title="Print code"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://brettic.us/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://brettic.us/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_23" class="wp-synhighlighter-inner" style="display: block;"><pre class="php"><span class="kw2">&lt;?php</span>
<span class="re0">$formval</span> <span class="sy0">=</span> <span class="kw2">new</span> FormValidator<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">required</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">email</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="st_h">'email'</span><span class="sy0">,</span> <span class="st_h">'Email Address'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">required</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">minlength</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="st_h">'password'</span><span class="sy0">,</span> <span class="st_h">'Password'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">required</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">matches</span><span class="br0">&#40;</span><span class="st_h">'password'</span><span class="sy0">,</span> <span class="st_h">'Password'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="st_h">'password2'</span><span class="sy0">,</span> <span class="st_h">'Password Confirmation'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">required</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">callback</span><span class="br0">&#40;</span><span class="st_h">'captcha'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw1">return</span> <span class="br0">&#40;</span> <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st_h">'/^\d\s*\+\s*\d$/'</span><span class="sy0">,</span> <a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <a href="http://www.php.net/eval"><span class="kw3">eval</span></a> <span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">10</span> <span class="br0">&#41;</span> ? <span class="kw4">TRUE</span><span class="sy0">:</span> <span class="kw4">FALSE</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">,</span> <span class="st_h">'%s does not add up to 10!'</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="st_h">'captcha'</span><span class="sy0">,</span> <span class="st_h">'Captcha'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">if</span> <span class="br0">&#40;</span> <span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">has_errors</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
   <a href="http://www.php.net/print_r"><span class="kw3">print_r</span></a><span class="br0">&#40;</span><span class="re0">$formval</span><span class="sy0">-&gt;</span><span class="me1">get_all_errors</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
	<span class="co1">// submit the form!</span>
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div>
<p>Pretty simple!</p>
<p>I have not started using this class heavily ( I just finished it) so let&#8217;s call it alpha code for now.</p>
<h2>Forks!</h2>
<p>I had submitted my code as a project on github, to which, Tasos has generously forked (and pointed out a bug in my original code&#8211;thanks!) Tasos has modified rules and created many more. Since this post was more of a demonstration of how using closures and method chaining goes a long way to making a better validation class, I would seriously consider using Tasos version since it has been &#8220;polished&#8221; for production use. You can find his repo at: <a title="Bekos PHP Validation" href="https://github.com/bekos/php-validation" target="_blank">https://github.com/bekos/php-validation</a></p>
<p>If you want to fork my original version (with any future big fixes and/or new features) the project is also at github:</p>
<p><a title="Bretticus Form Validator" href="https://github.com/bretticus/PHP-5.3-Form-Validator" target="_blank">https://github.com/bretticus/PHP-5.3-Form-Validator</a></p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Debugging PHP on a Mac with XDebug, Firefox and Netbeans</title>
		<link>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/</link>
		<comments>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 06:07:28 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Netbeans]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=202</guid>
		<description><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/>After nearly two months of using OS X, I think I have finally found my groove when it comes to doing my job. That is, writing code for websites, mainly in PHP. For many years prior to using a Mac for PHP development, I had been using Windows or Linux and Zend Studio 5.x. When [...]]]></description>
			<content:encoded><![CDATA[<img src="http://brettic.us/wp-content/uploads/2010/10/codeigniter.png" width="48" height="48" alt="" title="CodeIgniter" /><br/><p>After nearly two months of using OS X, I think I have finally found my groove when it comes to doing my job.  That is, writing code for websites, mainly in PHP.</p>
<p>For many years prior to using a Mac for PHP development, I had been using Windows or Linux and Zend Studio 5.x. When I was looking several months back for the Mac version download for my Zend Studio license, I was presented, instead, with a free upgrade to version 6. Wow, that was nice Zend! Unfortunately, the version I paid for (the &#8220;perpetual&#8221; license) was no longer there to download.</p>
<blockquote><p>UPDATE: As of my last login to Zend, the 5.5.1 download was available. I installed and everything worked well. Still using Netbeans but appreciated all the same.</p></blockquote>
<p>But hey! I got the brand-new version. That&#8217;s great right? Wrong! Zend decided to drink the Eclipse Kool Aid and completely changed an IDE that I had grown accustomed to (I guess it&#8217;s only fair to point out that Zend Studio 5.x was so old at this point that it was getting increasingly more difficult to install on more modern operating systems and newer java platforms.) In spite of the seemingly &#8220;over-bloat&#8221; of Zend Studio 6.0, I decided to give it a try. I must say that everything just felt wrong. Now I&#8217;m sure there are many PHP developers out there that have been using Eclipse for years and can&#8217;t live without it, but I just couldn&#8217;t seem to make it work my voodoo. I didn&#8217;t have the patience to get my Eclipse PHD, so I went looking elsewhere. That led me to a surprising alternative: <a href="http://www.netbeans.org/features/php/">Netbeans</a>. Although very similar to Eclipse, an IDE with modules/plug-ins for multiple computer language support, Netbeans (entirely free) impressed me right on the spot. I know Zend Studio has all the same features more or less, but the way Netbeans is put together just seemed more natural to me (things I wanted to do actually worked.) Let it suffice to say that I have been using Netbeans since then and I haven&#8217;t looked back!</p>
<p>In this post, we will discover how to go about debugging a specific page in your website through setting up debugging in your MAMP stack, the free Netbeans IDE and your Firefox browser.</p>
<p><span id="more-202"></span></p>
<h2>Setting up Debugging with Xdebug and Firefox</h2>
<p>In order to leverage the new-found versatility or <a href="http://www.mamp.info/en/mamp/index.html">MAMP</a>, I needed a good debugger. I had had a pretty decent debugger with Zend Studio 5.x and that would be an essential requirement for netbeans. One of the cool features of Zend Studio was the integration of the Zend Toolbar with the debugger. Fortunately, this can be done with netbeans also, but you have to get a firefox plugin called <span style="text-decoration: line-through;">Xdebug Helper</span> <a title="easy Xdebug" href="https://addons.mozilla.org/en-US/firefox/addon/58688/" target="_blank">easy Xdebug</a>. When you get it, note that it has one configuration option you need to set: Xdebug.idekey needs to be set to <strong>netbeans-xdebug</strong>.</p>
<p><a href="http://brettic.us/wp-content/uploads/2010/05/Picture-2.png"><img class="size-medium wp-image-298 alignnone" style="margin: 10px; border: 0pt none;" title="easy Xdebug" src="http://brettic.us/wp-content/uploads/2010/05/Picture-2-300x293.png" alt="" width="300" height="293" /></a></p>
<p>Since I was using MAMP, I needed to install the xdebug extension for PHP. I actually downloaded the source and built the extension (used Xcode tools to phpize the source and build it and then just copied it to the appropriate MAMP folder.) I wish I had taken the time to record the steps involved, but instead I&#8217;ll just refer you to <a href="http://debuggable.com/posts/setting-up-xdebug-on-mac-os-x-or-win32-linux:480f4dd6-0240-4a90-8fa1-4e41cbdd56cb">the easy way</a>. Now we just need a little change in our php.ini file and we are good to go:</p>
<blockquote><p>Note: As of MAMP version 1.8.4, there is an xdebug extension that comes with MAMP (just be sure to disable Zend Optimizer since they don&#8217;t seem to play well together.)</p></blockquote>
<p><code>; Xdebug config for Mac OS X and NetBeans IDE<br />
zend_extension=/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/xdebug.so<br />
xdebug.remote_enable=1<br />
xdebug.remote_handler=dbgp<br />
xdebug.remote_mode=req<br />
xdebug.remote_host=127.0.0.1<br />
xdebug.remote_port=9000</code></p>
<div id="attachment_213" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-3.png"><img class="size-medium wp-image-213" title="Xdebug in php.ini" src="http://brettic.us/wp-content/uploads/2009/11/Picture-3-300x198.png" alt="Xdebug in php.ini" width="300" height="198" /></a><p class="wp-caption-text">Xdebug in php.ini</p></div>
<p>Now, all we have to do in order to debug is evoke the debugger in netbeans. I do this by clicking on the debug project button in the netbeans toolbar:</p>
<div id="attachment_221" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-2.png"><img class="size-medium wp-image-221" title="button" src="http://brettic.us/wp-content/uploads/2009/11/Picture-2-300x92.png" alt="netbeans debug button" width="300" height="92" /></a><p class="wp-caption-text">netbeans debug button</p></div>
<p>Netbeans will open up your development website according to the URL you submitted when creating the project (make sure firefox is your default browser and that you have set the default page in your project.)</p>
<blockquote><p><strong>Tip! </strong>You can also force Netbeans to not open your browser when you debug:</p>
<p style="padding-left: 30px;">In your project properties (right click on the project -&gt;  properties), click on Run Configuration, look for the “Advanced…” button  and set “<strong>Do Not Open Web Browser</strong>“. Click OK.</p>
<p>A big thanks to <a href="http://www.leonardteo.com/2010/08/getting-php-xdebug-codeigniter-and-netbeans-to-all-work-nicely-together/" target="_blank">Leonard Teo</a> for this tip.</p>
</blockquote>
<p>Often, just clicking the button will automatically activate the Xdebug Helper addon for me. But if you want to make sure that netbeans debugs the URL in your browser, be sure the click the X in your firefox footer bar so that it changes color from gray to green.</p>
<div id="attachment_222" class="wp-caption alignnone" style="width: 310px"><a href="http://brettic.us/wp-content/uploads/2009/11/Picture-31.png"><img class="size-medium wp-image-222" title="Xdebug Helper Toogle Icon" src="http://brettic.us/wp-content/uploads/2009/11/Picture-31-300x272.png" alt="Xdebug Helper Toogle Icon" width="300" height="272" /></a><p class="wp-caption-text">Xdebug Helper Toogle Icon</p></div>
<p>Netbeans will open your startup page to something like:<br />
<code><br />
index.php?XDEBUG_SESSION_START=netbeans-xdebug</code></p>
<p>If you are using a PHP framework like <a href="http://codeigniter.com/">Codeigniter</a>, the URL can be a little annoying at first. I have found that I only need to press the continue button to let the start page run it&#8217;s path and then just close that tab. From that point I just leave the debugger running and toggle the Xdebug Helper &#8220;X&#8221; icon when I need to debug any page that I am working on. Couldn&#8217;t be easier!</p>
<h2>Screencast Tutorials</h2>
<p>The following screencasts cover making XDebug and Netbeans work together. It&#8217;s a recap of the text here with some added instruction on setting up an Apache virtual host for development from your computer. Please note, I do not have the ability to be concise when I speak and you may lose patience. My apologies up front. (If you want a condensed version, just watch Part 2 and than skip to Part 6. You might want to watch Part 5 if 6 leaves you scratching your head.) Also, as many people find when they listen to themselves recorded, I find that my voice and accent sounds kind of strange and annoying. That&#8217;s one part me and another part Southern Utah accent. Bear with me if you can. <img src='http://brettic.us/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h3 style="text-align: left;">Part 1: Prerequites for Xdebug and PHP on a Mac</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43318" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43318"></embed></object></p>
<h3>Part 2: Setting up XDebug Helper</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43319" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43319"></embed></object></p>
<h3>Part 3: Setting up an virtualhost apache environment in MAMP for codeigniter</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43320" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43320"></embed></object></p>
<h3>Part 4: Starting to use XDebug in Netbeans</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43322" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43322"></embed></object></p>
<h3>Part 5: Debugging with Netbeans and XDebug</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43323" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43323"></embed></object></p>
<h3>Part 6: Wrapping up by showing a few features of debugging with xdebug in  Netbeans</h3>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="345" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashvars" value="i=43324" /><param name="allowFullScreen" value="true" /><param name="src" value="http://screenr.com/Content/assets/screenr_1116090935.swf" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="345" src="http://screenr.com/Content/assets/screenr_1116090935.swf" allowfullscreen="true" flashvars="i=43324"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2009/11/07/developing-php-on-a-mac-with-netbeans/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>I&#8217;m a &#8220;Mapple&#8221; Person!</title>
		<link>http://brettic.us/2009/08/27/mapple-person/</link>
		<comments>http://brettic.us/2009/08/27/mapple-person/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 07:22:25 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Bretticus]]></category>
		<category><![CDATA[Humor]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://brettic.us/?p=127</guid>
		<description><![CDATA[<br/>I made the Switch I&#8217;ve jumped in! I&#8217;ve slipped on the proverbial black turtleneck. Yes, I&#8217;m a switcher! About a month ago I decided that my large, hot lead-brick-of-a-laptop, otherwise known as my HP Pavilion ZD8000, was getting a bit &#8220;long in the tooth.&#8221; On a hot July day, in my air-conditioned home office, it [...]]]></description>
			<content:encoded><![CDATA[<br/><h2>I made the Switch</h2>
<p>I&#8217;ve jumped in! I&#8217;ve slipped on the proverbial black turtleneck. Yes, I&#8217;m a switcher!</p>
<p>About a month ago I decided that my large, hot lead-brick-of-a-laptop, otherwise known as my <a href="http://www.google.com/search?q=hp+pavilion+zd8000">HP Pavilion ZD8000,</a> was getting a bit &#8220;long in the tooth.&#8221; On a hot July day, in my air-conditioned home office, it decided to to test my patience by overheating and shutting off without warning in approximately 10 minute intervals. My only thought, &#8220;Time to ship this sucker away!&#8221; Fortunately, my &#8220;brick&#8221; belonged to my employer. So, I asked for a new laptop; and I asked for a MacBook Pro!</p>
<p><span id="more-127"></span></p>
<p>This all started several years back when my office was in a building with my fellow employees and not in my house. You see, that office was chuck-full of Windows, but I decided I wanted to use Ubuntu exclusively at work. At that time, I wore two hats (and still do to a certain degree.) I moonlighted as a network admin while I wrote web code for most of the day. The vast majority of my code was PHP so Windows wasn&#8217;t all so necessary (yes, I had to reboot into XP once in a while to crank out a .NET app or two.)<br />
I got to where I could pretty much do everything I needed with <a href="http://www.ubuntu.com">Ubuntu Linux</a>. It was great for running a LAMP stack and many of my net admin tasks were done at the console. It was a great fit for me. I used it exclusively for several years, and then I moved.</p>
<p>I started telecommuting in late 2007. I turned in my Dell for a shiny new brick. It came with Windows on it. I promptly dual booted Ubuntu into it and it became a permanent fixture in my home office. Immediately, I had issues with my USB headphones and sound in Ubuntu. Since my Skype account became my work PBX extension, I found myself selling out and moving back to Windows to avoid the headaches associated with intermittent sound issues in Linux. In fact, I just started using my brick as a LAMP server (Ubuntu Desktop) and developing in Windows from my very own desktop pc. That was about the time I realized that a Mac might be a good compromise for me. Now that I&#8217;ve used it for few weeks, I couldn&#8217;t be happier with the combination. Thanks to helpful utilities like <a href="http://www.mamp.info/en/index.html">MAMP</a>, I have a full LAMP-equivalent *nix stack on my Mac with all the niceties of a proprietary GUI. Finally, I could roll two computers into one for development and administration duties.</p>
<p><span style="text-decoration: line-through;">Over the next few months, I&#8217;ll be writing about my setup for PHP development on my MacBook Pro and why I chose the setup that I did. I&#8217;ll be including tweaks, etc. that I have implemented to customize my experience.</span> (Let&#8217;s be honest, my record would indicate this Mac writing will not happen for awhile so, please, don&#8217;t hold your breath. Sorry.)<span style="text-decoration: line-through;"><br />
</span></p>
<h2>Impressions</h2>
<p>The first day I booted my new, shiny MacBook Pro (actually, it was used and a couple of years old. but &#8220;new and shiny&#8221; nonetheless ), I felt like I had lost several million brain cells and devolved into a Neanderthal. Apparently, Apple had decided that they invented the personal computer and, as years went by, to stubbornly hold onto their &#8220;crazy&#8221; keyboard configuration. To make me feel even more like a newbie, a certain suspicion that I had carried around for many years proved untrue. Ubuntu currently chooses gnome for it&#8217;s default GUI interface. Since gnome shares many similarities with Windows, I must admit, using Ubuntu had not prepared me for using a Mac as I assumed. I stumbled for a few days getting it set up just right to work for me. Finally, After a week or two of rewiring my brain to use the Alt key instead of the Ctrl key (I needed to use my PC keyboard because Apple keyboards seem to suck,) I finally felt &#8220;OS ambidextrous&#8221; enough to forge on.</p>
<p>One of the surprising things about Mac was how closed it felt compared to Linux or even Windows (As I get more familiar with the ins-and-outs of OSX, that perception  diminishes somewhat.) For example, it seems that all the option settings for Mac applications were, &#8220;I&#8217;m an idiot, just do it your way&#8221; and&#8230;well, that&#8217;s it. Apple does tend to make their software &#8220;blunt and to the point&#8221; (much like an Apple boardroom meeting I&#8217;m sure.) I do appreciate the rock-steady stability of OSX.</p>
<p>The hardware is just beautiful. It&#8217;s silent, cold, and light. It really is art and elegance. I do &#8220;dig it.&#8221; I have not started worshipping  it yet however.</p>
<h2>Mapple People</h2>
<p>I have gotten many compliments from several Mac users of my acquaintance and also in online communities. To my new status as a &#8220;switcher&#8221;, I typically get something like, &#8220;Welcome to the fresh, clean Apple air outside of the putrid, stinky torture-chamber-of-a-deep-dark-dungeon called Windows that you languished in all your life!.&#8221; &#8230; Wait a minute??? I still use Windows 7 (love it)  from time to time and there are still some things about OSX that drive me nuts! I must say, I do not want to be counted among the turtle-neck wearing fan-boys that have a hissy fit the instance Apple is criticized in any way, shape, or form (tune into MacBreak Weekly for examples.) The Mac culture is sort of weird (and I&#8217;ve thought so for years.) The &#8220;true believers&#8221; seem to be a throwback of the fringe-clad culture of the 60s. Apple has indeed encouraged it. I personally feel there are better things to get zealous about than whether or not you have a half-eaten piece of fruit icon opposite your screen. I&#8217;ll just take OSX as a  very nice *nix-based OS that I can use comfortably for network administration and development. Thank you very much!</p>
<p>(of course the real reason I posted was to show these two hilarious videos on mac culture.)</p>
<p style="text-align: center;"><iframe src="http://www.youtube.com/embed/7L2fsubA2-c" frameborder="0" width="425" height="349"></iframe></p>
<p style="text-align: center;"><iframe src="http://www.youtube.com/embed/CZGIn9bpALo" frameborder="0" width="425" height="349"></iframe></p>
<p>&nbsp;</p>
<p>The latter video is even more hilarious if you&#8217;ve seen this original 1984 Apple ad.</p>
<p style="text-align: center;"><object width="480" height="385" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/R706isyDrqI&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6" /><param name="allowfullscreen" value="true" /><embed width="480" height="385" type="application/x-shockwave-flash" src="http://www.youtube.com/v/R706isyDrqI&amp;hl=en_US&amp;fs=1&amp;color1=0x006699&amp;color2=0x54abd6" allowFullScreen="true" allowscriptaccess="always" allowfullscreen="true" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2009/08/27/mapple-person/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bretticus.org get&#8217;s a face-lift! Welcome to brettic.us!</title>
		<link>http://brettic.us/2009/07/13/bretticus-org-gets-a-face-lift/</link>
		<comments>http://brettic.us/2009/07/13/bretticus-org-gets-a-face-lift/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 17:45:41 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Bretticus]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<br/>Welcome to the new Brettic.us!

Had to do away with Drupal as it was overkill. I am using wordpress and it's much easier to skin, etc. And, I don't need anything bigger than a blog. 

More to come...]]></description>
			<content:encoded><![CDATA[<br/><p>Welcome to the new Brettic.us!</p>
<p>Besides changing my domain name, I decided to use a new blogging engine to boot. Drupal, while great, is just so much more than I needed. When I got looking into upgrading my drupal installation, it was apparent that WordPress would be much easier to update (and if time ever permits, to extend.) Besides, the WordPress administration layout is much better streamlined. Really, I just needed a blog with the ability to upload a page or two. WordPress does this in a manner that is both simple and elegant.</p>
<p>Perhaps, I will even be inspired to blog about something worthwhile a little more often now with all this newly found ease!</p>
]]></content:encoded>
			<wfw:commentRss>http://brettic.us/2009/07/13/bretticus-org-gets-a-face-lift/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 74/100 queries in 0.042 seconds using memcached

Served from: brettic.us @ 2012-02-05 05:18:55 -->
