<?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>Richard Willars &#187; Articles</title>
	<atom:link href="http://www.richardwillars.com/category/articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardwillars.com</link>
	<description>Web Designer / Web Developer / Application Builder</description>
	<lastBuildDate>Wed, 28 Oct 2009 16:20:39 +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>Google chart sparkline &#8211; encoding in PHP</title>
		<link>http://www.richardwillars.com/articles/php/google-chart-sparkline-encoding-in-php/</link>
		<comments>http://www.richardwillars.com/articles/php/google-chart-sparkline-encoding-in-php/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 11:53:01 +0000</pubDate>
		<dc:creator>Richard Willars</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[google chart]]></category>

		<guid isPermaLink="false">http://www.richardwillars.com/?p=58</guid>
		<description><![CDATA[The Google Chart API is quite powerful and easy to use, with many customisations available. For my projects i&#8217;ve been using the &#8216;simple encoding&#8216; method for passing chart data to the Google Chart API. After doing a search on the Internet for a PHP script that converts the data into the encoded string and failing [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://code.google.com/apis/chart/" target="_blank">Google Chart API</a> is quite powerful and easy to use, with many customisations available. For my projects i&#8217;ve been using the &#8216;<a href="http://code.google.com/apis/chart/formats.html#simple" target="_blank">simple encoding</a>&#8216; method for passing chart data to the Google Chart API. After doing a search on the Internet for a PHP script that converts the data into the encoded string and failing miserably I decided to write my own. This simple PHP function to convert a numerical array of data into the encoded string.<span id="more-58"></span></p>
<pre>&lt;?php
function simpleEncode($valueArray,$maxValue) {
	$simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	$str = 's:';
	$t = count($valueArray);
	for ($i=0;$i&lt;$t;$i++) {
		$currentValue = $valueArray[$i];
		if ((strlen($currentValue)!=0) &amp;&amp; ($currentValue&gt;=0)) {
    		$str .= substr($simpleEncoding,round((strlen($simpleEncoding)-1) * $currentValue / $maxValue),1);
    	}
      	else {
      		$str .= '_';
      	}
  	}
	return $str;
}
?&gt;</pre>
<p>You can then create the graph in a similar fashion to this:</p>
<pre>&lt;?php
$data = array(1,3,5,3,2,9,1,10,4);
?&gt;
&lt;img alt="sparkline" src="http://chart.apis.google.com/chart?cht=lc&amp;chs=100x30&amp;chls=1,1,0&amp;chxt=r,x,y
&amp;chxs=0,990000,11,0,_|1,990000,1,0,_|2,990000,1,0,_&amp;chxl=0:||1:||2:||&amp;chd=&lt;?php echo simpleEncode($data,max($data)); ?&gt;"/&gt;</pre>
<p>Which would output something similar to this:</p>
<p><img src="http://chart.apis.google.com/chart?cht=lc&amp;chs=100x30&amp;chls=1,1,0&amp;chxt=r,x,y&amp;chxs=0,990000,11,0,_|1,990000,1,0,_|2,990000,1,0,_&amp;chxl=0:||1:||2:||&amp;chd=s:KKKKKUUppzz9" alt="example sparkline" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardwillars.com/articles/php/google-chart-sparkline-encoding-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Form Processor</title>
		<link>http://www.richardwillars.com/articles/php/php-form-processor/</link>
		<comments>http://www.richardwillars.com/articles/php/php-form-processor/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 12:33:36 +0000</pubDate>
		<dc:creator>Richard Willars</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[processor]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://www.richardwillars.com/?p=40</guid>
		<description><![CDATA[A powerful open source PHP form processor that allows for rapid development and maintainence, as well as protecting your website forms from various hacking attacks.]]></description>
			<content:encoded><![CDATA[<p>Ever since I started building websites I&#8217;ve found making forms to be time consuming, tedious and generally boring, and I&#8217;m pretty sure most web developers are in a similar situation.</p>
<p>First of all you have your HTML form, which then submits all the data to PHP to be validated etc. You have to write the validation rules for each form field in PHP, and then come up with a system of reporting errors back to the user. With large forms this can get extremely complicated!</p>
<p>Around two years ago I came across a <a href="http://simonwillison.net/2003/Jun/17/theHolyGrail/">form validator by Simon Willison</a>. This basically let you write the entire form and it&#8217;s validation rules in the html. These rules are then read by PHP, applied to the relevant form fields and so on. It&#8217;s a very elegant solution that makes building forms much easier and faster, as well as easier to debug and maintain. I&#8217;ve been using this script for the last 2 years and have made various changes to the script along the way to suit my purposes.</p>
<p>However, the script does have it&#8217;s down-points. It uses the PHP 4 XML library which to be honest is rubbish! It causes the code to become extremely bloated and  unable to handle certain characters such as ampersands (&amp;). This means that as soon as a user enters an ampersand or a character like a dollar, the script would crash!</p>
<p>The script also only supported textfields, and didn&#8217;t provide support for radio buttons, checkboxes and select drop-down lists.</p>
<p>With all these positives and negatives in mind I&#8217;ve built my own PHP Form Processor from scratch, which I think does the job quite nicely. The script is open source and I&#8217;ve provided a tutorial and several example forms on how to use it.</p>
<p>The script also protects your forms against CSRF (Cross-site request forgery) and form manipulation. Anyway, without futher ado, here is the tutorial..<span id="more-40"></span></p>
<p><strong>Requirements</strong></p>
<ul>
<li>PHP 5 with SimpleXML support</li>
<li>Session support</li>
<li>Cheese on toast</li>
</ul>
<p><strong>Demonstration</strong></p>
<p>Traditionally an XHTML form input box would look like this:</p>
<pre>&lt;input type="text" id="myBox" name="myBox" /&gt;</pre>
<p>Using the PHP form processor this would be written as:</p>
<pre>&lt;element id="myBox"&gt;
 &lt;input type="text" /&gt;
 &lt;error&gt; &lt;strong class="error"&gt;!&lt;/strong&gt;&lt;/error&gt;
 &lt;compulsory message="myBox must be filled in" /&gt;
 &lt;validate test="alpha" message="myBox must only be letters" /&gt;
&lt;/element&gt;</pre>
<p>At first glance it looks a lot more bloated than the traditional method, so lets take a look at what it&#8217;s doing and why it&#8217;s better.</p>
<p>When using the form processor all the form items are wrapped in element tags. The element tag tells the form processor that it&#8217;s a form field, and also specifies the ID. Within the element tag we define the properties and rules of the form field. In the above example we&#8217;re telling the form processor that the form field we want is a text field (&lt;input type=&#8221;text&#8221; /&gt;), as well as defining various rules to apply to the user&#8217;s input.</p>
<p>The line  &#8217;&lt;error&gt; &lt;strong class=&#8221;error&#8221;&gt;!&lt;/strong&gt;&lt;/error&gt;&#8217; defines what is displayed when a validation rule fails. In this case a bold exclamation mark would be displayed just after the text field.</p>
<p>The next few lines are the rules which we want to apply to the text field. The compulsory line states that the field must be filled in, and if it&#8217;s not present the message &#8216;myBox must be filled in&#8217; to the user. The validate line states that the user input must only be letters, and if it&#8217;s not present the message &#8216;myBox must only be letters&#8217; to the user.</p>
<p>Many rules can be applied to the form fields. They will be applied to the form field in the order that you define them (top to bottom). For example, if there are 3 rules defined for a form field and the first rule fails then the next 2 rules won&#8217;t be applied. Instead the error message for rule 1 will be shown to the user. If the 1st rule runs fine it&#8217;ll move onto the 2nd rule etc.</p>
<p>Here is a description of each of the rules available:</p>
<p><strong>Compulsory</strong></p>
<p>Checks whether the user has entered any input or not.</p>
<pre>&lt;compulsory message="This field is compulsory" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Validate</strong></p>
<p>Checks whether the user input matches the format specified. The format can be alpha, alphanumeric or numeric.</p>
<pre>&lt;validate test="alpha" message="email must be alpha" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Regular Expression</strong></p>
<p>Checks whether the user input matches the regular expression specified.</p>
<pre>&lt;regexp test="|^[a-zA-Z]*$|" message="email doesn't match the regexp" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Text transform<br />
</strong></p>
<p>Alters how the user input is outputted.</p>
<pre>&lt;text transform="upper" /&gt; UPPERCASES THE ENTIRE INPUT
&lt;text transform="lower" /&gt; lowercases the entire input
&lt;text transform="ucwords" /&gt; Uppercases The First Letter Of Each Word</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Minimum length<br />
</strong></p>
<p>Checks whether the user input is of a required length.</p>
<pre>&lt;minLength length="4" message="This field must contain more than 3 characters" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Maximum length<br />
</strong></p>
<p>Checks whether the user input is greater than a required length.</p>
<pre>&lt;maxLength length="10" message="This field must contain less than 10 characters" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Match</strong></p>
<p>Checks whether the user input matches the user input in another field. Most common usage is for confirming passwords.</p>
<pre>&lt;match element="anotherFieldID" message="this field must match another field" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>No</td>
<td>No</td>
<td>No</td>
<td>No</td>
</tr>
</tbody>
</table>
<p><strong>Callback</strong></p>
<p>Takes the user input and passes it to a PHP function of your choice. The function should take one parameter as a string (the user input), and return Boolean true or false. If the function returns false the error message will be presented to the user.</p>
<p>An unlimited number of callbacks can be defined for each form field &#8211; just place them one after another. Each callback will be run in order.. if the first one is ok then it moves onto the next one. However, if the first one fails it won&#8217;t run the other callbacks or rules.</p>
<pre>&lt;callback function="functionname" message="callback 1 failed" /&gt;</pre>
<table border="0">
<tbody>
<tr>
<td>Textfield</td>
<td>Password</td>
<td>Textarea</td>
<td>Dropdown list</td>
<td>Checkbox</td>
<td>Radio button</td>
<td>Button</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
<td>Yes</td>
</tr>
</tbody>
</table>
<p><strong>Summary &amp; goodies!</strong></p>
<p><strong></strong>The entire form and it&#8217;s rules are read in by PHP, which then goes through the form and extracts the rules. It then cleans up the form and produces strict XHTML which it outputs to the user. This means they never see the rules and just see standard XHTML markup.</p>
<p>It&#8217;s a difficult concept and you might be a bit confused at this point, so it&#8217;s probably best just to give you a working example. Here is a simple form which tests some of the functionality available (complete with source-code).</p>
<p><strong><a href="http://www.richardwillars.com/demos/fp/examples/" target="_blank">View example forms</a><br />
<a href="http://www.richardwillars.com/demos/fp/formprocessor.zip" target="_blank">Download the form processor (with examples) (version 2.01 &#8211; 24/01/2009)</a></strong></p>
<p><strong><br />
</strong></p>
<p>As always I&#8217;d love to hear from you, so any thoughts, comments, bugs etc, please post! I&#8217;d also like to see examples of where people have used it, so feel free to drop me links!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardwillars.com/articles/php/php-form-processor/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Storing sessions in the database</title>
		<link>http://www.richardwillars.com/articles/php/storing-sessions-in-the-database/</link>
		<comments>http://www.richardwillars.com/articles/php/storing-sessions-in-the-database/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 17:27:27 +0000</pubDate>
		<dc:creator>Richard Willars</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.richardwillars.com/articles/php/storing-sessions-in-the-database/</guid>
		<description><![CDATA[Using Sessions in PHP can be extremely useful, and is almost a requirement when making dynamic web applications. However, sessions do have drawbacks, and one of these is security. When a session is created, it gets written to a file on the server. If the server you are using has other hosting accounts, they will [...]]]></description>
			<content:encoded><![CDATA[<p>Using Sessions in PHP can be extremely useful, and is almost a requirement when making dynamic web applications. However, sessions do have drawbacks, and one of these is security. When a session is created, it gets written to a file on the server. If the server you are using has other hosting accounts, they will also be using the same directory as your session files. If you&#8217;re storing any personal information about your website visitors, you have quite a serious problem.</p>
<p>For this reason, I would strongly suggest storing your sessions in a database. This tightens security considerably, and also allows for a wealth of new possibilities, such as running SQL queries on the database to see how many users are logged in. It is also the only logical solution if you are using multiple servers that need to access the same user sessions.</p>
<p><span id="more-26"></span>It&#8217;s quite simple to make the change to storing sessions in the database, and all you need for the following example is PHP 5 and MySQL.</p>
<p>First of all you will need to include the following PHP code in every page on your website that needs database or session access. It should be the very first thing included on every page.</p>
<p><a HREF="http://www.richardwillars.com/wp-content/uploads/2008/02/sessions_database.inc.txt" TITLE="sessions_database.inc.txt">sessions_database.inc.txt</a></p>
<p>Don&#8217;t forget to change your database details!</p>
<p>Secondly, you will need the following at the bottom of each page:</p>
<pre>
//You must call the following at the bottom of every page that uses sessions
session_write_close();</pre>
<p>The last thing you need to do is to create the database table to store the sessions. Here is the SQL to do so for a MySQL database:</p>
<pre>
CREATE TABLE `sessions` (
  `id` char(32) NOT NULL,
  `data` longtext NOT NULL,
  `last_accessed` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.richardwillars.com/articles/php/storing-sessions-in-the-database/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
