<?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>Scott Blaine &#187; security</title>
	<atom:link href="http://scottblaine.com/tag/security/feed" rel="self" type="application/rss+xml" />
	<link>http://scottblaine.com</link>
	<description>Omaha web developer</description>
	<lastBuildDate>Wed, 12 Oct 2011 01:00:22 +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>Form validation callbacks and private functions</title>
		<link>http://scottblaine.com/form-validation-callbacks-and-private-functions</link>
		<comments>http://scottblaine.com/form-validation-callbacks-and-private-functions#comments</comments>
		<pubDate>Tue, 02 Jun 2009 01:00:02 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=149</guid>
		<description><![CDATA[If you&#8217;re familiar with CodeIgniter you probably know about callbacks within form validation. Callbacks allow you to do your own validation of fields. For example, if you want to verify if a username is unique then you could create a username_check function to validate the field. You add the callback rule like this: $this->form_validation->set_rules( 'username', [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re familiar with CodeIgniter you probably know about <a title="Form Validation" href="http://codeigniter.com/user_guide/libraries/validation.html">callbacks within form validation</a>. Callbacks allow you to do your own validation of fields. For example, if you want to verify if a username is unique then you could create a username_check function to validate the field. You add the callback rule like this:</p>
<pre>
$this->form_validation->set_rules( 'username', 'Username',
  'trim|required|callback_username_check' );
</pre>
<p>And then create a matching function like this:</p>
<pre>
function username_check( $username )
{
  // some code
}
?>
</pre>
<p>However, as this is currently implemented someone could access your function as a page at a URL like example.com/index.php/login/<strong>username_check</strong>/ if they guessed the function name. While that may not have any ill side-effects, it&#8217;s probably just as well if no one can access the function besides you.</p>
<p>In come <a title="Controllers" href="http://codeigniter.com/user_guide/general/controllers.html">private functions for controllers</a>, which allow you to create a function like this:</p>
<pre>
function _utility()
{
  // some code
}
</pre>
<p>And if you try to access the function via a URL, like example.com/index.php/login/<strong>_utility</strong>/, you&#8217;ll get a 404 (page not found).</p>
<p>You probably see where I&#8217;m going with this. If you create your callbacks as private functions, no one will be able to access the callbacks as pages. It&#8217;s quite simple to do. You add an underscore before your callback function name:</p>
<pre>
function _username_check( $username )
{
  $valid_username = TRUE; // You would perform some kind of check on the field here

  if ($valid_username == FALSE)
  {
    $this->form_validation->set_message('_username_check', 'The username you have provided is not valid.');
    return FALSE;
  }
  else
  {
    return TRUE;
  }
}
</pre>
<p>And then add an underscore in your callback rule (note the two underscores after callback):</p>
<pre>
$this->form_validation->set_rules( 'username', 'Username',
  'trim|required|callback__username_check' );
</pre>
<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/form-validation-callbacks-and-private-functions/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

