<?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>Chris Monnat &#187; CodeIgniter</title>
	<atom:link href="http://www.christophermonnat.com/topics/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christophermonnat.com</link>
	<description>Programmer Extraordinaire</description>
	<lastBuildDate>Thu, 03 Dec 2009 19:24:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Facebook Connect Library for CodeIgniter</title>
		<link>http://www.christophermonnat.com/2009/05/facebook-connect-library-for-codeigniter/</link>
		<comments>http://www.christophermonnat.com/2009/05/facebook-connect-library-for-codeigniter/#comments</comments>
		<pubDate>Mon, 18 May 2009 22:00:52 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=762</guid>
		<description><![CDATA[Just a brief follow-up to the post series I did a while back on Implementing Facebook Connect with CI. Today, Elliot Haughin continued his tradition of releasing helpful CI libraries and published a new library for implementing Facebook Connect using CodeIgniter. Haven&#8217;t had a chance to review it personally yet but I would imagine it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Just a brief follow-up to the post series I did a while back on <a href="http://www.christophermonnat.com/guides">Implementing Facebook Connect with CI</a>. Today, Elliot Haughin continued his tradition of releasing helpful CI libraries and published a <a href="http://www.haughin.com/2009/05/18/new-codeigniter-library-facebook-connect/">new library for implementing Facebook Connect using CodeIgniter</a>. Haven&#8217;t had a chance to review it personally yet but I would imagine it&#8217;s just the thing to make implementing Facebook Connect on your CI site easier. Check it out!</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/01/implementing-facebook-connect-part-1/' rel='bookmark' title='Permanent Link: Implementing Facebook Connect (Part 1) &#8211; What is Facebook Connect?'>Implementing Facebook Connect (Part 1) &#8211; What is Facebook Connect?</a></li>
<li><a href='http://www.christophermonnat.com/2009/02/implementing-facebook-connect-part-3-accessing-data-from-facebook/' rel='bookmark' title='Permanent Link: Implementing Facebook Connect (Part 3) &#8211; Accessing Data from Facebook'>Implementing Facebook Connect (Part 3) &#8211; Accessing Data from Facebook</a></li>
<li><a href='http://www.christophermonnat.com/2008/08/ci-inferno/' rel='bookmark' title='Permanent Link: CI Inferno'>CI Inferno</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/05/facebook-connect-library-for-codeigniter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building Applications using CodeIgniter (Part 4) &#8211; Code Templates</title>
		<link>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/</link>
		<comments>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/#comments</comments>
		<pubDate>Wed, 13 May 2009 10:00:00 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=601</guid>
		<description><![CDATA[Last July I wrote a post entitled &#8220;What does your code say about you?&#8221; In that post I discuss the importance of writing clean well formatted code. It&#8217;s vitally important, even if you are the only programmer working on a project, that you document and format your code because you may be the only programmer [...]]]></description>
			<content:encoded><![CDATA[<p>Last July I wrote a post entitled &#8220;<a href="http://www.christophermonnat.com/2008/07/what-does-you-code-say-about-you/">What does your code say about you?</a>&#8221; In that post I discuss the importance of writing clean well formatted code. It&#8217;s vitally important, even if you are the only programmer working on a project, that you document and format your code because you may be the only programmer now but who knows about 6 months or a year from now. Plus, as a programmer, the code you write is a direct reflection on you as a professional.</p>
<p>So far in this series I&#8217;ve discussed my typical <a href="http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/">application structure</a>, <a href="http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/">configuration</a> and <a href="http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/">helper files</a> when developing apps using CodeIgniter (CI). In this final post I&#8217;ll review creating code templates for quick consistent development.<br />
<span id="more-601"></span></p>
<p>There is nothing really functional/technical about code templates. Templates are a set of simple files that have the basic document structure for the specified file (controller, model, library, etc.) that you use as a common starting point when creating any new file for your app. Your template should contain common elements to all files like the header comment block, class declaration, constructor, etc. There is no need to come up with your own standard when creating your templates because CI has a very nice style guide you should follow.</p>
<h2>CI Style Guide</h2>
<p>A few versions ago, CI added a page to their users guide entitled <a href="http://codeigniter.com/user_guide/general/styleguide.html">PHP Style Guide</a>. In this section they do a great job at outlining the proper format when declaring variables, writing comments, naming files, etc. This is a great place to start when creating your templates. If you don&#8217;t already, you should think about getting in the habit of following these standards (even if you aren&#8217;t programming with CI) because they will help keep your code clean and consistent.</p>
<h2>Template Files</h2>
<p>Before I start coding my app I typically create a template file and place it in the controller and model folders. The templates are different of course, but they both create a nice starting point whenever I need to create a new file. It&#8217;s important to note that you don&#8217;t necessarily have to create physical template files to accomplish clean and well formatted code. Some tools like <a href="http://www.panic.com/coda/">Coda</a> and <a href="http://macromates.com/">TextMate</a> give you the ability to save bits of text and reuse them in your files. So you could create your header comment block and save it in your editor and just call upon that whenever you create a new file. Doesn&#8217;t really matter how you do it, it just matters that you do.</p>
<h2>That&#8217;s a Wrap</h2>
<p>OK&#8230; lecture over. That does it for the building applications using CodeIgniter post series. If interested, you can <a href="http://www.christophermonnat.com/wp-content/uploads/2009/04/codeigniter_171.zip">download a copy</a> of the final CI install that we&#8217;ve created over the last 4 posts with helpers, code templates and all.</p>
<p>I hope by sheding some light on my process and explaining how I do things you came up with some ideas on how you can improve your own CI apps. As always, feel free to leave any questions or feedback as a comment below.</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 1) &#8211; File Structure'>Building Applications using CodeIgniter (Part 1) &#8211; File Structure</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 3) &#8211; Helpers'>Building Applications using CodeIgniter (Part 3) &#8211; Helpers</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 2) &#8211; Configuration'>Building Applications using CodeIgniter (Part 2) &#8211; Configuration</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building Applications using CodeIgniter (Part 3) &#8211; Helpers</title>
		<link>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/</link>
		<comments>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/#comments</comments>
		<pubDate>Fri, 08 May 2009 10:00:23 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=593</guid>
		<description><![CDATA[So far in this series we&#8217;ve discussed framework/application structure and configuration information. The next thing I always add to new applications is some common helper files I&#8217;ve developed and that come in handy in most projects. CodeIgniter (CI) helpers are very flexible tools to assist with common tasks throughout the app. If it doesn&#8217;t belong [...]]]></description>
			<content:encoded><![CDATA[<p>So far in this series we&#8217;ve discussed <a href="http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/">framework/application structure</a> and <a href="http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/">configuration information</a>. The next thing I always add to new applications is some common <a href="http://codeigniter.com/user_guide/general/helpers.html">helper files</a> I&#8217;ve developed and that come in handy in most projects. CodeIgniter (CI) helpers are very flexible tools to assist with common tasks throughout the app. If it doesn&#8217;t belong in a controller and is too small for a custom library then a helper might be just what the programmer ordered.</p>
<p><span id="more-593"></span></p>
<h2>What belongs in a helper?</h2>
<p>Something I&#8217;ve struggled with in the past is determining when a certain function or process belongs in a helper file and when it would be better off elsewhere. There is no cold hard rule for determining when something belongs in a helper and when it doesn&#8217;t but you do need to be aware of the following:</p>
<p>&#8220;Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.&#8221;<br />
- CI Users Guide</p>
<p>This means that in order to access libraries and models within your helper functions you will need to get a CI instance:</p>
<p>$CI =&amp; get_instance();</p>
<p>With the technicalities out of the way let&#8217;s take a look at the 2 helper files I&#8217;ve developed: <strong>display_helper.php</strong> and  <strong>flash_helper.php</strong>.</p>
<h2>display_helper.php</h2>
<p>A lot can be accomplished with CI&#8217;s built in templating but there are still some things that require logic and therefore fit nicely within a helper function. The display_helper.php file holds a number of common HTML display functions that I use throughout my program view files. To see what this file looks like, <a href="http://www.christophermonnat.com/wp-content/uploads/2009/04/display_helper.zip">download an example</a>. The functions in this file are pretty self-explanatory but I will briefly describe what each does below:</p>
<p><strong>display_error()</strong></p>
<p>The display_error() function displays a properly formatted HTML error message. I have this function setup so it can display 2 types of message: standard and form. When using the form_validation library I use the set_error_delimiter() method to wrap my error messages in list item tags and then display form error messages as unordered lists. So, I can easily accomplish displaying both types of messages using this function just by passing some variables.</p>
<p><strong>display_msg()</strong></p>
<p>This function just displays a properly formatted HTML message. This doesn&#8217;t necessarily need to be in a helper function because it&#8217;s so simple. But, the nice thing about having it here is if I ever decide to change the format of my messages I just need to come here in one place and make the update. So it consolidates my code nicely&#8230; but the same could probably be accomplished by using more generic CSS ID names.</p>
<p><strong>req_field()</strong></p>
<p>You should always identify required fields on your forms so users know what they need to fill in and what can be left blank. I&#8217;ve put the display of this flag in a helper function again to consolidate my code. As with the display_msg() function, if I ever decide to change my required field flag I just need to come to this one place and change it instead of going into all my view files and going a find and replace.</p>
<p><strong>js_confirm()</strong></p>
<p>JavaScript confirmation messages come in handy when you want to check with a user to make sure they are sure of something before you do it. You can accomplish the same confirmation by writing more PHP but that takes more work. The js_confirm() function, when used with the default <a href="http://codeigniter.com/user_guide/helpers/url_helper.html">CI URL helper</a> anchor() function, displays a browser pop-up message with whatever text I pass to the function. If I&#8217;m in a hurry and don&#8217;t provide a message the function just returns a generic &#8220;Are you sure?&#8221; message.</p>
<p>Your display_helper file might look much different than mine, this is just an example of how I use it. But you want to be on the look out while your coding for repetition and little details here and there that might be changing down the road. If you can consolidate those things in a helper file you will end up saving a lot of time updating down the road.</p>
<h2>flash_helper.php</h2>
<p>CI added flashdata to it&#8217;s <a href="http://codeigniter.com/user_guide/libraries/sessions.html">session library</a> a few versions back and I for one was happy to see it. Flashdata allows you to pass messages for display between pages.  Simple status messages like &#8220;record added&#8221; or &#8220;record not found&#8221; can be placed in a flashdata variable and then displayed after the user is redirected to another page. A very handy tool for letting your users know the results of their actions.</p>
<p>While I do like this functionality, writing $this-&gt;session-&gt;set_flashdata(&#8216;item&#8217;, &#8216;value&#8217;) and $this-&gt;session-&gt;flashdata(&#8216;item&#8217;) every time I need to set or get something is a lot of typing. So the flash_helper is a pair of wrapper functions that handle the job of setting and displaying flash messages for me. To see what this file looks like, <a href="http://www.christophermonnat.com/wp-content/uploads/2009/04/flash_helper.zip">download an example</a>.</p>
<p><strong>display_flash()</strong></p>
<p>The display_flash() function displays a selected flash message in a view file. Simply provide the name of the flash variable and it will display it in a properly formatted HTML div.</p>
<p><strong>set_flash()</strong></p>
<p>This function sets a flash variable with a value I specify. Since I use flash variables for a lot of different messages, which I may need to format differently, I also use this function to store some formatting data along with the message. That way when I display the message it is automatically displayed using the CSS style I intended.</p>
<p>The flash_helper file is based on the same concept that <a href="http://www.michaelwales.com/">Michael Wales</a> posted about a while back. I would link to the post but I can&#8217;t seem to find it anymore.</p>
<h2>Last up: Code Templates</h2>
<p>That wraps up the 3rd installment of this series. In the next, and final post, I&#8217;ll discuss creating code templates to help you maintain consistency between your files.</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 2) &#8211; Configuration'>Building Applications using CodeIgniter (Part 2) &#8211; Configuration</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 4) &#8211; Code Templates'>Building Applications using CodeIgniter (Part 4) &#8211; Code Templates</a></li>
<li><a href='http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 1) &#8211; File Structure'>Building Applications using CodeIgniter (Part 1) &#8211; File Structure</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building Applications using CodeIgniter (Part 2) &#8211; Configuration</title>
		<link>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/</link>
		<comments>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/#comments</comments>
		<pubDate>Mon, 04 May 2009 10:00:59 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=578</guid>
		<description><![CDATA[The framework that you use to build your application will end up being the foundation of your program. Everything that your program does will revolve around how that framework works. Things like naming conventions, file paths and settings are dictated by the framework. But if you have chosen a good framework, like CodeIgniter (CI), you [...]]]></description>
			<content:encoded><![CDATA[<p>The framework that you use to build your application will end up being the foundation of your program. Everything that your program does will revolve around how that framework works. Things like naming conventions, file paths and settings are dictated by the framework. But if you have chosen a good framework, like CodeIgniter (CI), you will be able to manipulate how that framework works and the tools it makes available to you by modifying it&#8217;s configuration information. In the <a href="http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure">first post</a> I talked about application file structure. Now that the structure is solid I&#8217;m going to look at how I configure the framework so that it works the way I expect.<br />
<span id="more-578"></span></p>
<h2>Config</h2>
<p>All of CI&#8217;s configuration files are stored in application/config.</p>
<p>- config<br />
&#8212; autoload.php<br />
&#8212; config.php<br />
&#8212; constants.php<br />
&#8212; database.php<br />
&#8212; doctypes.php<br />
&#8212; hooks.php<br />
&#8212; mimes.php<br />
&#8212; routes.php<br />
&#8212; smileys.php<br />
&#8212; user_agents.php</p>
<p>The purpose of each file coincides with it&#8217;s name. To get us up and running we are only going to worry about autoload.php, config.php, database.php and routes.php.</p>
<h2>config.php</h2>
<p>The config.php file is the main configuration file for the framework. In this file you can control things like logging, character sets, extension prefixs, etc. The folks over at <a href="http://www.ellislab.php">EllisLab</a> have done a great job at commenting this file so I&#8217;m not going to go through the whole thing. The only thing I&#8217;m going to change here are lines 14 and 26.</p>
<p>Line 14 holds the full web path to your CI installation. In the past I used to type this in by hand until I had the need to write portable apps that could be distributed and installed without much configuration on the users end. Since then I have resorted to using <a href="http://codeigniter.com/wiki/Automatic_configbase_url/">this code</a> I found on the CI wiki. By replacing line 14 with that code you can set this variable and forget about it. Wherever your program ends up this config variable will be set automatically so you don&#8217;t have to worry about it.</p>
<p>Line 26 of the config file holds the name of the index file. Since I use search engine friendly URLs where ever possible I just delete the value and leave it blank (just like the comment tells you to do).</p>
<h2>autoload.php</h2>
<p>Now that the config.php file is taken care of we turn our attention to the tools we need to build out app. What libraries, models, helpers, etc. are we going to need on a regular basis throughout the entire app? The answer to this question will probably differ based on what app your building, but I have found there are a number of common files I always load throughout any application:</p>
<p><strong>Libraries</strong> &#8211; database, session</p>
<p><strong>Helper</strong> &#8211; form, url, display, flash</p>
<p><strong>Config</strong> &#8211; program</p>
<p>The libraries above, database and session, are the default CI libraries to handle database abstraction and user sessions (note, if your app doesn&#8217;t use a database consistently throughout the whole app you might not want to autoload this library). The form and url helpers are both default CI helpers but display and flash are two custom helper files I&#8217;ve developed to assist with some common tasks. I will discuss helpers in the next post. Finally, program is a custom config file that I create for all my applications which I will discuss shortly.</p>
<h2>database.php</h2>
<p>If you are app is interacting with a database then you need to update the database.php config file. This file stores the access information for the database or databases that your app will be interacting with. This file is pretty self-explanitory so I&#8217;m wont go to delve into it.</p>
<h2>routes.php</h2>
<p>The routes.php file allows you to define custom URLs and map them to specfic controllers and methods. This is a very powerful tool that comes built into the CI framework. The only thing we want to update in this file to get started is line 43. Right now it&#8217;s set for the default welcome message we saw before. I usually set this variable to <strong>home</strong> just because it&#8217;s pretty generic and tends to work well in URLs but you can set it to whatever makes sense to you and your app.</p>
<h2>program.php</h2>
<p>Now that I&#8217;ve discussed all the default CI files, I&#8217;m going to create a custom config file called program.php (the same one we auto-loaded above) and place it in the config folder. This file will store all the application wide configuration settings and information that I will be referring to both programmtically and manually. To see what this file looks like you can <a href="http://www.christophermonnat.com/wp-content/uploads/2009/04/program.zip">dowload an example</a> for reference.</p>
<p>At the very least this file will always have 2 variables: <strong>ci_version</strong> and <strong>program_version</strong>. Both are pretty straight forward in purpose, ci_version stores the current version of CI that I&#8217;ve used to build the app and program_version is the current version of my app. I&#8217;ll typically call upon the program_version and display it in the footer of the app and knowing the version of CI running comes in handy when updating or making any framework modifications.</p>
<p>When building an app of any size you always want to build in a quick and easy way of taking the app offline. I accomplish this with the <strong>status</strong> variable. If the status is true then the program is online and everything is operational but if it&#8217;s false then things are offline. By creating a config variable for this I can easily refer to this flag throughout the program to determine whether or not input should be accepted and processed.</p>
<p>Another thing I commonly store in the program config file is e-mail settings that will be used whenever an e-mail is sent from the app. This commonly consists of the from and reply-to address and names. Again, by storing this information here it&#8217;s easily accessed whenever sending an e-mail.</p>
<p>Your program config file can store any type of system wide configuration information that your app will need to reference from many places. You could theoretically store this configuration information in a database but then you wouldn&#8217;t be able to take advantage of the native <a href="http://codeigniter.com/user_guide/libraries/config.html">CI config tools</a> available. In the end it&#8217;s up to you.</p>
<h2>Next up: Helpers</h2>
<p>That wraps up configuration. In the next post I will review some custom helper files I&#8217;ve developed and autoload in most of my applications.</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 3) &#8211; Helpers'>Building Applications using CodeIgniter (Part 3) &#8211; Helpers</a></li>
<li><a href='http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 1) &#8211; File Structure'>Building Applications using CodeIgniter (Part 1) &#8211; File Structure</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 4) &#8211; Code Templates'>Building Applications using CodeIgniter (Part 4) &#8211; Code Templates</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How-to Ensure a Secure Connection Using PHP</title>
		<link>http://www.christophermonnat.com/2009/05/how-to-ensure-a-secure-connection/</link>
		<comments>http://www.christophermonnat.com/2009/05/how-to-ensure-a-secure-connection/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:00:04 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=636</guid>
		<description><![CDATA[



Image via Wikipedia



When submitting sensitive information over the web it&#8217;s important to ensure that the requested page is being accessed via an HTTPS encrypted connection. I&#8217;ve come across some forms that don&#8217;t check whether a secure connection has been made or not. In other words, you can delete the S from HTTP and instead of [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignright" style="width: 171px;">
<dt class="wp-caption-dt"><a href="http://en.wikipedia.org/wiki/Image:SympaticoSecurityManagerSafe.png"><img title="‎Sympatico Security Manager" src="http://upload.wikimedia.org/wikipedia/en/9/99/SympaticoSecurityManagerSafe.png" alt="‎Sympatico Security Manager" width="161" height="131"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://en.wikipedia.org/wiki/Image:SympaticoSecurityManagerSafe.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>When submitting sensitive information over the web it&#8217;s important to <strong>ensure</strong> that the requested page is being accessed via an HTTPS encrypted connection. I&#8217;ve come across some forms that don&#8217;t check whether a secure connection has been made or not. In other words, you can delete the S from HTTP and instead of redirecting the user back to the HTTPS connection the form is just displayed unsecured. This is a BIG NO NO&#8230; as a programmer you cannot rely on the visitor, or even other developers who would be linking to the form, to request a form securely. In this post I will review how you can ensure that your users are accessing certain pages using a secure connection.<br />
<span id="more-636"></span></p>
<h2>The Server Superglobal</h2>
<p>How can you tell if your user is requesting a certain page using a secure connection (HTTPS)? Enter the <a href="http://www.php.net/manual/en/reserved.variables.server.php">PHP server surperglobal</a>.</p>
<p>&#8220;<var class="varname">$_SERVER</var> is an array containing information    such as headers, paths, and script locations. The entries in this    array are created by the web server.&#8221;<br />
- PHP Manual (http://www.php.net)</p>
<p>There are 2 elements within this array that you can check that will tell you whether the user has made a secure request or not: <strong>https</strong> and <strong>server_port</strong>.</p>
<h2>Programmer Beware</h2>
<p>Something to keep in mind is that each web server will provide or not provide certain information in the $_SERVER array depending on their configuration. The PHP manual also points this out:</p>
<p>&#8220;There is no guarantee that    every web server will provide any of these; servers may omit some,    or provide others not listed here.&#8221;<br />
- PHP Manual (http://www.php.net)</p>
<p>So I guess that brings us back to the original question: how can you tell if your user is requesting a certain page using a secure connection (HTTPS)? Of the 2 elements mentioned above, server_port is part of the CGI 1.1 specification so the chances are good that element will be available in most servers. You can check for the availability of the https element if you wish but you should also include a check of the server_port as a fallback.</p>
<h2>The Code</h2>
<p>There is a good code example in the <a href="http://www.php.net/manual/en/reserved.variables.server.php#89306">comments of the PHP manual</a> of a function that checks whether or not the user has made a secure request:</p>
<pre class="brush: php">
&lt;?php
function isSSL(){

if($_SERVER[&#039;https&#039;] == 1) /* Apache */ {
return TRUE;
} elseif ($_SERVER[&#039;https&#039;] == &#039;on&#039;) /* IIS */ {
return TRUE;
} elseif ($_SERVER[&#039;SERVER_PORT&#039;] == 443) /* others */ {
return TRUE;
} else {
return FALSE; /* just using http */
}

}
?&gt;
</pre>
<p>This example function makes use of both the https and server_port elements of the superglobal array and also takes into account the different values that might be provided based on the web server. The only thing I will mention about using the server_port element is to make sure you know what port your server is using for HTTPS connections. I believe 443 is the standard, but ports can be changed so you just want to make sure you are checking the correct port for your server.</p>
<h2>What about CodeIgniter (CI)?</h2>
<p>If your programming using CI, you could certainly put the function above in a helper and call it whenever necessary. Or, a technique I&#8217;ve used successfully in the past, is to put the check in a custom library and auto load it. When you do it this way the connection is tested every time a page is loaded automatically without you having to make any additional function calls. And instead of returning TRUE or FALSE you can simply redirect the user to the requested page using HTTPS instead of HTTP which truly automates the process.</p>
<p>One thing that can trip you up when making secure connections using CI is the address you&#8217;ve entered on line 14 of application/config/config.php. If you enter just a static address starting with HTTP then HTTP will be used when calling any URL helper function like site_url() or anchor(). To avoid this issue,&nbsp; you can replace line 14 with the following code from the <a href="http://codeigniter.com/wiki/Automatic_configbase_url/">CI wiki</a>:</p>
<pre class="brush: php">

$config[&#039;base_url&#039;] = ((isset($_SERVER[&#039;HTTPS&#039;]) &amp;amp;&amp;amp; $_SERVER[&#039;HTTPS&#039;] == &quot;on&quot;) ? &quot;https&quot; : &quot;http&quot;);
$config[&#039;base_url&#039;] .= &quot;://&quot;.$_SERVER[&#039;HTTP_HOST&#039;];
$config[&#039;base_url&#039;] .= str_replace(basename($_SERVER[&#039;SCRIPT_NAME&#039;]),&quot;&quot;,$_SERVER[&#039;SCRIPT_NAME&#039;]); 
</pre>
<p>This code will automatically set the base_url config element so you don&#8217;t have to. This also comes in real handy when writing portable code that you want to distribute to other users or clients for installation on their own servers.</p>
<h2>That&#8217;s a wrap</h2>
<p>That does it for making sure your files are being accessed securely. If you have any questions or use a different technique for checking secure connections please share them by posting a comment.</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2008/09/customizing-domains-with-wildcard-dns/' rel='bookmark' title='Permanent Link: Customizing Domains With Wildcard DNS'>Customizing Domains With Wildcard DNS</a></li>
<li><a href='http://www.christophermonnat.com/2009/02/implementing-facebook-connect-part-3-accessing-data-from-facebook/' rel='bookmark' title='Permanent Link: Implementing Facebook Connect (Part 3) &#8211; Accessing Data from Facebook'>Implementing Facebook Connect (Part 3) &#8211; Accessing Data from Facebook</a></li>
<li><a href='http://www.christophermonnat.com/2008/08/generating-pdf-files-using-codeigniter/' rel='bookmark' title='Permanent Link: Generating PDF files using CodeIgniter'>Generating PDF files using CodeIgniter</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/05/how-to-ensure-a-secure-connection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Applications using CodeIgniter (Part 1) &#8211; File Structure</title>
		<link>http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/</link>
		<comments>http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 10:00:26 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=570</guid>
		<description><![CDATA[Creating web applications with CodeIgniter (CI) is quick and easy because CI handles a lot of the typical application requirements right out of the box (like session management, database abstraction and file uploading). I&#8217;ve developed a number of applications with CI now, including BadgeTracker and Sign-Up-Sheet.com, and while CI does handle the repetitive stuff it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Creating web applications with <a href="http://www.codeigniter.com">CodeIgniter</a> (CI) is quick and easy because CI handles a lot of the typical application requirements right out of the box (like session management, database abstraction and file uploading). I&#8217;ve developed a number of applications with CI now, including <a href="http://www.badgetracker.com">BadgeTracker</a> and <a href="http://www.sign-up-sheet.com">Sign-Up-Sheet.com</a>, and while CI does handle the repetitive stuff it&#8217;s still up to you to create a scalable and easy to update application. Over the last couple of years I&#8217;ve come up with a pretty solid structure and set of files that I use whenever I&#8217;m building a new app and in this series I&#8217;m going to show you what I do so you can get ideas for your own apps.</p>
<p><span id="more-570"></span>In this first part of the series I&#8217;m going to talk about file structure. The first step to building your app is to get your folder and file structure setup so you have an idea where everything is going to go. If you wait to figure this out as you go along you run the risk of inconsistencies in your code so it&#8217;s good practice to just settle on a structure first thing.</p>
<h2>CodeIgniter Default</h2>
<p>By default, CI (version 1.7.1) presents you with the following structure:</p>
<p>- system<br />
&#8212; application<br />
&#8212;&#8212; application folders&#8230;<br />
&#8212; cache<br />
&#8212; codeigniter<br />
&#8212; database<br />
&#8212; fonts<br />
&#8212; helpers<br />
&#8212; language<br />
&#8212; libraries<br />
&#8212; logs<br />
&#8212; plugins<br />
&#8212; scaffolding</p>
<p>Everything lives inside the system folder. This isn&#8217;t necessarily a bad thing, but every time you want to get to your application to add or update files it&#8217;s just a hassle to go into system to get to your application. So, it&#8217;s pretty common practice to move your application folder outside of the system folder so it&#8217;s front and center in the document root (or whatever folder your developing in).</p>
<p>To accomplish this you will need to update the main index.php file outside of the system folder. Line 43 needs to change to &#8220;../application&#8221; instead of &#8220;application&#8221;. Once that update is done then CI shouldn&#8217;t be able to tell the difference.</p>
<h2>Application Assets</h2>
<p>It might be because I&#8217;m a neat freak or maybe it&#8217;s because of the brief exposure I had with Ruby on Rails (RoR) but I don&#8217;t like having my application root littered with folders for images and JavaScript files and such. I like a nice clean document root. So I create a folder at the root of my application called <strong>assets</strong>. Inside the assets folder I create 3 other directories for images, JavaScript and CSS files so it looks something like this:</p>
<p>- assets<br />
&#8212; images<br />
&#8212; js<br />
&#8212; css</p>
<p>Now all of my non-php files are nicely organized in a single directory at the top of my application. From time to time I&#8217;ll also add a directory for videos but you get the idea.</p>
<h2>Template Files</h2>
<p>Another one of those common application tasks that CI handles, like I mentioned earlier, is templating. The views folder (inside application) makes up the V in MVC and that&#8217;s where all your design/display files should go. It&#8217;s taken me some time but I&#8217;ve settled on a pretty standard way I handle my application templating and it all revolves around a folder I create within views called <strong>_templates</strong>.</p>
<p>Instead of loading a bunch of view files one after another to make up my page, I prefer to create layout files which are complete web pages just with some variables acting as place folders for content to be inserted later. When it&#8217;s time to display a page I load a couple of partial views into variables (by returning the views as strings instead of displaying them) and then passing those variables to my master view file for display. The main benefit of this type of templating structure is that your whole application can use a just a hand full of layout files to handle page structure.</p>
<p>Within the _templates folder I create another folder called layouts. This is where the actual layout files live. Other include/template files are placed inside the _templates folder that are included in more than one layout file like navigation elements and such. I will also typically create other folders within _templates for things like system e-mails and the like.</p>
<h2>Final Structure</h2>
<p>That just about does it for our structure. I&#8217;ve listed the new structure below with just the new directories we should be left with and where they live.</p>
<p>- application<br />
&#8212; views<br />
&#8212;&#8212; _templates<br />
&#8212;&#8212;&#8212; layouts<br />
- assets<br />
&#8212; css<br />
&#8212; images<br />
&#8212; js<br />
- system</p>
<p>At this point I typically move the files to the development environment and make sure the default CI welcome message is displayed. If your following along at home and don&#8217;t see the welcome screen you more than likely didn&#8217;t make the change to the index.php file I mentioned above when we moved the application folder outside system. Double check that and if all was done correctly you should be all set. If you continue to have problems, leave a comment and I&#8217;ll see what I can do to help.</p>
<h2>Next up: Configuration</h2>
<p>In part two of this series I&#8217;ll talk about configuring CI to load the common libraries we&#8217;ll need and creating a custom config file to keep information specific to our application.</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-2-configuration/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 2) &#8211; Configuration'>Building Applications using CodeIgniter (Part 2) &#8211; Configuration</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 4) &#8211; Code Templates'>Building Applications using CodeIgniter (Part 4) &#8211; Code Templates</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 3) &#8211; Helpers'>Building Applications using CodeIgniter (Part 3) &#8211; Helpers</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/04/building-applications-using-codeigniter-part-1-file-structure/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>CodeIgniter in php&#124;architect Magazine</title>
		<link>http://www.christophermonnat.com/2009/03/codeigniter-in-phparchitect-magazine/</link>
		<comments>http://www.christophermonnat.com/2009/03/codeigniter-in-phparchitect-magazine/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 14:09:22 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=560</guid>
		<description><![CDATA[For those of you who havn&#8217;t given CI a try yet or are just starting out, Chris Cornutt wrote a nice intro. article for this months issue of php&#124;architect magazine. Check it out!


Related posts:JPG Magazine Says Goodbye
Building Applications using CodeIgniter (Part 4) &#8211; Code Templates
]]></description>
			<content:encoded><![CDATA[<p>For those of you who havn&#8217;t given CI a try yet or are just starting out, Chris Cornutt wrote a nice intro. article for <a href="http://www.phparch.com/c/magazine/issue/93">this months issue</a> of <a href="http://www.phparch.com">php|architect magazine</a>. Check it out!</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/01/jpg-magazine-says-goodbye/' rel='bookmark' title='Permanent Link: JPG Magazine Says Goodbye'>JPG Magazine Says Goodbye</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-4-code-templates/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 4) &#8211; Code Templates'>Building Applications using CodeIgniter (Part 4) &#8211; Code Templates</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2009/03/codeigniter-in-phparchitect-magazine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CI 1.7.0 Released</title>
		<link>http://www.christophermonnat.com/2008/10/ci-170-released/</link>
		<comments>http://www.christophermonnat.com/2008/10/ci-170-released/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 18:53:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=244</guid>
		<description><![CDATA[The folks over at EllisLab have released CodeIgniter 1.7.0. The highlight of this release appears to be a brand new validation library as well as a number of other enhancements and bug fixes. Check out the change log for more information then download the latest.
On another note, I know it&#8217;s been quiet here for the [...]]]></description>
			<content:encoded><![CDATA[<p>The folks over at <a href="http://www.ellislab.com">EllisLab</a> have released <a href="http://www.codeigniter.com">CodeIgniter</a> 1.7.0. The highlight of this release appears to be a brand new validation library as well as a number of other enhancements and bug fixes. Check out the <a href="http://codeigniter.com/user_guide/changelog.html">change log</a> for more information then <a href="http://codeigniter.com/downloads/">download the latest</a>.</p>
<p>On another note, I know it&#8217;s been quiet here for the last month or so. I have been trying to get BadgeTracker 4.0 in shape for an early November release so I&#8217;ve been heads down coding. More soon&#8230;</p>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/08/update/' rel='bookmark' title='Permanent Link: Update'>Update</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2008/10/ci-170-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customizing Domains With Wildcard DNS</title>
		<link>http://www.christophermonnat.com/2008/09/customizing-domains-with-wildcard-dns/</link>
		<comments>http://www.christophermonnat.com/2008/09/customizing-domains-with-wildcard-dns/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 13:00:45 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[How-to]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=109</guid>
		<description><![CDATA[It seems to be the &#8220;in&#8221; feature of most modern web apps today. Giving your users the ability to create a custom URL that they can use to access their account like http://chickenbbq.sign-up-sheet.com. From a feature stand point its nice because it&#8217;s another way users can customize your app to their specifications. From a developers [...]]]></description>
			<content:encoded><![CDATA[<p>It seems to be the &#8220;in&#8221; feature of most modern web apps today. Giving your users the ability to create a custom URL that they can use to access their account like http://chickenbbq.sign-up-sheet.com. From a feature stand point its nice because it&#8217;s another way users can customize your app to their specifications. From a developers perspective however it can be a bit challenging to implement if you don&#8217;t know what your doing.<br />
<span id="more-109"></span></p>
<p>When I was developing my first web app (version 1 of <a href="http://www.badgetracker.com">BadgeTracker</a>) I really wanted to implement this feature, but for the life of me I couldn&#8217;t figure out how to do it. I didn&#8217;t even really know what it was called so I had a hell of a time searching for information. I then built <a href="http://www.sign-up-sheet.com">Sign-Up-Sheet.com</a> and still had no idea how to accomplish the effect, but I really needed to because each sign-up-sheet needed to have it&#8217;s own address and a dynamic address would have been perfect. I don&#8217;t really recall how I finally figured out what I was looking for, but the answer to my prayers was <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">Wildcard DNS</a>. In this post I will walk you through implementing Wildcard DNS with your next web app.</p>
<h2>Disclaimer</h2>
<p>Before I get started I just wanted to mention that I do not claim to be a networking person. I don&#8217;t enjoy networking or server administration or anything like that&#8230; I&#8217;m software not hardware <img src='http://www.christophermonnat.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . However, this was a topic that I personally struggled with when starting out and I wanted to pass along what I have found and done.</p>
<h2>What is wildcard DNS?</h2>
<p>Wildcard DNS is typically defined as: an address record you can place in the DNS for your domain that will send all subdomains (not otherwise declared in your DNS) to a specified IP address. The concept is simple once you figure it out. Adding a Wildcard DNS record to your existing host records looks something like this:</p>
<p><strong>*.christophermonnat.com A 10.5.128.1</strong></p>
<p>The astrisk in this case is the wildcard character. This A record directs all subdomain requests to the IP 10.5.128.1. So now all you have to do is place you web app at the same IP address and you will be well on your way.</p>
<h2>Do I need a dedicated IP?</h2>
<p>It depends on how your app works. With Sign-Up-Sheet I have one CodeIgniter installation running 2 parts of the site. There is the website/admin area where users can browse the features of the program or sign-in to administer their account. And there is the actual public sign-up-sheet&#8217;s themselves which need to be displayed when a user enters the specified sheets address into the browser window. Because my app has 2 parts but is using the same CI installation I decided to use a shared IP for Sign-Up-Sheet.</p>
<p>With BadgeTracker there is only one CI installation but it has nothing to do with the website. The website and application are 2 physically separate things so the easiest way to facilitate this architecture was to get a dedicated IP for the application and leave the website with a shared IP. All subdomains go to the dedicated IP while the WWW record points to the shared.</p>
<p>When asking yourself this question I guess the determining factor boils down to how many parts there are to your app and whether they all belong to a single CI installation or many separate ones.</p>
<h2>Wildcard DNS and CI</h2>
<p>As I mentioned above, with Sign-Up-Sheet I have one CI install that handles both the display of sign-up-sheets decided by the URL and the front-end website/admin area. Since I&#8217;m using a shared IP I chose to have CI handle the determination of what to display to the user and when.</p>
<p>When doing something like this, there are 2 files you need to be concerned with: <strong>config/config.php</strong> and <strong>config/routes.php</strong>. In config.php, the issue is the base_url variable. This needs to be set to the home of your app in order for CI to operate properly. The problem with our implementation however is that it may change depending on what the user is requesting. Sometimes it will be a subdomain and other times it will be straight WWW. One way to account for this is to generate the base_url setting dynamically.</p>
<p>[code]<br />
$config['base_url'] = ((isset($_SERVER['HTTPS']) &#038;&#038; $_SERVER['HTTPS'] == "on") ? "https" : "http");<br />
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];<br />
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);<br />
[/code]</p>
<p>The above example is from the <a href="http://codeigniter.com/wiki/Automatic_configbase_url/">CI Wiki</a>. By placing this code in your config.php file, the base_url will be dynamically generated based on what is being requested thereby solving that problem. The other file we need to be concerned with is routes.php. The issue here is that we want to serve the user with a different default controller if they are visiting the site using a subdomain vs. just WWW. We can accomplish this with the following code:</p>
<p>[code]<br />
if(preg_match("/^([a-zA-Z0-9\-_]+)\.([a-zA-Z0-9\-_]+)\.([a-zA-Z]{2,5})$/",$_SERVER["SERVER_NAME"],$matches))<br />
{<br />
  list($subdomain,$domain,$tld) = $matches;<br />
}</p>
<p>if($domain == 'www'):<br />
	$route['default_controller'] = "main";<br />
else:<br />
	$route['default_controller'] = "signup";<br />
endif;<br />
[/code]</p>
<p>The above code looks at the URL and gets just the subdomain portion. We then look at the subdomain and determine if its WWW or something else. From there we decide which default controller to set for the application. Once placed in your routes.php file this accomplishes our goal nicely. Now that we are pointing our users to the correct controller we can refer to our database to find the records associated with the requested subdomain and proceed from there.</p>
<h2>Conclusion</h2>
<p>Wildcard DNS is available with most popular DNS providers like <a href="http://www.enom.com">Enom, Inc.</a> and <a href="http://www.godaddy.com">GoDaddy</a> and is also available with popular control panels like Plesk. If your not sure if your provider offers Wildcard DNS you should ask because there are some out there that don&#8217;t. Something I did want to mention about using Wildcard DNS is that it&#8217;s not necessarily the answer to all of your problems and should be implemented after careful consideration. The makers of <a href="http://www.wufoo.com">Wufoo</a>, a popular app that makes hosted web forms, wrote <a href="http://particletree.com/notebook/subdomains-development-sucks/">a post</a> a while ago about the issues they experienced while working with this kind of setup. It&#8217;s a must read before adopting this concept for your own apps.</p>
<p>Well, there you have it Wildcard DNS demystified. If you have any questions or if you are a networking/DNS genius and you see something I have gotten terribly wrong please post a comment and let me know.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/25d3cf69-5471-453f-8e85-68fd98e7ad97/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=25d3cf69-5471-453f-8e85-68fd98e7ad97" alt="Reblog this post [with Zemanta]"></a></div>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2009/05/how-to-ensure-a-secure-connection/' rel='bookmark' title='Permanent Link: How-to Ensure a Secure Connection Using PHP'>How-to Ensure a Secure Connection Using PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2008/09/customizing-domains-with-wildcard-dns/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to use reCAPTCHA with CI</title>
		<link>http://www.christophermonnat.com/2008/09/how-to-use-recaptcha-with-ci/</link>
		<comments>http://www.christophermonnat.com/2008/09/how-to-use-recaptcha-with-ci/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 16:46:12 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[How-to]]></category>

		<guid isPermaLink="false">http://www.christophermonnat.com/?p=160</guid>
		<description><![CDATA[Image by mathowie via Flickr 
SPAM&#8230; a nasty little four letter word. Could mean tasty meat in a can or could mean junk e-mails and form submissions clogging your server and taking up space in your inbox. We are all familiar with it and are engaged in a constant battle to stop it. In this [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; float: right; display: block;"><a href="http://www.flickr.com/photos/12037949644@N01/2366107675"><img style="display: block;" src="http://farm4.static.flickr.com/3095/2366107675_9905c51fe7_m.jpg" alt="ReCAPTCHA's quality is going down"></a><span class="zemanta-img-attribution">Image by <a href="http://www.flickr.com/photos/12037949644@N01/2366107675">mathowie</a> via Flickr </span></div>
<p>SPAM&#8230; a nasty little four letter word. Could mean tasty meat in a can or could mean junk e-mails and form submissions clogging your server and taking up space in your inbox. We are all familiar with it and are engaged in a constant battle to stop it. In this tutorial I&#8217;m going to show you how to implement a popular 3rd party <a href="http://en.wikipedia.org/wiki/CAPTCHA" title="CAPTCHA" rel="wikipedia" class="zem_slink">CAPTCHA</a> service with your CodeIgniter application.</p>
<p><span id="more-160"></span>The commonly accepted approach of handling form SPAM is by using a CAPTCHA control on your forms. The good news is that CodeIgniter comes with it&#8217;s own built in CAPTCHA functionality included <a href="http://codeigniter.com/user_guide/general/plugins.html">in the form of a plug-in</a>. I&#8217;ve used their plug-in before and it works well. It takes a little bit to get it setup and working properly and requires it&#8217;s own database table but it gets the job done. </p>
<p>There is, however, another CAPTCHA tool which I have been using a lot lately called <a href="http://www.recaptcha.org">reCAPTCHA</a> and I like it better. reCAPTCHA is a free hosted CAPTCHA service provided by Carnegie Mellon University and serving over 60 million CAPTCHAs a day. This service allows you to integrate a CAPTCHA control on your site quickly and easily without any database modifications or a lot of coding. Below are step-by-step instructions for integrating reCAPTCHA in your CI app.</p>
<h2>Step 1 &#8211; Create a reCAPTCHA account</h2>
<p>Before you can start working with reCAPTCHA you will first need to visit their website and <a href="https://admin.recaptcha.net/accounts/signup/">create an account</a>. Once you have filled out the form you will be logged in and prompted to enter your first site. Each site you wish to protect with reCAPTCHA needs to be entered in your account. This is because each site will get it&#8217;s own unique private and public keys which you will need to interact with the reCAPTCHA API. So, enter your domain name and click <strong>Create Key</strong>.</p>
<p>Once you have entered your domain you will now be presented with your public and private key. Take a minute to copy and paste those two long strings into a text document for later use. Be sure to keep track of which is which or else things won&#8217;t work properly.</p>
<h2>Step 2 &#8211; Download the reCAPTCHA PHP library</h2>
<p>Now that you have your keys, you will want to <a href="http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest">download</a> the PHP library they provide for interacting with the reCAPTCHA API. The archive you download will contain two example files and a code file called <strong>recaptchalib.php</strong>. Once you have the archive, unzip it and move the recaptchalib.php file into your applications helpers folder.</p>
<h4>Why a helper and not a library?</h4>
<p>We could certainly turn what they provided us into a library, but that would involve changing some of the code they provided since they are not providing a class. The recaptchalib.php contains a number of functions which are meant to help while interacting with the API. Therefore, it fits the helper definition nicely.</p>
<h2>Step 3 &#8211; Use the library in your project</h2>
<p>Now that we have the code we can use it in our application. The first thing we need to do is display the CAPTCHA control on the form your trying to protect. So, first load the helper:</p>
<pre class="brush: php">
$this-&gt;load-&gt;helper(&#039;recaptchalib&#039;);
</pre>
<p>Now that we have access to the helper functions, we can use the <strong>recaptcha_get_html()</strong> function.</p>
<pre class="brush: php">
&lt;?php echo recaptcha_get_html($public_key); ?&gt;
</pre>
<p>You will want to place this function in your view where you want to display the form control. This function takes only one argument: the public key you received earlier when you added your site to your reCAPTCHA account. You can either add both the public and private keys as class variables to your current controller. Or, my preference is to add the public and private keys to a custom config file that I autoload throughout my entire app. That way I can use the control anyplace in my site without duplicating my keys.</p>
<p>Now that we have the control displaying on our form we need to add code to handle the validation.</p>
<pre class="brush: php">
&lt;?php

// Validate the captcha submission.
function val_recaptcha($string)
{
	$resp = recaptcha_check_answer($this-&gt;config-&gt;item(&#039;recap_private&#039;),
									$_SERVER[&quot;REMOTE_ADDR&quot;],
									$this-&gt;input-&gt;post(&quot;recaptcha_challenge_field&quot;),
									$this-&gt;input-&gt;post(&quot;recaptcha_response_field&quot;));

	if(!$resp-&gt;is_valid) {
		$this-&gt;validation-&gt;set_message(&#039;val_recaptcha&#039;,&#039;Your answer for the security question was incorrect, please try again.&#039;);
		return FALSE;
	}
	else {
		return TRUE;
	}
}

function process_form()
{
	$this-&gt;load-&gt;library(&#039;validation&#039;);
	$this-&gt;load-&gt;helper(&#039;recaptcha_helper&#039;);

	$this-&gt;validation-&gt;set_error_delimiters(&#039;&lt;li&gt;&#039;, &#039;&lt;/li&gt;&#039;);

	// Validation rules.
	$rules[&#039;recaptcha_challenge_field&#039;] = &#039;required|callback_val_recaptcha&#039;;
	$this-&gt;validation-&gt;set_rules($rules);

	// Validation field names (for display in error messages).
	$fields[&#039;recaptcha_challenge_field&#039;] = &#039;Security Question&#039;;
	$this-&gt;validation-&gt;set_fields($fields);

	if($this-&gt;validation-&gt;run() == FALSE) {
		// display errors
	}
	else {
		// process submission
	}
}

?&gt;
</pre>
<p>In the above code we are using CI&#8217;s built in validation library to validate the CAPTCHA. You can of course add any additional rules or fields that are needed. I&#8217;ve created a custom validation function called <strong>val_recaptcha()</strong> which is called via a callback within the validation rules on the recaptcha_challenge_field field (which is created when we used the <strong>recaptcha_get_html()</strong> function above). This validation function uses the <strong>recaptcha_check_answer()</strong> function from the reCAPTCHA helper to check whether what the user submitted is correct or not.</p>
<p>And that&#8217;s really all there is to it. You are now using the same CAPTCHA protection as <a href="http://www.facebook.com/r.php?r=200">Facebook</a> and many other sites throughout the web. </p>
<h2>Wait&#8230; it&#8217;s RED! Can I make it look better?</h2>
<p>Absolutely, the reCAPTCHA website has detailed <a href="http://recaptcha.net/apidocs/captcha/client.html#customization">instructions</a> for how to alter the look and feel of the reCAPTCHA form control. There are a number of pre-set themes which you can choose from or you can create your own.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/5a8d6c8d-f133-484b-8284-17f574a68ffd/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=5a8d6c8d-f133-484b-8284-17f574a68ffd" alt="Reblog this post [with Zemanta]"></a></div>


<p>Related posts:<ol><li><a href='http://www.christophermonnat.com/2008/08/generating-pdf-files-using-codeigniter/' rel='bookmark' title='Permanent Link: Generating PDF files using CodeIgniter'>Generating PDF files using CodeIgniter</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/how-to-ensure-a-secure-connection/' rel='bookmark' title='Permanent Link: How-to Ensure a Secure Connection Using PHP'>How-to Ensure a Secure Connection Using PHP</a></li>
<li><a href='http://www.christophermonnat.com/2009/05/building-applications-using-codeigniter-part-3-helpers/' rel='bookmark' title='Permanent Link: Building Applications using CodeIgniter (Part 3) &#8211; Helpers'>Building Applications using CodeIgniter (Part 3) &#8211; Helpers</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.christophermonnat.com/2008/09/how-to-use-recaptcha-with-ci/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
