<?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>Marginally Clever &#187; PHP</title>
	<atom:link href="http://www.marginallyclever.com/category/programming/php-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marginallyclever.com</link>
	<description>DIY Robotics, automation, tools, manufacturing, and everything related</description>
	<lastBuildDate>Sun, 05 Feb 2012 22:32:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Header Redirection vs echo/print</title>
		<link>http://www.marginallyclever.com/2009/10/header-location-vs-important-messages/</link>
		<comments>http://www.marginallyclever.com/2009/10/header-location-vs-important-messages/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 21:59:21 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=145</guid>
		<description><![CDATA[Consider the following code: Ok, let&#8217;s try to buffer our output to solve the problem. These greatly simplified examples shows a common problem that happens with posting, forms, and header redirection: What do you do if you need (or accidentally) print some text before the redirect? The answer is: Sessions. Now imagine what happens when [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php  // file1.php
if(isset($_POST['submit'])) {
  echo &quot;You can see this important message but the header() won't work.&quot;;
  header('Location: somewhere_else.php');
}
?&gt;
&lt;form action='' method='post'&gt;&lt;input type='submit'&gt;&lt;/form&gt;
</pre>
<p>Ok, let&#8217;s try to buffer our output to solve the problem.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php  // file2.php
ob_start();
if(isset($_POST['submit'])) {
  echo &quot;header() will work but you won't see this important message.&quot;;
  header('Location: somewhere_else.php');
}
?&gt;
&lt;form action='' method='post'&gt;&lt;input type='submit'&gt;&lt;/form&gt;
&lt;?php
ob_get_flush();
?&gt;
</pre>
<p>These greatly simplified examples shows a common problem that happens with posting, forms, and header redirection: <strong>What do you do if you need (or accidentally) print some text before the redirect?</strong> The answer is: <em>Sessions</em>.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php  // session_messages.php
start_session();

// add_message() - queues a message for display later
// msg - the contents of the message
// type - the type of message
function add_message($msg,$type='notice') {
  if(!isset($_SESSION['messages'])) {
    $_SESSION['messages']=array();
  }
  $_SESSION['messages'][]=array('type'=&gt;$type,'msg'=&gt;$msg);
}

function add_notice($msg) {
  add_message($msg,'notice');
}

function add_error($msg) {
  add_message($msg,'error');
}

function add_warning($msg) {
  add_message($msg,'warning');
}

function debug_item($a) {
  add_message('&lt;pre&gt;'.print_r($a).'&lt;/pre&gt;','debug');
}

// display_messages() - show the queued messages
function display_messages() {
  if(count($_SESSION['messages'])) {
    echo &quot;&lt;div class='messages'&gt;&quot;;
    foreach($_SESSION['messages'] as $v) {
      echo &quot;&lt;div class='&quot;.$v['type'].&quot;&gt;&quot;.$v['msg'].&quot;&lt;/div&gt;\n&quot;;
    }
    $_SESSION['messages']=array();
    echo &quot;&lt;/div&gt;&quot;;
  }
}
?&gt;
</pre>
<p>Now imagine what happens when all your error messages pass through add_error.  The file1.php example would become</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php  // file3.php
require_once &quot;session_messages.php&quot;;

if(isset($_POST['submit'])) {
  add_notice(&quot;You can see this message no matter what happens*&quot;);
  add_warning(&quot;* as long as somewhere_else.php calls display_messages().&quot;);
  header('Location: somewhere_else.php');
}
// you can also display them on the same page if you don't redirect.
display_messages();
?&gt;
&lt;form action='' method='post'&gt;&lt;input type='submit'&gt;&lt;/form&gt;
</pre>
<p>From here the sky is the limit &#8211; you could sort messages by type, you could hide errors so that only the administrators can see them.  Debugging becomes easier, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/10/header-location-vs-important-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forms, simplified, v3</title>
		<link>http://www.marginallyclever.com/2009/10/forms-simplified-v3/</link>
		<comments>http://www.marginallyclever.com/2009/10/forms-simplified-v3/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 21:32:59 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=141</guid>
		<description><![CDATA[+ Added form_date(), using Kevin Luck&#8217;s jQuery DatePicker. ! Fixed form_static() error message. ! Fixed form_select() to use set selected value. ~ Changed includes to requires to force errors. ! Fixed form_select() javascript died when required select had no options. You can find it all here.]]></description>
			<content:encoded><![CDATA[<ul>
<li>+ Added form_date(), using <a href="http://www.kelvinluck.com/assets/jquery/datePicker/">Kevin Luck&#8217;s jQuery DatePicker</a>.</li>
<li>! Fixed form_static() error message.</li>
<li>! Fixed form_select() to use set selected value.</li>
<li>~ Changed includes to requires to force errors.</li>
<li>! Fixed form_select() javascript died when required select had no options.</li>
</ul>
<p>You can find it all <a href="http://www.marginallyclever.com/forms/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/10/forms-simplified-v3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forms, simplified, v2</title>
		<link>http://www.marginallyclever.com/2009/09/forms-simplified-v2/</link>
		<comments>http://www.marginallyclever.com/2009/09/forms-simplified-v2/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:30:29 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=138</guid>
		<description><![CDATA[+ Added &#8216;changed&#8217; class to input_blocks + Added &#8216;confirm_unload&#8217; class to make per-form abandonment warnings + Added __form_cancel_warning to forms.js + Added __form_abandon_change_warning to forms.js + Added a diskette icon to changed elements for better feedback response. ~ Clarified some comments You can find it all here.]]></description>
			<content:encoded><![CDATA[<ul>
<li>+ Added &#8216;changed&#8217; class to input_blocks</li>
<li> + Added &#8216;confirm_unload&#8217; class to make per-form abandonment warnings</li>
<li> + Added __form_cancel_warning to forms.js</li>
<li> + Added __form_abandon_change_warning to forms.js</li>
<li> + Added a diskette icon to changed elements for better feedback response.</li>
<li> ~ Clarified some comments</li>
</ul>
<p>You can find it all <a href="http://www.marginallyclever.com/forms/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/09/forms-simplified-v2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forms, simplified</title>
		<link>http://www.marginallyclever.com/2009/09/forms-simplified/</link>
		<comments>http://www.marginallyclever.com/2009/09/forms-simplified/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 03:57:02 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[creative commons]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/2009/09/forms-simplified/</guid>
		<description><![CDATA[http://www.marginallyclever.com/forms/ A short while ago I posted about my PHP form system. People wrote in with many questions, leading me to the conclusion that the old system with it&#8217;s klunky javascript, lack of commenting, and general disarray &#8230;was not very friendly. So I&#8217;ve written a whole new library with examples, templates, better CSS, better email [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.marginallyclever.com/forms/">http://www.marginallyclever.com/forms/</a></p>
<p>A short while ago I posted about my PHP form system.  People wrote in with many questions, leading me to the conclusion that the old system with it&#8217;s klunky javascript, lack of commenting, and general disarray &#8230;was not very friendly.  So I&#8217;ve written a whole new library with examples, templates, better CSS, better email validation, and better url validation, not to mention code you can really use right off the shelf with a Creative Commons license.  Try version 1.0 today and let me know what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/09/forms-simplified/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easier PHP forms, part 3</title>
		<link>http://www.marginallyclever.com/2009/07/easier-php-forms-part-3/</link>
		<comments>http://www.marginallyclever.com/2009/07/easier-php-forms-part-3/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 23:30:20 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=76</guid>
		<description><![CDATA[In Part 1 I showed you how to simplify building basic forms in PHP. In Part 2 I extended this to show you almost every input type you&#8217;d ever want. (For the full set, hire me!) Now in Part 3, let&#8217;s add some friendly javascript to make sure your users input valid data. I shouldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/2009/06/easier-php-forms-part-1/">Part 1</a> I showed you how to simplify building basic forms in PHP.</p>
<p>In <a href="/2009/06/easier-php-forms-part-2/">Part 2</a> I extended this to show you almost every input type you&#8217;d ever want.  (For the full set, hire me!)</p>
<p>Now in Part 3, let&#8217;s add some friendly javascript to make sure your users input valid data.</p>
<p style="padding-left: 30px;">I shouldn&#8217;t need to point out that Javascript isn&#8217;t enough.  If, for any reason, javascript doesn&#8217;t run then all your tests will be ignored and bad data can be input.  Injection attacks can also circumvent javascript with ease.  There is no way to avoid PHP testing server-side, but javascript is friendly to the average user and saves your server bandwidth.</p>
<p>The first thing we&#8217;ll need is to identify where we&#8217;ll need to add tests.</p>
<ul>
<li>make sure this field isn&#8217;t empty</li>
<li>urls in the right format</li>
<li>emails in the right format</li>
<li>select must be/must not be a certain value</li>
<li>custom tests</li>
</ul>
<p>In every case we&#8217;ll need some javascript in the &lt;head&gt; section of the page. Something like&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> test_form_X<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #006600; font-style: italic;">// where X is the name of a form, in case there are multiple forms on the same page</span>
  ok<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  msg<span style="color: #339933;">=</span><span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
  first<span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#X .form_item'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// jQuery/CSS magic!</span>
&nbsp;
  ...
  <span style="color: #006600; font-style: italic;">// let's assume we have a form element called form_element_N.</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>is_form_element_N_ok<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ok<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    msg<span style="color: #339933;">+=</span><span style="color: #3366CC;">'Form element N is no good!<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>first<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      form_element_N.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      first<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// every input/select/textarea is inside an input_item inside a form_item.</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#form_element_N'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span>.<span style="color: #660066;">parent</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// jQuery/CSS magic!</span>
  <span style="color: #009900;">&#125;</span>
  ...
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ok<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> ok<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The trick is twofold: knowing which is_form_element_N_ok() to put in.  First, I take that whole inner part of the test and put it in a function by itself.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add_form_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;      if(first) {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;        first=0;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;        <span style="color: #000099; font-weight: bold;">\$</span>('#<span style="color: #006699; font-weight: bold;">$name</span>').focus();<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;      }<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;      <span style="color: #000099; font-weight: bold;">\$</span>('#<span style="color: #006699; font-weight: bold;">$name</span>').parent.parent.addClass('error');<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;      ok=false;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;      msg+='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;\'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;';<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>so let&#8217;s create our first test.  We&#8217;ll have to update create_form_start().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Create a javascript error message for fields that can't be left blank.</span>
<span style="color: #000000; font-weight: bold;">function</span> add_form_test_required<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$test</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;    if(<span style="color: #000099; font-weight: bold;">\$</span>('<span style="color: #006699; font-weight: bold;">$name</span>').value==null || <span style="color: #000099; font-weight: bold;">\$</span>('<span style="color: #006699; font-weight: bold;">$name</span>').value.length==0) {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
       <span style="color: #339933;">.</span>add_form_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$label</span> is required.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;    }<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  add_form_test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> add_form_test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$forms_to_validate</span><span style="color: #339933;">,</span><span style="color: #000088;">$last_form_name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$forms_to_validate</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$last_form_name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.=</span><span style="color: #000088;">$test</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> create_form_start<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">'form1'</span><span style="color: #339933;">,</span><span style="color: #000088;">$classname</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'form'</span><span style="color: #339933;">,</span><span style="color: #000088;">$action</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$method</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span><span style="color: #000088;">$target</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$forms_to_validate</span><span style="color: #339933;">,</span><span style="color: #000088;">$last_form_name</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$forms_to_validate</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$last_form_name</span><span style="color: #339933;">=</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;
&amp;lt; form id=&quot;</span><span style="color: #000088;">$name</span><span style="color: #0000ff;">&quot; class=&quot;</span><span style="color: #000088;">$classname</span><span style="color: #0000ff;">&quot; action=&quot;</span><span style="color: #000088;">$action</span><span style="color: #0000ff;">&quot; enctype=&quot;</span>multipart<span style="color: #339933;">/</span>form<span style="color: #339933;">-</span>data<span style="color: #0000ff;">&quot; method=&quot;</span><span style="color: #000088;">$method</span><span style="color: #0000ff;">&quot;&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So where do these tests go?  Well, when we&#8217;re ready to echo the page, we call the following method</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> form_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$forms_to_validate</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  &lt;script type=&quot;</span><span style="color: #0000ff;">'text/javascript'</span><span style="color: #0000ff;">&quot;&gt;&lt;!--mce:0--&gt;&lt;/script&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So what does this mean?  It means we can have our forms built in a few lines of code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$body</span><span style="color: #339933;">=</span>create_form_start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$body</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Your Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$body</span><span style="color: #339933;">.=</span>create_form_password<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user_pass'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Password'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Your Password. &lt;a href=&quot;forgot.php&quot;&gt;Did you forget your password?&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$body</span><span style="color: #339933;">.=</span>create_form_required<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There is nothing stopping us from writing a test inside create_form_password that checks the password is a certain length, or from adding a &#8220;confirm password&#8221; field and checking they are both the same.  This same technique can be extended in any way you please.</p>
<p>As an interesting aside, HTML5 supports URLs and emails natively &#8211; the tests are built right into the web browser.  No need for javascript!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/07/easier-php-forms-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easier PHP forms, part 2</title>
		<link>http://www.marginallyclever.com/2009/06/easier-php-forms-part-2/</link>
		<comments>http://www.marginallyclever.com/2009/06/easier-php-forms-part-2/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 17:44:51 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[email validation]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[password match]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=57</guid>
		<description><![CDATA[In part 1 I showed you my method for simplifying form building and maintenance. Now I&#8217;d like to show you a few of the things that can be simplified by using this system. Included are: prices passwords emails textareas selects yes/no (booleans) files hiddens cancel buttons submit buttons &#8220;are you sure you want to do [...]]]></description>
			<content:encoded><![CDATA[<p>In part 1 I showed you my method for simplifying form building and maintenance.  Now I&#8217;d like to show you a few of the things that can be simplified by using this system.  Included are:</p>
<ul>
<li>prices</li>
<li>passwords</li>
<li>emails</li>
<li>textareas</li>
<li>selects</li>
<li>yes/no (booleans)</li>
<li>files</li>
<li>hiddens</li>
<li>cancel buttons</li>
<li>submit buttons</li>
<li>&#8220;are you sure you want to do that?&#8221; submit buttons</li>
<li>addresses</li>
</ul>
<p>I use this code for 95% of the forms I build and then modify the CSS to get the effects a customer wants.</p>
<p>In part 3 I&#8217;ll show you how to use jQuery to automatically validate user data.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_price<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>&amp;lt;input type='text' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_password<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;input type='password' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_password_confirm<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  add_form_test_password_confirm<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// we'll cover this in part 3</span>
&nbsp;
  <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;input type='password' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
  create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;input type='password' name='<span style="color: #006699; font-weight: bold;">{$name}</span>_confirm' value=''&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&amp;amp;nbsp;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_confirm&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">// @TODO: add email validation.</span>
 <span style="color: #000088;">$test</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;    regex_<span style="color: #006699; font-weight: bold;">{$name}</span>=/^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*(\.[a-zA-Z]{2,4})$/<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
 <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;    compare_to=f.<span style="color: #006699; font-weight: bold;">{$name}</span>.value.replace(/^\s+|\s+$/g,'');<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
 <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;    if(compare_to!='' &amp;amp;&amp;amp; !compare_to.match(regex_<span style="color: #006699; font-weight: bold;">{$name}</span>)) {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
 <span style="color: #339933;">.</span>add_form_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$label</span> does not appear to be a valid email.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;    }<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 add_form_test<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;input type='text' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_textarea<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;textarea name='<span style="color: #006699; font-weight: bold;">$name</span>' rows='10' cols='80'&amp;gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;lt;/textarea&amp;gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_select_internal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$options</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$multiple</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;select id='<span style="color: #006699; font-weight: bold;">$name</span>' name='<span style="color: #006699; font-weight: bold;">$name</span>' <span style="color: #006699; font-weight: bold;">$extra</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$multiple</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot; multiple='yes' size='<span style="color: #006699; font-weight: bold;">$multiple</span>'&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$k</span><span style="color: #339933;">=</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$v</span><span style="color: #339933;">=</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;  &amp;lt;option value='<span style="color: #006699; font-weight: bold;">$k</span>'&amp;gt;<span style="color: #006699; font-weight: bold;">$v</span>&amp;lt;/option&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;&amp;lt;/select&amp;gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$v</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>?<span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value='<span style="color: #006699; font-weight: bold;">$v</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;value='<span style="color: #006699; font-weight: bold;">$v</span>' selected&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$sel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_select<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$options</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$multiple</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">=</span>create_form_select_internal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$options</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$multiple</span><span style="color: #339933;">,</span><span style="color: #000088;">$extra</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sel</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_bool<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;select name='<span style="color: #006699; font-weight: bold;">$name</span>' <span style="color: #006699; font-weight: bold;">$extra</span>&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;  &amp;lt;option value='yes'&amp;gt;&quot;</span><span style="color: #339933;">.</span>_t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yes&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;lt;/option&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;  &amp;lt;option value='no'&amp;gt;&quot;</span><span style="color: #339933;">.</span>_t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;lt;/option&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;&amp;lt;/select&amp;gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$v</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>?<span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$sel</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value='<span style="color: #006699; font-weight: bold;">$v</span>'&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;value='<span style="color: #006699; font-weight: bold;">$v</span>' selected&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$sel</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sel</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$inner</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;input name='<span style="color: #006699; font-weight: bold;">$name</span>' type='file'&amp;gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$inner</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_address<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #000088;">$hint</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address1'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address2'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'region'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'region'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalcode'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalcode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_address1&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address1'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Street&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_address2&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_city&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;City&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_region&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'region'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;State/Province&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_country&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'country'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Country&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span>create_form_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;_postalcode&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalcode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Postal Code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">return</span> create_form_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #000088;">$label</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_hidden<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;  &amp;lt;input type='hidden' id='<span style="color: #006699; font-weight: bold;">$name</span>' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_submit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$extra</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$last_form_name</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// we'll cover this is in part 3</span>
&nbsp;
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;  &amp;lt;input type='submit' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' onclick='javascript:return validate_<span style="color: #006699; font-weight: bold;">$last_form_name</span>(this.form);' <span style="color: #006699; font-weight: bold;">$extra</span>&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_cancel<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$redirect</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;  &amp;lt;input type='button' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' onclick='javascript:window.location=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$redirect</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>;'&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #000000; font-weight: bold;">function</span> create_form_submit_confirm<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #000088;">$confirm</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Are you sure?&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$validate</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$last_form_name</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// we'll cover this is in part 3</span>
&nbsp;
 <span style="color: #000088;">$str</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;  &amp;lt;input type='submit' name='<span style="color: #006699; font-weight: bold;">$name</span>' value='&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' onclick='javascript:return (confirm(<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;\'&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$confirm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$validate</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot; &amp;amp; validate_<span style="color: #006699; font-weight: bold;">$last_form_name</span>(this.form)&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;);'&amp;gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/06/easier-php-forms-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easier PHP forms, part 1</title>
		<link>http://www.marginallyclever.com/2009/06/easier-php-forms-part-1/</link>
		<comments>http://www.marginallyclever.com/2009/06/easier-php-forms-part-1/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 18:52:05 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=50</guid>
		<description><![CDATA[Building and maintaining forms in PHP is one of the most time-consuming parts of the job.  Put another way, it's one of the biggest bottlenecks for fast prototyping and development.  Getting rid of bottlenecks speeds up work and increases overall joy.  So how do we do this?]]></description>
			<content:encoded><![CDATA[<p>Building and maintaining forms in PHP is one of the most time-consuming parts of the job.  Put another way, it&#8217;s one of the biggest bottlenecks for fast prototyping and development.  Getting rid of bottlenecks speeds up work and increases overall joy.  So how do we do this?</p>
<p>The first thing I did was look at all the forms I had been writing.  In general they all follow a pattern:</p>
<pre class="html">&lt;DOCTYPE ...&gt;
&lt;html ...&gt;
&lt;head&gt;
  ...
  // javascript tests to make sure user fills form correctly
&lt;/head&gt;
&lt;body&gt;
...
&lt;form name='blah' action='#' method='post'&gt;
...

  &lt;div class='form_item'&gt;
    &lt;label for='ABC'&gt;A human-readable name for the input&lt;/label&gt;
    &lt;div class='input_item'&gt;
      &lt;input name='ABC'&gt;  &lt;?/* or select/textarea/etc */?&gt;
      &lt;div class='help'&gt;Some explanation text here.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class='clear'&gt;&lt;/div&gt;
  &lt;/div&gt;
...
&lt;/form&gt;
...
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>It seemed that cutting and pasting this was rife with errors.  Even worse is trying to change the layout of the page &#8211; a major (non-CSS) alteration could really ruin my day!  I had to come up with somethin better.</p>
<pre class="php">function create_form_row($input,$label_name=null,$label_for=null,$hint=null) {
                         $str ="  &lt;div class='form_item'&gt;\n";
  if(isset($label_name)) $str.="    &lt;label for='$label_for'&gt;$label_name&lt;/label&gt;\n";
                         $str.= "    &lt;div class='input_item'&gt;\n";
                         $str.= "      $input\n";
  if(isset($hint))       $str.= "      &lt;div class='help'&gt;$hint&lt;/div&gt;\n";
                         $str.= "    &lt;/div&gt;\n";
                         $str.= "    &lt;div class='clear'&gt;&lt;/div&gt;\n";
                         $str.= "  &lt;/div&gt;\n";
  return $str;
}

function create_form_start($name,$classname='form',$action='',$method='post',$target='') {
  return "&lt;form enctype='multipart/form-data' id='$name' name='$name' class='$classname' action='$action' method='$method'&gt;\n";
}

function create_form_end() {
  echo '&lt;/form&gt;\n';
}</pre>
<p>At first this might seem like a lot of work for not much gain.  Think of this as the center of the onion.  If we build layers on top of it we start to see some big big benefits.</p>
<pre class="php">function create_form_check_inner($name,$value=null,$checked=false) {
  return "&lt;input type='checkbox' id='$name' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."'".($checked?" checked":"")."&gt;";
}

//------------------------------------------------------------------------------
function create_form_check($name,$value=null,$label=null,$hint=null,$checked=false) {
  return create_form_row($label,create_form_check_inner($name,$value,$checked),$name,$hint);
}

//------------------------------------------------------------------------------
function create_form_text_inner($name,$value=null,$extra='') {
  return "&lt;input type='text' id='$name' name='$name' value='".htmlspecialchars($value,ENT_QUOTES)."' $extra&gt;";
}

//------------------------------------------------------------------------------
function create_form_text($name,$value=null,$label=null,$hint=null,$extra='') {
  return create_form_row(create_form_text_inner($name,$value,$extra),$label,$name,$hint);
}</pre>
<p>So now when I want to create a new form all I do is</p>
<pre class="php">echo create_form_start('form1');
echo create_form_text('user_name','','User Name','what is your name?');
echo create_form_check('love_this','yes','Love','Do you love this new form system?',true);
echo create_form_end();</pre>
<p>In Part II I&#8217;ll show you how to use create_form_ elements for passwords, selects, textareas, and more.</p>
<p>In Part III I&#8217;ll show you how you can use this system and jQuery to add checks that make sure users put in valid emails, required fields, and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/06/easier-php-forms-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP login handling tutorial &#8211; sessions &amp; cookies included</title>
		<link>http://www.marginallyclever.com/2009/06/php-login-handling-tutorial-sessions-cookies-included/</link>
		<comments>http://www.marginallyclever.com/2009/06/php-login-handling-tutorial-sessions-cookies-included/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 18:06:03 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[secure]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=41</guid>
		<description><![CDATA[I see a lot of people trying to write code to authenticate users logging into a PHP website.  This is some code I cobbled together in december of 2008 and it has worked problem free since then.]]></description>
			<content:encoded><![CDATA[<p>I see a lot of people trying to write code to authenticate users logging into a PHP website.  This is some code I cobbled together in december of 2008 and it has worked problem free since then.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// I've already sanitized all GET, POST, and COOKIE data at this point.</span>
<span style="color: #000000; font-weight: bold;">function</span> check_login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$DB</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$login_justnow</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// if the user isn't logged in and they're POSTing a login request, process it</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user/id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'login_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #000088;">$remember_me</span><span style="color: #339933;">=</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'remember_me'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>?<span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pass</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'login_pass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$user_id</span><span style="color: #339933;">=</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>QueryXY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT id FROM `users` WHERE name='<span style="color: #006699; font-weight: bold;">$name</span>' AND pass='<span style="color: #006699; font-weight: bold;">$pass</span>' AND confirmed='1' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      account_login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$remember_me</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$login_justnow</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$name</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/given_name&quot;</span><span style="color: #009900;">&#41;</span>?<span style="color: #0000ff;">', '</span><span style="color: #339933;">.</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/given_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
      add_notice<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome<span style="color: #006699; font-weight: bold;">$name</span>!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      add_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Login failed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      account_logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// if the user isn't logged in but has a COOKIE, process it</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;remember_me&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=@</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;remember_me&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$cookie</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>QueryArray<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM `users` WHERE id='<span style="color: #006699; font-weight: bold;">$user_id</span>' AND cookie='<span style="color: #006699; font-weight: bold;">$cookie</span>' AND confirmed='1' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        account_login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$login_justnow</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$name</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/given_name&quot;</span><span style="color: #009900;">&#41;</span>?<span style="color: #0000ff;">', '</span><span style="color: #339933;">.</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/given_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        add_notice<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome<span style="color: #006699; font-weight: bold;">$name</span>!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// if the user's session says they're logged in, process it</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$login_justnow</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$user_id</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cookie</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/cookie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ip</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/ip&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$session</span><span style="color: #339933;">=</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM `users` WHERE id='<span style="color: #006699; font-weight: bold;">$user_id</span>' AND ip='<span style="color: #006699; font-weight: bold;">$ip</span>' AND session='<span style="color: #006699; font-weight: bold;">$session</span>' AND cookie='<span style="color: #006699; font-weight: bold;">$cookie</span>' AND confirmed='1' LIMIT 1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>DoQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>NumRows<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      account_login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      add_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Session security failed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      account_logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>EndQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// check if the user actually has rights to this part of the site - your implementation may vary</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> account_login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$remember_me</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$DB</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>user_is_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// update cookie</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$remember_me</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$cookie_code</span><span style="color: #339933;">=</span>generate_random_string<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cookie_str</span><span style="color: #339933;">=</span><span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'remember_me'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cookie_str</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    add_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/cookie&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    remove_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/cookie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// update session security</span>
  <span style="color: #000088;">$ip</span><span style="color: #339933;">=</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$session</span><span style="color: #339933;">=</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$cookie_code</span><span style="color: #339933;">=</span>get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/cookie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$cookie</span><span style="color: #339933;">=</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie_code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>DoQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE `users` SET last_on=NOW(), session='<span style="color: #006699; font-weight: bold;">$session</span>'&quot;</span>
    <span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cookie_code</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>?<span style="color: #0000ff;">&quot;, cookie='&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$cookie</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;, ip='<span style="color: #006699; font-weight: bold;">$ip</span>' WHERE id='<span style="color: #006699; font-weight: bold;">$user_id</span>' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// update session info</span>
  <span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>DoQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM `users` WHERE id='<span style="color: #006699; font-weight: bold;">$user_id</span>' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$row</span><span style="color: #339933;">=</span><span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>FetchAssoc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    add_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$DB</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>EndQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// check if any other part of your system needs to know about a user logging in.</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> user_is_logged_in<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">return</span> get_session<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user/id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> generate_random_string<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$random</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #990000;">srand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>double<span style="color: #009900;">&#41;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$char_list</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$char_list</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$char_list</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;1234567890&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #000088;">$length</span><span style="color: #339933;">;++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000088;">$random</span><span style="color: #339933;">.=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$char_list</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$char_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$random</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/06/php-login-handling-tutorial-sessions-cookies-included/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Updating PHP cookies without breaking the experience</title>
		<link>http://www.marginallyclever.com/2009/06/updating-cookies-without-breaking-the-experience/</link>
		<comments>http://www.marginallyclever.com/2009/06/updating-cookies-without-breaking-the-experience/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:42:51 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.marginallyclever.com/?p=22</guid>
		<description><![CDATA[PHP cookies are a great idea for storing data temporarily.  Sure, they have their security problems but (arguably) they do more good than bad.  The trickiest part about cookies is when you update your PHP and it forces you to change the contents of your cookies.  You don't want your faithful site visitors to see an error message!]]></description>
			<content:encoded><![CDATA[<p>PHP cookies are a great idea for storing data temporarily.  Sure, they have their security problems but (arguably) they do more good than bad.  The trickiest part about cookies is when you update your PHP and it forces you to change the contents of your cookies.  You don&#8217;t want your faithful site visitors to see an error message!  Consider the following naive implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It doesn&#8217;t take a lot of imagination to see how this could break.  Let&#8217;s start by making it easier to store lots of info in a single cookie.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'something else'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">=</span><span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;userdata&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'something else'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userdata'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userdata'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, but what about if we need to add more data to our cookies later?  Everyone with an old cookie will be missing parts of $data and we&#8217;ll have to catch that.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'version'</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'1.0'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'something else'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$str</span><span style="color: #339933;">=</span><span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;userdata&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'something else'</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$old</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userdata'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userdata'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$old</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this version is so old &amp;amp; busted we can't read it at all.</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$old</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Use the $data['version'] to help us figure out what pieces of $data need to be filled in.</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And&#8230; voila!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marginallyclever.com/2009/06/updating-cookies-without-breaking-the-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

