<?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>Optimus Pete &#187; php</title>
	<atom:link href="http://tech.petegraham.co.uk/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.petegraham.co.uk</link>
	<description>yes i love technology</description>
	<lastBuildDate>Fri, 16 Jul 2010 16:30:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP monitor server log</title>
		<link>http://tech.petegraham.co.uk/2007/06/28/php-monitor-server-log/</link>
		<comments>http://tech.petegraham.co.uk/2007/06/28/php-monitor-server-log/#comments</comments>
		<pubDate>Thu, 28 Jun 2007 17:16:52 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/06/28/php-monitor-server-log/</guid>
		<description><![CDATA[PHP monitor server log
Occasionally you'll be working on a webserver and not have SSH access to it. Below is a simple script I've written that reads a server log (in this case the error log) and outputs a portion of it to a webpage.
PLAIN TEXT
PHP:




$no_lines = 25;


&#160;


$filename = $_SERVER&#91;'DOCUMENT_ROOT'&#93;."/../logs/error.log";


$handle = fopen&#40;$filename, "r"&#41;;


$contents = fread&#40;$handle, filesize&#40;$filename&#41;&#41;;


fclose&#40;$handle&#41;;


&#160;


$contents [...]]]></description>
			<content:encoded><![CDATA[<p>PHP monitor server log</p>
<p>Occasionally you'll be working on a webserver and not have SSH access to it. Below is a simple script I've written that reads a server log (in this case the error log) and outputs a portion of it to a webpage.</p>
<div class="igBar"><span id="lphp-2"><a href="#" onclick="javascript:showPlainTxt('php-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-2">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$no_lines</span> = <span style="color:#CC66CC;color:#800000;">25</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$filename</span> = <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'DOCUMENT_ROOT'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">"/../logs/error.log"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$handle</span> = <a href="http://www.php.net/fopen"><span style="color:#000066;">fopen</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$filename</span>, <span style="color:#FF0000;">"r"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$contents</span> = <a href="http://www.php.net/fread"><span style="color:#000066;">fread</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$handle</span>, <a href="http://www.php.net/filesize"><span style="color:#000066;">filesize</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$filename</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/fclose"><span style="color:#000066;">fclose</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$handle</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$contents</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$contents</span>, <span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$contentsArr</span> = <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>, <span style="color:#0000FF;">$contents</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$size</span> = <a href="http://www.php.net/sizeof"><span style="color:#000066;">sizeof</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$contentsArr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$n</span> = <span style="color:#0000FF;">$size</span> - <span style="color:#0000FF;">$no_lines</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&lt;h1&gt;monitor log&lt;/h1&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&lt;ol start=<span style="color:#000099; font-weight:bold;">\"</span>$n<span style="color:#000099; font-weight:bold;">\"</span>&gt;"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$n</span>; <span style="color:#0000FF;">$n</span>&lt;<span style="color:#0000FF;">$size</span>; <span style="color:#0000FF;">$n</span>++<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">&nbsp; &nbsp; &lt;li&gt;"</span>.<span style="color:#0000FF;">$contentsArr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$n</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#FF0000;">"&lt;/li&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"&lt;/ol&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF0000;">"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Obviously in a production environment you'd want to add some sort of password protection to such a script.</p>
<p>/* Pete Graham */</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/06/28/php-monitor-server-log/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: File Downloads HTTP Headers</title>
		<link>http://tech.petegraham.co.uk/2007/05/16/php-file-downloads-http-headers/</link>
		<comments>http://tech.petegraham.co.uk/2007/05/16/php-file-downloads-http-headers/#comments</comments>
		<pubDate>Wed, 16 May 2007 10:53:47 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Website Development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/05/16/php-file-downloads-http-headers/</guid>
		<description><![CDATA[In PHP you can provide file downloads by using a PHP page to “pass through” the file. Of course you could just link to the actual file, however this technique means your file can kept outside of your website root. Another advantage is you can integrate this system into a PHP based user/password system easily.
Now [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP you can provide file downloads by using a PHP page to “pass through” the file. Of course you could just link to the actual file, however this technique means your file can kept outside of your website root. Another advantage is you can integrate this system into a PHP based user/password system easily.</p>
<p>Now this technique is fairly straightforward, but you need to make sure you get the HTTP Headers correct for it to run smoothly. Here’s the code:</p>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showPlainTxt('php-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file</span> = <span style="color:#FF0000;">"/path/to/file.txt"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$file_extension</span> = <a href="http://www.php.net/strtolower"><span style="color:#000066;">strtolower</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/substr"><span style="color:#000066;">substr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/strrchr"><span style="color:#000066;">strrchr</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$basename</span>,<span style="color:#FF0000;">"."</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//This will set the Content-Type to the appropriate setting for the file</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">switch</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file_extension</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"htm"</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"xhtml"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"txt"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"html"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"text/html"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"pdf"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/pdf"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"doc"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/msword"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"exe"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/octet-stream"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"zip"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/zip"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"xls"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/vnd.ms-excel"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"ppt"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/vnd.ms-powerpoint"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"gif"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"image/gif"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"png"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"image/png"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"jpeg"</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"jpg"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"image/jpeg"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"mp3"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"audio/mpeg"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"wav"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"audio/x-wav"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"mpeg"</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"mpg"</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"mpe"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"video/mpeg"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"mov"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"video/quicktime"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">case</span> <span style="color:#FF0000;">"avi"</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"video/x-msvideo"</span>; <span style="color:#616100;">break</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">default</span>: <span style="color:#0000FF;">$ctype</span>=<span style="color:#FF0000;">"application/force-download"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Begin writing headers</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Pragma: public"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Expires: 0"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Cache-Control: must-revalidate, post-check=0, pre-check=0"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Cache-Control: public"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Content-Description: File Transfer"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//Use the switch-generated Content-Type</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Content-Type: $ctype"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Content-Length: "</span>.<a href="http://www.php.net/filesize"><span style="color:#000066;">filesize</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// regex stops download prompt for browser associated types</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>!<a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'/(htm|xhtml|txt|html|gif|png|jpeg|jpg)/'</span>, <span style="color:#0000FF;">$file_extension</span>, <span style="color:#0000FF;">$matches</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Content-Disposition: attachment; filename=<span style="color:#000099; font-weight:bold;">\"</span>"</span>.<a href="http://www.php.net/basename"><span style="color:#000066;">basename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$filename</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\"</span>;"</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// output the file</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/readfile"><span style="color:#000066;">readfile</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"$file"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I will be writing a follow up article in the future that shows how this technique can be improvement using Apache's mod-rewrite to provide cleaner URLs for file downloads.</p>
<p><strong>Note:</strong> The regular expression on line 42 will make files normally associated with the browser (images, webpages, etc) open inside the browser. If you want to get the “open/save” download prompt for all types of files then comment out this line.</p>
<p><strong>Note:</strong> Make sure you use: “Content-Type: image/jpeg” for JPGs, IE doesn’t seems to like “Content-Type: image/jpg”</p>
<p># Pete Graham</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/05/16/php-file-downloads-http-headers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Get First Index in Associative Array</title>
		<link>http://tech.petegraham.co.uk/2007/05/15/php-get-first-index-in-associative-array/</link>
		<comments>http://tech.petegraham.co.uk/2007/05/15/php-get-first-index-in-associative-array/#comments</comments>
		<pubDate>Tue, 15 May 2007 14:07:43 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Website Development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/05/15/php-get-first-index-in-associative-array/</guid>
		<description><![CDATA[The other day I was programming in PHP and needed to get the first index in an associative array. There doesn’t appear to be a built in function to deal with this so I wrote my own.
PLAIN TEXT
PHP:




function getArrayFirstIndex&#40;$arr&#41;


&#123;


foreach &#40;$arr as $key =&#62; $value&#41;


return $key;


&#125; 






Here's a bit of code you can test it on:
PLAIN [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was programming in PHP and needed to get the first index in an associative array. There doesn’t appear to be a built in function to deal with this so I wrote my own.</p>
<div class="igBar"><span id="lphp-7"><a href="#" onclick="javascript:showPlainTxt('php-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-7">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">function</span> getArrayFirstIndex<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">foreach</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span> <span style="color:#616100;">as</span> <span style="color:#0000FF;">$key</span> =&gt; <span style="color:#0000FF;">$value</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">return</span> <span style="color:#0000FF;">$key</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here's a bit of code you can test it on:</p>
<div class="igBar"><span id="lphp-8"><a href="#" onclick="javascript:showPlainTxt('php-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-8">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'banana'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">"yellow"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'apple'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">"apple"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'orange'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#FF0000;">"orange"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$first_index</span> = getArrayFirstIndex<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> <span style="color:#FF0000;">"first index ="</span>.<span style="color:#0000FF;">$first_index</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The above code will output 'first index = banana'.</p>
<p>/* Pete Graham */</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/05/15/php-get-first-index-in-associative-array/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP: Swap Variables One Liner</title>
		<link>http://tech.petegraham.co.uk/2007/03/29/php-swap-variables-one-liner/</link>
		<comments>http://tech.petegraham.co.uk/2007/03/29/php-swap-variables-one-liner/#comments</comments>
		<pubDate>Thu, 29 Mar 2007 16:26:00 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/03/29/php-swap-variables-one-liner/</guid>
		<description><![CDATA[Here's a nifty one liner in PHP which let's you swap 2 variables values.
PLAIN TEXT
PHP:




list&#40;$a,$b&#41; = array&#40;$b,$a&#41;; 






Much like the PHP XOR swap there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.
/* Pete Graham */
]]></description>
			<content:encoded><![CDATA[<p>Here's a nifty one liner in PHP which let's you swap 2 variables values.</p>
<div class="igBar"><span id="lphp-10"><a href="#" onclick="javascript:showPlainTxt('php-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/list"><span style="color:#000066;">list</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$a</span>,<span style="color:#0000FF;">$b</span><span style="color:#006600; font-weight:bold;">&#41;</span> = <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$b</span>,<span style="color:#0000FF;">$a</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Much like the <a href="http://tech.petegraham.co.uk/2007/03/26/php-xor-swap/">PHP XOR swap</a> there's no real reason this is any better than using a temporary variable, but you might be able to impress chicks with it or something.</p>
<p>/* Pete Graham */</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/03/29/php-swap-variables-one-liner/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Drupal: Hide Navigation on Forum</title>
		<link>http://tech.petegraham.co.uk/2007/03/29/drupal-hide-navigation-on-forum/</link>
		<comments>http://tech.petegraham.co.uk/2007/03/29/drupal-hide-navigation-on-forum/#comments</comments>
		<pubDate>Thu, 29 Mar 2007 16:15:23 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/03/29/drupal-hide-navigation-on-forum/</guid>
		<description><![CDATA[If you want to hide certain Navigational Blocks on your Drupal sites forum(s) here’s how to do it:
1. Go to "Administer » Blocks"
2. Click "configure" on the block you want to hide.
3. On Page specific Visibility Settings click the option for "Show if the following PHP code returns TRUE"
4. Enter the following code in the [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to hide certain Navigational Blocks on your Drupal sites forum(s) here’s how to do it:</p>
<p>1. Go to "Administer » Blocks"<br />
2. Click "configure" on the block you want to hide.<br />
3. On Page specific Visibility Settings click the option for "Show if the following PHP code returns TRUE"<br />
4. Enter the following code in the text area:</p>
<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-12">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#FF0000;">'node'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$node</span> = node_load<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'nid'</span> =&gt; arg<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">return</span> <span style="color:#0000FF;">$node</span>-&gt;<span style="color:#006600;">type</span> != <span style="color:#FF0000;">'forum'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">elseif</span> <span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#FF0000;">'forum'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">return</span> <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This will hide the navigation on the forum hompage, when viewing lists of topics and when viewing individual posts (nodes).</p>
<p># Pete Graham</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/03/29/drupal-hide-navigation-on-forum/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: XOR swap</title>
		<link>http://tech.petegraham.co.uk/2007/03/26/php-xor-swap/</link>
		<comments>http://tech.petegraham.co.uk/2007/03/26/php-xor-swap/#comments</comments>
		<pubDate>Mon, 26 Mar 2007 16:25:49 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Website Development]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/03/26/php-xor-swap/</guid>
		<description><![CDATA[The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also slightly faster.
PLAIN TEXT
PHP:




$x ^= $y;


$y ^= $x;


$x ^= $y; 






I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good [...]]]></description>
			<content:encoded><![CDATA[<p>The XOR swap lets you swap the value of integer variables without using a temporary variable, it's also <a target="_blank" href="http://epsilondelta.net/2006/04/19/php-xor-swap-speed/">slightly faster</a>.</p>
<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-14">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$x</span> ^= <span style="color:#0000FF;">$y</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$y</span> ^= <span style="color:#0000FF;">$x</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$x</span> ^= <span style="color:#0000FF;">$y</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I can't actually think of a good reason Why you'd want to use this in PHP since memory isn't normally a big issue, however it might be good for showing off <img src='http://tech.petegraham.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>// Pete Graham xXx</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/03/26/php-xor-swap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: Remove Values from Array</title>
		<link>http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/</link>
		<comments>http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/#comments</comments>
		<pubDate>Thu, 22 Mar 2007 10:30:17 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/</guid>
		<description><![CDATA[If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PLAIN TEXT
PHP:




// our initial array


$arr = Array&#40;"blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red"&#41;;


print_r&#40;$arr&#41;;


&#160;


// remove the elements who's values are yellow or red


$arr = array_diff&#40;$arr, array&#40;"yellow", "red"&#41;&#41;;


print_r&#40;$arr&#41;;


&#160;


// optionally you could reindex [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:</p>
<div class="igBar"><span id="lphp-16"><a href="#" onclick="javascript:showPlainTxt('php-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-16">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// our initial array</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array"><span style="color:#000066;">Array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"blue"</span>, <span style="color:#FF0000;">"green"</span>, <span style="color:#FF0000;">"red"</span>, <span style="color:#FF0000;">"yellow"</span>, <span style="color:#FF0000;">"green"</span>, <span style="color:#FF0000;">"orange"</span>, <span style="color:#FF0000;">"yellow"</span>, <span style="color:#FF0000;">"indigo"</span>, <span style="color:#FF0000;">"red"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// remove the elements who's values are yellow or red</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array_diff"><span style="color:#000066;">array_diff</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span>, <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"yellow"</span>, <span style="color:#FF0000;">"red"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// optionally you could reindex the array</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$arr</span> = <a href="http://www.php.net/array_values"><span style="color:#000066;">array_values</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.php.net/print_r"><span style="color:#000066;">print_r</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$arr</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is the output from the code above:<br />
Array ( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red )<br />
Array ( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo )<br />
Array ( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo )</p>
<p>Pete Graham xXx</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)</title>
		<link>http://tech.petegraham.co.uk/2007/02/09/email-interaction-with-web-applications-using-php-postfix-and-regular-expressions/</link>
		<comments>http://tech.petegraham.co.uk/2007/02/09/email-interaction-with-web-applications-using-php-postfix-and-regular-expressions/#comments</comments>
		<pubDate>Fri, 09 Feb 2007 14:31:27 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phuser]]></category>
		<category><![CDATA[postfix]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://tech.petegraham.co.uk/2007/02/09/email-interaction-with-web-applications-using-php-postfix-and-regular-expressions/</guid>
		<description><![CDATA[One of the popular facilities on phuser.com is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!
Handling of emails replies has easy [...]]]></description>
			<content:encoded><![CDATA[<p>One of the popular facilities on <a href="http://phuser.com">phuser.com</a> is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!</p>
<p>Handling of emails replies has easy been the trickiest part of this process. Here is a very basic overview of the steps we have taken:</p>
<ol>
<li>Have postfix forward the email to a PHP command line script that saves the email to file. Details of how to do this can be found <a target="_blank" href="http://www.niquille.com/2005/05/19/mail2php-postfix-phpstdin/">here</a>.</li>
<li>Cron runs a PHP script that processes the email files. It makes sure the emails are valid, strips the users reply from the body of the email, and then adds the email to a database table.</li>
<li>Crons runs another script which queries the database table and processes the emails replies, this is where the actually interaction with the phuser system (the magic) happens!</li>
</ol>
<p>Today I shall be describing the regular expression that we used for Step 3 as it took a bit of effort to get right. Step 2 will be covered at a later date.<strong>Warning:</strong> when I cover Step 2 I will be having a rant about my new found hatred of HTML emails!</p>
<p><strong>Note:</strong> Steps 1 and 2 are separate because security precautions on our servers prevent the Postfix user from interacting with the Databases.</p>
<p>When someone gets a poll email from phuser the content looks like this:</p>
<blockquote><p><em>pete (pete graham) wants your opinion:</em></p>
<p><em>Which is your favourite drink?</em></p>
<p><em>Vote for one of the options below:</em></p>
<p><em>1) Tea<br />
2) Coffee<br />
3) I love them both like they were my children<br />
4) Urgh! I can’t stand either!</em></p>
<p><em>--------------------- YOUR REPLY BELOW THIS LINE ---------------------</em></p>
<p><em>--------------------- YOUR REPLY ABOVE THIS LINE ---------------------</em></p>
<p><em>Reply using one of the following methods:</em></p>
<p><em>o Reply to this email. Include the number of your vote at the start of your message. Any extra text will be added as a comment. (Please do not edit the Subject line).</em></p></blockquote>
<p>Now due to the difference in the way email clients work and the ways people think here is a small selection of the possible replies we needed to be able to handle:</p>
<ul>
<li>> 1 I like to drink tea since I am a English Gentleman!</li>
<li>4 I’ll have whats going</li>
<li>3 – I hate them all</li>
<li>> 2)</li>
<li>1) tea because I love tea!!!</li>
<li>1 I'm not a big drinker of either since I'm the no-caffs, but like a cup of decaff tea</li>
<li>> 1</li>
<li>2</li>
</ul>
<p>So what we required is a Regular Expression which would correctly grab the number if one was included at the beginning so we could log the voting option. We also needed to grab the text afterwards if any was included so it could be used as a comment. Here’s what we eventually came up with:</p>
<div class="igBar"><span id="lcode-1"><a onclick="javascript:showPlainTxt('code-1'); return false;" href="http://tech.petegraham.co.uk/#">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span></p>
<div id="code-1">
<div class="code">
<ol>
<li style="font-family: 'Courier New',Courier,monospace; font-style: normal; color: #3a6a8b; font-weight: bold">
<div style="font-family: 'Courier New',Courier,monospace; font-weight: normal">/^<span style="color: #006600; font-weight: bold">[</span>^\w<span style="color: #006600; font-weight: bold">]</span>*<span style="color: #006600; font-weight: bold">(</span><span style="color: #006600; font-weight: bold">[</span><span style="color: #800000">0</span>-<span style="color: #800000">9</span><span style="color: #006600; font-weight: bold">]</span>+<span style="color: #006600; font-weight: bold">)</span><span style="color: #006600; font-weight: bold">[</span>^\w<span style="color: #006600; font-weight: bold">]</span>*<span style="color: #006600; font-weight: bold">(</span>\w.*<span style="color: #006600; font-weight: bold">)</span>/ms</div>
</li>
</ol>
</div>
</div>
</div>
<p>And it works, yay! The 'm' flag is set so the Regex is multiline , the 's' flag makes the dot character match every character including newlines. <a target="_blank" href="http://uk.php.net/manual/en/reference.pcre.pattern.modifiers.php">Here</a> is the page that describes all the PHP regular expression modifiers.</p>
<p>If you want to learn more about regular expressions (and who doesn’t) then loads of information can be found on <a href="http://www.regular-expressions.info/">this site</a>. Another <a target="_blank" href="http://www.fileformat.info/tool/regex.htm">great site</a> is the one that we used to testing our regular expressions, it lets you supply up to 10 different test strings at once.</p>
<p>If you’d like to see this system in action then go to <a target="_blank" href="http://phuser.com">phuser.com</a> and request an invite we are still looking for Beta testers currently, if you sign-up now you can have a real input into the site as it develops and grows.</p>
<p>Ps. The regular expression is in an if statement. If it fails the entire message is put in the comments section on the poll.</p>
<p># Pete Graham</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.petegraham.co.uk/2007/02/09/email-interaction-with-web-applications-using-php-postfix-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
