<?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>Yet another blog about PHP, HTML and CSS &#187; Programming Techniques</title>
	<atom:link href="http://blog.pepa.info/php-html-css/category/programming-techniques/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pepa.info</link>
	<description>Petr 'PePa' Pavel</description>
	<lastBuildDate>Thu, 19 Aug 2010 18:17:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MySQL comparison catch: &#8216;abc&#8217; = 00</title>
		<link>http://blog.pepa.info/php-html-css/programming-techniques/mysql-comparison-catch-abc-00/</link>
		<comments>http://blog.pepa.info/php-html-css/programming-techniques/mysql-comparison-catch-abc-00/#comments</comments>
		<pubDate>Mon, 25 May 2009 20:39:00 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming Techniques]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=113</guid>
		<description><![CDATA[I was shocked to find a weakness in my authentication library today. It was possible to get around the password check if you knew the username. All you had to do was to enter 00 as the password. Or you just entered 00 as the username too and you got authenticated as the first user. [...]]]></description>
			<content:encoded><![CDATA[<p>I was shocked to find a weakness in my authentication library today. It was possible to get around the password check if you knew the username. All you had to do was to enter 00 as the password. Or you just entered 00 as the username too and you got authenticated as the first user.<br />
<span id="more-113"></span><br />
The bug was a combination of these three things:</p>
<ol>
<li>A password (or/and username) that doesn&#8217;t contain any numbers</li>
<li>MySQL type conversion in conditions &#8211; when comparing a VARCHAR column to an INTEGER value it converts VARCHAR to INTEGER, not the other way around.</li>
<li>My own database wrapper was trying to be smart and didn&#8217;t wrap numeric values with apostrophes</li>
</ol>
<p>Consider this template:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> <span class="nu0">1</span> <span class="kw1">FROM</span> my_users <span class="kw1">WHERE</span> <span class="br0">&#40;</span>username = %s<span class="br0">&#41;</span> <span class="kw1">AND</span> <span class="br0">&#40;</span>password = %s<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>and something along these PHP lines:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#8216;username&#8217;</span><span class="br0">&#93;</span> &amp;&amp; <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#8216;password&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// will catch 0 but not 00</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>mysqlValue<span class="br0">&#40;</span><span class="re0">$sqlTemplate</span>, <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#8216;username&#8217;</span><span class="br0">&#93;</span>, <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#8216;password&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;welcome&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;wrong password&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I won&#8217;t go into details about <code>mysqlValue()</code>. The important part is that it checks for special cases of NULL, true, false and is_numeric() and runs the rest through mysql_real_escape_string(). Then it inserts all parameters into the template using vprintf().</p>
<p>The problem is with is_numeric(). After being evaluated as a numeric value, &#8217;00&#8242; is entered into the query without apostrophes.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> <span class="nu0">1</span> <span class="kw1">FROM</span> my_users <span class="kw1">WHERE</span> <span class="br0">&#40;</span>username = <span class="st0">&#8216;my_username&#8217;</span><span class="br0">&#41;</span> <span class="kw1">AND</span> <span class="br0">&#40;</span>password = <span class="nu0">00</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>And because any varchar that doesn&#8217;t contain a number compared to an integer is evaluated as true the result is a catastrophe.</p>
<p><code>'abc' = 00</code> is evaluated as true. <code>'abc1' = 00</code> would be false however, unfortunately only few users append a number to their password. If you use 00 for both the username and the password you get authenticated as the first user who didn&#8217;t do so.</p>
<p>Check out this <a href="http://bugs.mysql.com/bug.php?id=42241">false bug report</a> or the <a href="http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html">related documentation page</a>.</p>
<h2>Why you could want to pass numeric values without apostrophes</h2>
<p>I wonder myself :-) Here are some reasons I remembered:</p>
<ul>
<li>I need to pass NULL in some cases and for that I can&#8217;t use %d
<li>Using apostrophes for integer comparison (1 = &#8217;1&#8242;) means unnecessary type conversion that takes time. A small amount of time but still.</li>
<li>An argument for abandoning the is_numeric() code: Using %d and %s in SQL templates provides a visual clue about what values can be expected there. But then I couldn&#8217;t insert NULL values.</li>
</ul>
<h2>So how to fix this mess?</h2>
<p>What do you think? Should I remove the is_numeric() test and replace all %d with %s in all SQL templates? Remember that <code>printf("%d", "'123'")</code> will return 0.<br />
Or should I keep the is_numeric() test and rewrite vulnerable queries to use for example CAST:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> <span class="nu0">1</span> <span class="kw1">FROM</span> my_users <span class="kw1">WHERE</span> <span class="br0">&#40;</span>username = CAST<span class="br0">&#40;</span>%s <span class="kw1">AS</span> CHAR<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">AND</span> <span class="br0">&#40;</span>password = CAST<span class="br0">&#40;</span>%s <span class="kw1">AS</span> CHAR<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Update 2010-01-18: I added the real reason for wanting to preserve %s in the template.</p>
<p>Update 2010-01-20:<br />
Unfortunately, what I thought to be a solution (CAST(%s AS CHAR)) has another flaw &#8211; strips leading zeroes from the username / password which results into authentication failure.<br />
So dear reader, any ideas?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/programming-techniques/mysql-comparison-catch-abc-00/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dealing with different configuration for development and production box</title>
		<link>http://blog.pepa.info/php-html-css/programming-techniques/different-configuration-development-production-box/</link>
		<comments>http://blog.pepa.info/php-html-css/programming-techniques/different-configuration-development-production-box/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 00:16:42 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=94</guid>
		<description><![CDATA[All right, this isn&#8217;t exactly a rocket science, it&#8217;s just something I use and find very handy. We all develop (and test) on one computer and deploy to a different computer to make the changes live. Having to run the application in two different environments brings a challenge of using different configuration based on where [...]]]></description>
			<content:encoded><![CDATA[<p>All right, this isn&#8217;t exactly a rocket science, it&#8217;s just something I use and find very handy.</p>
<p>We all develop (and test) on one computer and deploy to a different computer to make the changes live. Having to run the application in two different environments brings a challenge of using different configuration based on where the application runs.<br />
<span id="more-94"></span></p>
<h3>Multiple configuration files</h3>
<p>I started by having two configuration files, one for my dev box and another for the production server. They were named the same and I had one on my dev box and one on the server. Each deployment though, meant that I had to be careful not to overwrite the server file with my local copy. That ruled out using a simple synchronization script like lftp or <a title="feature Keep Remote Directory Up To Date" href="http://winscp.net/eng/docs/task_keep_up_to_date" target="_blank">WinScp</a>.</p>
<p>The simplest solution is to name the configuration files differently and upload them both to both servers. All right, even simpler is to have a single configuration php file and define different values using if/else but that can quickly get out of hands as the number of variables grows.</p>
<p>If you need to upload to more production servers (more than one customer) you need to use some more sophisticated deployment script (Ant?).</p>
<p>Let&#8217;s return to the simple solution for now. You need to find a piece of information that can be used as a computer identifier and use it to choose the right set of configuration data.</p>
<h3>$_SERVER['SERVER_NAME'] &amp; $_SERVER['SERVER_ADDR']</h3>
<p>You can use these two for most situations. I develop on my workstation and I have set up domain name based web hosts (C:\xampp\apache\conf\extra\httpd-vhosts.conf) using fake domains (C:\WINDOWS\system32\drivers\etc\hosts).</p>
<p>Using $_SERVER['SERVER_NAME'] works when you know it up front but sometimes your client is going to choose it just before launching the site. Or he decides to change it later on. Not a big deal but I don&#8217;t like chaos.</p>
<p>$_SERVER['SERVER_ADDR'] is also fine but don&#8217;t forget to change it when your production server&#8217;s ip address changes. Not an issue if you use just two computers with 127.0.0.1 and &#8220;else&#8221;.</p>
<h3>Running scripts locally</h3>
<p>Sometimes you need to run a script locally as an extra level of security. Say you run a cron job and you don&#8217;t want public to mess with it by running it off schedule. So you run it as http://localhost/tadada.php and check for SERVER_NAME/ADDR to make sure it was indeed run that way. But then your cannot use it for picking the right configuration set.</p>
<h3>getenv(&#8220;COMPUTERNAME&#8221;) to the rescue</h3>
<p>This value is unlikely to change during the lifetime of your application and doesn&#8217;t change based on the way you run it. The drawback is that not all servers define this variable so you may have to seek a different computer identificator.</p>
<p>What is your way of dealing with configuration sets?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/programming-techniques/different-configuration-development-production-box/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculating time difference in months as a decimal number</title>
		<link>http://blog.pepa.info/php-html-css/design-techniques/calculating-time-difference-in-months-as-a-decimal-number/</link>
		<comments>http://blog.pepa.info/php-html-css/design-techniques/calculating-time-difference-in-months-as-a-decimal-number/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 20:54:21 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Design Techniques]]></category>
		<category><![CDATA[Programming Techniques]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/?p=44</guid>
		<description><![CDATA[There&#8217;s a number of functions out there for calculating the length of time in months but they only provide integer results. MySQL can do it too, for instance. But what if you need to get a precise number? 2008-11-15 minus 2008-10-01 can&#8217;t be one if you&#8217;re calculating rent. It must be 1.466&#8230; function monthCount&#40;$start, $end&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a number of functions out there for calculating the length of time in months but they only provide integer results. MySQL can do it too, for instance. But what if you need to get a precise number?</p>
<p>2008-11-15 minus 2008-10-01 can&#8217;t be one if you&#8217;re calculating rent. It must be 1.466&#8230;</p>
<p><span id="more-44"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> monthCount<span class="br0">&#40;</span><span class="re0">$start</span>, <span class="re0">$end</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.php.net/list"><span class="kw3">list</span></a><span class="br0">&#40;</span><span class="re0">$startYear</span>, <span class="re0">$startMonth</span>, <span class="re0">$startDay</span><span class="br0">&#41;</span> = <a href="http://www.php.net/split"><span class="kw3">split</span></a><span class="br0">&#40;</span><span class="st0">&#8216;-&#8217;</span>, <span class="re0">$start</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.php.net/list"><span class="kw3">list</span></a><span class="br0">&#40;</span><span class="re0">$endYear</span>, <span class="re0">$endMonth</span>, <span class="re0">$endDay</span><span class="br0">&#41;</span> = <a href="http://www.php.net/split"><span class="kw3">split</span></a><span class="br0">&#40;</span><span class="st0">&#8216;-&#8217;</span>, <span class="re0">$end</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="re0">$startMonthCount</span> =</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span><span class="re0">$startYear</span> * <span class="nu0">12</span><span class="br0">&#41;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// number of months since the beginning of our calendar</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re0">$startMonth</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// month number</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="re0">$startDay</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span> / <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&quot;t&quot;</span>, <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="re0">$start</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// day number divided by the number of days in that month</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$endMonthCount</span> = <span class="br0">&#40;</span><span class="re0">$endYear</span> * <span class="nu0">12</span><span class="br0">&#41;</span> + <span class="re0">$endMonth</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="re0">$endDay</span> &#8211; <span class="nu0">1</span><span class="br0">&#41;</span> / <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&quot;t&quot;</span>, <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="re0">$end</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re0">$endMonthCount</span> &#8211; <span class="re0">$startMonthCount</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Note: end date is not inclusive.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/design-techniques/calculating-time-difference-in-months-as-a-decimal-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to concatenate two object attributes and assign the result to a variable</title>
		<link>http://blog.pepa.info/php-html-css/programming-techniques/how-to-concatenate-two-object-attributes-and-assign-the-result-to-a-variable/</link>
		<comments>http://blog.pepa.info/php-html-css/programming-techniques/how-to-concatenate-two-object-attributes-and-assign-the-result-to-a-variable/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 18:21:25 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/2008/03/smarty/how-to-concatenate-two-object-attributes-and-assign-the-result-to-a-variable/</guid>
		<description><![CDATA[All right, this is really no rocket science it&#8217;s just something I needed and took me some time to figure out. &#123;assign var=&#34;fullName&#34; value=&#34;`$human-&#62;first` `$human-&#62;last`&#34;&#125; or more robust version: &#123;capture assign=&#34;fullName&#34;&#125; &#123;$human-&#62;first&#125; &#123;$human-&#62;last&#125; &#123;/capture&#125;]]></description>
			<content:encoded><![CDATA[<p>All right, this is really no rocket science it&#8217;s just something I needed and took me some time to figure out.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><a href="http://smarty.php.net/assign"><span class="kw3">assign</span></a> var=<span class="st0">&quot;fullName&quot;</span> <span class="kw6">value</span>=<span class="st0">&quot;`$human-&gt;first` `$human-&gt;last`&quot;</span><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>or more robust version:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><a href="http://smarty.php.net/capture"><span class="kw1">capture</span></a> <a href="http://smarty.php.net/assign"><span class="kw3">assign</span></a>=<span class="st0">&quot;fullName&quot;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span>$human-&gt;first<span class="br0">&#125;</span> <span class="br0">&#123;</span>$human-&gt;last<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span>/capture<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/programming-techniques/how-to-concatenate-two-object-attributes-and-assign-the-result-to-a-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date formatting in Smarty</title>
		<link>http://blog.pepa.info/php-html-css/programming-techniques/date-formatting-in-smarty/</link>
		<comments>http://blog.pepa.info/php-html-css/programming-techniques/date-formatting-in-smarty/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 21:23:44 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/2007/09/programming-techniques/date-formatting-in-smarty/</guid>
		<description><![CDATA[If you use the so called, German date format (day of month. month. year) you may have wondered how to achieve it with Smarty &#8211; without the leading zeroes for month. You all know the common way to format date in Smarty: &#123;$myDate&#124;date_format:&#34;%e. %m. %Y&#34;&#125; If you ever wanted to have month number without the [...]]]></description>
			<content:encoded><![CDATA[<p>If you use the so called, German date format (day of month. month. year) you may have wondered how to achieve it with Smarty &#8211; without the leading zeroes for month.<br />
<span id="more-11"></span><br />
You all know the common way to format date in Smarty:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span>$myDate|date_format:<span class="st0">&quot;%e. %m. %Y&quot;</span><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If you ever wanted to have month number without the leading zeroes, here&#8217;s how:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span>$myDate|date_format:<span class="st0">&quot;%e. %#m. %Y&quot;</span><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>or even a cooler way:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><a href="http://smarty.php.net/assign"><span class="kw3">assign</span></a> var=<span class="st0">&quot;myDateTimestamp&quot;</span> <span class="kw6">value</span>=$myDate|strtotime<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><span class="st0">&quot;j. n. Y&quot;</span>|date:$myDateTimestamp<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>(because you can use almost any PHP function as Smarty variable modifier, just solve the parameters-order problem)</p>
<p><strong>Question</strong></p>
<p>Got any better solution? Tell me about it!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/programming-techniques/date-formatting-in-smarty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to report messages to user</title>
		<link>http://blog.pepa.info/php-html-css/programming-techniques/how-to-report-messages-to-user/</link>
		<comments>http://blog.pepa.info/php-html-css/programming-techniques/how-to-report-messages-to-user/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 10:21:45 +0000</pubDate>
		<dc:creator>Petr 'PePa' Pavel</dc:creator>
				<category><![CDATA[Programming Techniques]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://blog.pepa.info/2007/09/uncategorized/how-to-report-messages-to-user/</guid>
		<description><![CDATA[In the course of executing a program you need to report errors / success messages to the user. But you don&#8217;t want to mix your code with HTML, right? (A quick explanation on separating presentation from business / data logic.) So here&#8217;s what I use: class MessageStack &#123; var $_stack = array&#40;&#41;; &#8230; /** * [...]]]></description>
			<content:encoded><![CDATA[<p>In the course of executing a program you need to report errors / success messages to the user. But you don&#8217;t want to mix your code with HTML, right? (A <a target="_blank" href="http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21">quick explanation on separating presentation from business / data logic</a>.)</p>
<p>So here&#8217;s what I use:<br />
<span id="more-9"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> MessageStack <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> <span class="re0">$_stack</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">* Factory function, ensures that</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* MessageStack is a singleton</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> create<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> add<span class="br0">&#40;</span><span class="re0">$type</span>, <span class="re0">$text</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$this</span>-&gt;_stack<span class="br0">&#91;</span><span class="re0">$type</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$text</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">* Returns stack contents</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> get<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">return</span> <span class="re0">$this</span>-&gt;_stack;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>When I want to report something I call:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$messageStack</span> = MessageStack::<span class="me2">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$messageStack</span>-&gt;<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&#8216;ok&#8217;</span>, <span class="st0">&#8216;Record saved.&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Then in your main file you run this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$messageStack</span> = MessageStack::<span class="me2">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$smarty</span>-&gt;<span class="me1">assign</span><span class="br0">&#40;</span><span class="st0">&quot;messages&quot;</span>, <span class="re0">$messageStack</span>-&gt;<span class="me1">get</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Include this into template (preferably the part that is included into every template):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><a href="http://smarty.php.net/foreach"><span class="kw1">foreach</span></a> <span class="kw6">from</span>=$messages <span class="kw6">key</span>=type <span class="kw6">item</span>=messagesType<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span><a href="http://smarty.php.net/section"><span class="kw1">section</span></a> <span class="kw6">name</span>=pc <span class="kw6">loop</span>=$messagesType<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&lt;div class=&quot;message <span class="br0">&#123;</span>$type<span class="br0">&#125;</span>&quot;&gt;<span class="br0">&#123;</span>$messagesType<span class="br0">&#91;</span>pc<span class="br0">&#93;</span><span class="br0">&#125;</span>&lt;/div&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span>/section<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span>/foreach<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>And then in css, define all message types:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re1">.message</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">font-weight</span>: <span class="kw2">bold</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">padding</span>: <span class="re3">5px</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="re1">.critical</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">background-color</span>: <span class="kw1">black</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">padding</span>: <span class="re3">2px</span> <span class="re3">5px</span> <span class="re3">3px</span> <span class="re3">5px</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">margin</span>: <span class="re3">3px</span> <span class="re3">0px</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><strong>Question</strong></p>
<p>Do you use something like this or something more clever? Share your wisdom :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pepa.info/php-html-css/programming-techniques/how-to-report-messages-to-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
