<?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; CodeIgniter</title>
	<atom:link href="http://brettic.us/category/tech/programming/php/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettic.us</link>
	<description>Web devlopment and life in general</description>
	<lastBuildDate>Fri, 23 Mar 2012 16:58:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<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_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">application/libraries/MY_form_validation.php</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="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_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">application/config/form_validation.php</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="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_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">application/controllers/mycontroller.php</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="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>
<p>&nbsp;</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">application/libraries/MY_Input.php</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="php"><span class="kw2">&lt;?php</span>
<span class="kw2">class</span> MY_Input <span class="kw2">extends</span> CI_input <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">function</span> _sanitize_globals<span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="co1">// setting allow_get_array to true is</span>
	<span class="co1">// the only real modification</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">allow_get_array</span> <span class="sy0">=</span> <span class="kw4">TRUE</span><span class="sy0">;</span>
&nbsp;
        parent<span class="sy0">::</span>_sanitize_globals<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span>
<span class="coMULTI">/* End of file MY_Input.php */</span>
<span class="coMULTI">/* Location: .application/libraries/MY_Input.php</span></pre></div></div>
<p>&nbsp;</p>
<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>
<p>&nbsp;</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">Use parse_str to rebuild $_GET</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="php"><span class="kw2">&lt;?php</span>
<span class="co1">//Remove everything before (and including)</span>
<span class="co1">//the question mark and then parse into $_GET array.</span>
<a href="http://www.php.net/parse_str"><span class="kw3">parse_str</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><a href="http://www.php.net/strrchr"><span class="kw3">strrchr</span></a><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st_h">'REQUEST_URI'</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="st0">&quot;?&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="re0">$_GET</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="st_h">' Using rebuilt $_GET array with xss protection: '</span> <span class="sy0">.</span>
	<span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="sy0">-&gt;</span><span class="me1">xss_clean</span><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st_h">'foo'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>
<p>&nbsp;</p>
<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_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">google_analytics.php</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="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_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">hooks.php</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="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_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="#config.php"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#config.php"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#config.php" 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="#config.php" 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="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>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>
	</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 52/66 queries in 0.036 seconds using memcached

Served from: brettic.us @ 2012-05-20 01:36:27 -->
