<?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>DaveOnCode &#187; include</title>
	<atom:link href="http://www.daveoncode.com/tag/include/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +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>Reading querystring variables using SSI and regular expressions</title>
		<link>http://www.daveoncode.com/2009/10/29/reading-querystring-variables-using-server-side-includes-and-regular-expressions/</link>
		<comments>http://www.daveoncode.com/2009/10/29/reading-querystring-variables-using-server-side-includes-and-regular-expressions/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 12:09:23 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[ssi]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=501</guid>
		<description><![CDATA[In these days I worked on a project into which I have to rely on SSI (apache&#8217;s Server Side Includes) in order to read and use url parameters to dynamically include certain html files with &#8220;include virtual&#8221; directive. Unfortunately the documentations available online is not exhaustive, and I had to figure out some things by [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/10/29/reading-querystring-variables-using-server-side-includes-and-regular-expressions/' addthis:title='Reading querystring variables using SSI and regular expressions ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In these days I worked on a project into which I have to rely on SSI (apache&#8217;s Server Side Includes) in order to read and use url parameters to dynamically include certain html files with &#8220;include virtual&#8221; directive. Unfortunately the documentations available online is not exhaustive, and I had to figure out some things by myself.<br />
Anyway, according to the docs, there are several global variables we can use in SSI, two of these are: <strong>DOCUMENT_URI</strong> and <strong>QUERY_STRING</strong>, which are the two we can use to handle the page url. The first returns the (%-decoded) URL path of the document, the second all the string starting with &#8220;?&#8221;.<br />
So, how we can extract our desired variables from these strings, since SSI doesn&#8217;t offer method such &#8220;substring&#8221;, &#8220;split&#8221;, &#8220;indexOf&#8221; and similar? The answer is: by using Regular Expression in a tricky and ingenious way!<br />
SSI offers a basic way to implementing decision flow (<strong>if</strong>, <strong>else</strong>, <strong>elif</strong>), the <strong>if</strong> command has an attribute <strong>expr</strong> which represents a declaration to be valuated, in this attribute is also possible to use a regex to test a given pattern. By knowing this, is possible to declare an SSI variable which represents the desired querystring parameter in the following way:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!--#if expr=&quot;$QUERY_STRING = /year=([0-9]{4})/&quot; --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--ssi-comment: year found --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--#set var=&quot;year&quot; value=&quot;$1&quot; --&gt;</span><br />
<span style="color: #808080; font-style: italic;">&lt;!--#else --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--ssi-comment: year NOT found --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!--#set var=&quot;year&quot; value=&quot;$DATE_LOCAL&quot; --&gt;</span> <br />
<span style="color: #808080; font-style: italic;">&lt;!--#endif --&gt;</span></div></td></tr></tbody></table></div>
<p>In the code above I&#8217;m looking to a querystring parameter called <strong>year</strong> which must be a 4 ({4}) digit number ([0-9]).<br />
If the pattern tested returns true, the matched value (returned by the regex) will be assigned to the SSI variable <strong>year</strong>, otherwise the current server date year ($DATE_LOCAL) will be assigned.</p>
<p>Notes:<br />
1. &#8220;ssi-comment:&#8221; is not a special syntax, but just a comment style I decided to adopt to be readable and understandable.<br />
2. To get only the year from $DATE_LOCAL variable, you must config the format using &#8220;#config timefmt=&#8221;%Y&#8221;"</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/10/29/reading-querystring-variables-using-server-side-includes-and-regular-expressions/' addthis:title='Reading querystring variables using SSI and regular expressions ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/10/29/reading-querystring-variables-using-server-side-includes-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

