<?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>Security And Caffeine &#187; coding</title>
	<atom:link href="http://www.securityandcaffeine.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.securityandcaffeine.com</link>
	<description>something witty here</description>
	<lastBuildDate>Thu, 12 Feb 2009 22:42:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Better Form Processing</title>
		<link>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/</link>
		<comments>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 08:03:01 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=22</guid>
		<description><![CDATA[This is a much condensed version of the original post. Usually, processing form data means getting either POST or GET data from a form, and trying to figure out, in code, what you have, and then do something with it.  This can be easy or complicated, depending on how much is being passed in.  Email [...]]]></description>
			<content:encoded><![CDATA[<p>This is a much condensed version of the original post.</p>
<p>Usually, processing form data means getting either POST or GET data from a form, and trying to figure out, in code, what you have, and then do something with it.  This can be easy or complicated, depending on how much is being passed in.  Email address only? Easy.  Checkboxes, optional fields, and so on, all together?  Pain.  Often, a lot of form processing is done with stacks of &#8220;if&#8221; statements.  This sucks.  Here is a better way:</p>
<p>From now on, I want you to name all of your &#8220;real&#8221; form elements (ones that have data that could change, so not buttons) using the name you would have given then, <em>plus an array name</em>, that they will all share.</p>
<p>So,</p>
<pre>&lt;input type="text" name="username" id="username" /&gt;</pre>
<p>becomes</p>
<pre>&lt;input type="text" name="formdata[username]" id="username" /&gt;</pre>
<p>Why?  Instead of having one array ($_POST), you&#8217;ll now have two ($_POST and &#8216;formdata&#8217;, within $_POST).  Your buttons and other &#8220;static&#8221; form elements will still live in $_POST, but everything containing data that needs handling will be in the &#8216;formdata&#8217; array, which you can access as $_POST[formdata].</p>
<p>Why does this rock?  I find I have to go through all of the data in a form, even if I know what and how much is coming in, and that the amount wont change.  For example, it&#8217;s a good idea to clean all user input.  Since users can change any form value, this needs handled.  Why bother with a ton of &#8216;if&#8217; statements when we can just iterate through the &#8216;formdata&#8217; array?</p>
<pre>  foreach ($_POST['formdata'] as $key =&gt; $value)
    $_POST['formdata'][$key] = makesafe($value);</pre>
<p>Cautionary note: this definitely seems ideal for updating data in a database and saving time, doesn&#8217;t it?  Maybe something like (let&#8217;s assume &#8220;$user_id&#8221; is the current user&#8217;s account, and we determined that earlier somehow) :</p>
<pre>  foreach ($_POST['formdata'] as $key =&gt; $value)
    mysql_query("UPDATE info_table SET $key = '$value' WHERE index = $user_id");</pre>
<p>This is a bad thing.  Why?  SQL injection.  At the very least, clean <em>all</em> input, including the field names, if you think something like the above statement is a good idea.  If you don&#8217;t, don&#8217;t say I didn&#8217;t warn you if you get owned.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F06%2F16%2Fbetter-form-processing%2F&amp;linkname=Better%20Form%20Processing"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/06/16/better-form-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>U@T Rogue Forums</title>
		<link>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/</link>
		<comments>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/#comments</comments>
		<pubDate>Fri, 23 May 2008 04:18:57 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=11</guid>
		<description><![CDATA[During the Spring Semester of 2007, I worked as the lead developer on the UAT Rogue Forums.  The project was designed and lead by Jordan Womack, a UAT Master&#8217;s Student.  Under him, the Rogue Forums was created as an alternative to UAT&#8217;s intranet forums; general opinion of the intranet forums was quite low. We used and [...]]]></description>
			<content:encoded><![CDATA[<p>During the Spring Semester of 2007, I worked as the lead developer on the <a title="UAT Rogue Forums" href="http://op-server.0x58.com/forums/" target="_blank">UAT Rogue Forums</a>.  The project was designed and lead by Jordan Womack, a UAT Master&#8217;s Student.  Under him, the Rogue Forums was created as an alternative to <a title="University of Advancing Technology" href="http://www.uat.edu" target="_blank">UAT&#8217;s</a> intranet forums; general opinion of the intranet forums was quite low.</p>
<p>We used and modified <a title="phpBB Main Site" href="http://www.phpbb.com" target="_blank">phpBB.</a>  My portion of the project was to re-skin the bulletin board system, act as lead moderator, prevent non-UAT students from registering, and implement the post voting system.  phpBB version 3, RC5 was used and updated to subsequent release candidates.</p>
<p>Two php files were added to the core of phpBB3, one core file was modified, and additional tables were created in the database.  Currently this project only works with phpBB3 on a MySQL database, but I hope to make it database-independent.  Below is a snippet of code from the new files.</p>
<pre>&lt;?php
	$sql = 'INSERT INTO ' . VOTES_TABLE . ' ' . $db-&gt;sql_build_array('INSERT', array(
		'post_id'		=&gt; (int) $post_id,
		'user_id'		=&gt; $user-&gt;data['user_id'],
		'adjust'		=&gt; 1,
		'vote_time'		=&gt; time(),
		'voter_ip'		=&gt; $user-&gt;ip)
	);
?&gt;</pre>
<p>A working example of these modifications in a live phpBB3 environment can be seen at <a title="UAT's Rogue Forums" href="http://op-server.0x58.com/forums/" target="_blank">The Rogue Forums</a>.  Please contact me for a login, as account creation is limited to UAT students only.  The new files associated with this project are linked below.</p>
<p><a title="Zipfile of phpBB3 voting system files" href="http://www.securityandcaffeine.com/projects/phpBB3.zip">pbpBB voting system files</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F05%2F23%2Fut-rogue-forums%2F&amp;linkname=U%40T%20Rogue%20Forums"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/05/23/ut-rogue-forums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Rotate v2</title>
		<link>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/</link>
		<comments>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/#comments</comments>
		<pubDate>Thu, 08 May 2008 22:59:10 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=7</guid>
		<description><![CDATA[Background: This projects began as a request from a friend.  His site, acting as a working demo, is at Fried Pope.  He wanted the ability to have different stylesheets govern the look of his site, and be chosen randomly for each visitor.  In short, he wanted to rotate his style sheets. Project: The result from that request, plus [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>Background: This projects began as a request from a friend.  His site, acting as a working demo, is at <a title="Fried Pope" href="http://www.friedpope.com">Fried Pope</a>.  He wanted the ability to have different stylesheets govern the look of his site, and be chosen randomly for each visitor.  In short, he wanted to rotate his style sheets.</p>
<p>Project: The result from that request, plus a major recode, is linked below.  The system is designed to pick a stylesheet from the directory you specify, and redirect the user to it.  It will remember the stylesheet selected for the user for their entire visit, so your site doesn&#8217;t keep changing its look.  That would be confusing.</p>
<p>Compatibility: This project works in all known browsers.</p>
<p>Use: Using this tool is extremely simple.  You must have a web host that supports PHP.</p>
<ol>
<li>Uncompress the source code, and put &#8216;cssrotate2.php&#8217; in your website&#8217;s directory</li>
<li>Open &#8216;cssrotate2.php&#8217; with an HTML editor</li>
<li>If you need to, change the the line under the comments to specify where your styles are ($stylesdir = &#8220;./styles&#8221;;)</li>
<li>Save and close it</li>
<li>Open any pages in your site that you want to have use CSS Rotate</li>
<li>Add a link to it like you would any other stylesheet (&lt;link href=&#8221;./styles/cssrotate2.php&#8221; rel=&#8221;stylesheet&#8221; type=&#8221;text/css&#8221; /&gt;)</li>
<li>Save and close those pages</li>
</ol>
<p><a title="CSS Rotate 2 Download" href="http://www.securityandcaffeine.com/projects/cssrotate2.zip">download css rotate 2</a></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F05%2F08%2Fcss-rotate-v2%2F&amp;linkname=CSS%20Rotate%20v2"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/05/08/css-rotate-v2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and mysql_fetch_array()</title>
		<link>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/</link>
		<comments>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 03:54:05 +0000</pubDate>
		<dc:creator>Mack Staples</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://securityandcaffeine.com/?p=5</guid>
		<description><![CDATA[Today, I spent a couple hours working on a customer management system for APS Web Design.  Over the course of that work, I discovered a very interesting behavior in a particular function, specifically mysql_fetch_array(). For those not familiar with it, but familiar with PHP, this function takes the results returned by mysql_query(), and turns it into [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I spent a couple hours working on a customer management system for <a title="APS Web Design" href="http://www.apswebdesign.com" target="_blank">APS Web Design</a>.  Over the course of that work, I discovered a very interesting behavior in a particular function, specifically <strong>mysql_fetch_array()</strong>.</p>
<p>For those not familiar with it, but familiar with PHP, this function takes the results returned by <strong>mysql_query()</strong>, and turns it into an array of values.  There are two other functions that do similar things: <strong>mysql_fetch_row()</strong> and <strong>mysql_fetch_assoc()</strong>.</p>
<p><strong>mysql_fetch_row()</strong> &#8221;fetches one row of data from the result associated with the specified result identifier.  The row is returned as an array.  Each result column is stored in an array offset, starting at offset 0&#8243;. <a title="PHP.net Reference for mysql_fetch_row" href="http://us.php.net/mysql_fetch_row" target="_blank"><em>source</em></a></p>
<p>So, first value is <strong>$result[0]</strong>, second is <strong>$result[1]</strong>, and so on.</p>
<p><strong>mysql_fetch_assoc()</strong> &#8220;returns an asociative array of strings that corresponds to the fetched row&#8221;. <a title="PHP.net Reference for mysql_fetch_assoc" href="http://us3.php.net/mysql_fetch_assoc" target="_blank"><em>source</em>  </a>That is, each value that is returned is part of a pair: a &#8220;key&#8221; (index) value, and the actual value.  The &#8220;key&#8221; names correspond to the name of the field in the database.</p>
<p>So, first value is <strong>$result['name']</strong>, second is <strong>$result['email']</strong>, and so on.</p>
<p>When I&#8217;m coding, I like to have access to both the &#8220;key&#8221; values, and the numeric index.  Now, in all honesty, I almost always use &#8220;key&#8221; values, over numeric, but I still like having the option.  This is where <strong>mysql_fetch_array()</strong> comes in.</p>
<p><strong>mysql_fetch_array()</strong> will give you &#8220;an array with both associative and number indices.&#8221; <a title="PHP.net Reference for mysql_fetch_array" href="http://us3.php.net/manual/en/function.mysql-fetch-array.php" target="_blank"><em>source</em></a></p>
<p>Many other languages can do both associative (&#8220;key&#8221; index) and numeric indices by default.  Others are constrained to just numeric.  Here, PHP is giving us both.  </p>
<p>The question: <em>how?</em></p>
<p>The answer: <em>it cheats!</em></p>
<p>Seriously.  It cheats.  What do I mean?  I mean that the numeric indices are not truly referencing the position of the value.  One would expect that &#8220;<strong>$result[0]</strong>&#8221; would reference the value in the first position.  It does not.  This can easily be verified with a handy <strong>print_r()</strong>, or the following loop:</p>
<blockquote><p><strong>$result = mysql_fetch_array($result);<br />
foreach ($result as $key =&gt; $value)<br />
echo $key . &#8216;=&#8217; . $value . &#8216;&lt;br /&gt;&#8217;;</strong></p></blockquote>
<p>The result of that loop, on a data set I was working with, returned the following:</p>
<blockquote><p>0=mack<br />
username=mack<br />
1=1<br />
job_num=1<br />
2=Mack Staples<br />
name=Mack Staples<br />
3=5415551234<br />
phone_num= 5415551234<br />
4=test@example.com<br />
email=test@example.com</p></blockquote>
<p>What do you notice?  Every value is in there twice, and while the &#8217;0&#8242; value is indeed in the first position, the rest of the numbers don&#8217;t correlate.  So what is it doing?</p>
<p><strong>mysql_fetch_array()</strong> is building a larger array (twice the size of either of the other functions) and using the numbers not as true indexes, but as associative &#8220;key&#8221; values.  This means a couple of things.</p>
<p>1 &#8211; You are wasting speed if you reference the values, but don&#8217;t use quotes.  According to <a title="Reinhold Weber's Blog" href="http://reinholdweber.com/?p=3" target="_blank"><em>Reinhold Weber&#8217;s Blog</em></a>:  #17: &#8220;<span>$row[’id’] is 7 times faster than $row[id]&#8220;.  If you&#8217;re doing this a lot, and often&#8230; ouch.</span></p>
<p>2 &#8211; You&#8217;re wasting speed just using it.  From having to initially populate twice the data into an array, to a slowdown copying the array to other memory locations, this can cost you performance.</p>
<p>So, what can we do about it?</p>
<p>1 &#8211; Use quotes!  1/7th of the time to get <strong>$result['2']</strong> compared to <strong>$result[2]</strong>.  Simple speed fix.</p>
<p>2 &#8211; Using <strong>mysql_fetch_array()</strong> is up to you.  For what I usually use database results for, the difference is negligible.  I do small, infrequent queries, and I like having both numeric and associated indices.  To me, it&#8217;s worth the <em>minor</em> performance hit.</p>
<p>Code Safe. Code Smart.  <em>Be paranoid.</em></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.securityandcaffeine.com%2F2008%2F04%2F03%2Fphp-mysql-and-mysql_fetch_array%2F&amp;linkname=PHP%2C%20MySQL%2C%20and%20mysql_fetch_array%28%29"><img src="http://www.securityandcaffeine.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.securityandcaffeine.com/2008/04/03/php-mysql-and-mysql_fetch_array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->