<?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>Harry Bailey &#187; PHP</title>
	<atom:link href="http://harrybailey.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://harrybailey.com</link>
	<description>just a little bit geeky</description>
	<lastBuildDate>Tue, 24 Aug 2010 12:15:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using PHP_SELF Safely and submitting forms to the same page</title>
		<link>http://harrybailey.com/2009/12/using-php_self-safely-and-submitting-forms-to-the-same-page/</link>
		<comments>http://harrybailey.com/2009/12/using-php_self-safely-and-submitting-forms-to-the-same-page/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:54:55 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[Methods]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=374</guid>
		<description><![CDATA[I&#8217;ve lost count of the number of times i&#8217;ve seen this bit of HTML / PHP:

&#60;form method=&#34;post&#34; action=&#34;&#60;?php print $_SERVER&#91;'PHP_SELF&#34;]; ?&#62;&#34;&#62;

Looks pretty harmless doesn&#8217;t it, but it is a pretty dangerous shortcut to use. Imagin I get a user to visit the page the form is on by following this link, maybe hiding it in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve lost count of the number of times i&#8217;ve seen this bit of HTML / PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">print</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="">'PHP_SELF&quot;]; ?&gt;&quot;&gt;</span></pre></div></div>

<p>Looks pretty harmless doesn&#8217;t it, but it is a pretty dangerous shortcut to use. Imagin I get a user to visit the page the form is on by following this link, maybe hiding it in a short url:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #006600; font-style: italic;">//example.com/formpage.php?&quot;&gt;&lt;script&gt;alert(document.cookie);&lt;/script&gt;</span></pre></div></div>

<p>where I&#8217;ve added some html into the url which contains a script tag.</p>
<p>I could use this method to grab all your cookies and log in as you, or send ajax requests back to the site on your behalf. All very frightening. The quick solution is to turn html characters into their harmless entities using the php function htmlspecialchars. So the code would be</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">print</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;</pre></div></div>

<p>But wait! The best way to submit to the same page with a form is to use and empty action attribute. It&#8217;s valid and it works.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;</pre></div></div>

<p> Don&#8217;t believe me? Go tell Jesse. He also wrote about <a href="http://www.thefutureoftheweb.com/blog/use-empty-form-action-submit-to-current">empty action attributes</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/12/using-php_self-safely-and-submitting-forms-to-the-same-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a PHP Coda Plugin</title>
		<link>http://harrybailey.com/2009/06/writing-a-php-coda-plugin/</link>
		<comments>http://harrybailey.com/2009/06/writing-a-php-coda-plugin/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 01:16:06 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[apps]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=337</guid>
		<description><![CDATA[Sound like a right pain in the arse? It&#8217;s surprisingly simple actually&#8230;.
The steps to creating a simple locally run php Coda plugin:
You must have php installed and running locally.
Start the plugin file with theses lines:

#!/usr/bin/php -q
&#60;?php

(no space between < and ?php)
Where /use/bin/php is the path to you local php install

This is an example of replacing [...]]]></description>
			<content:encoded><![CDATA[<p>Sound like a right pain in the arse? It&#8217;s surprisingly simple actually&#8230;.</p>
<p>The steps to creating a simple locally run php Coda plugin:</p>
<p>You must have php installed and running locally.<br />
Start the plugin file with theses lines:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">#!/usr/bin/php -q
<span style="color: #000000; font-weight: bold;">&lt;?php</span></pre></div></div>

<p>(no space between < and ?php)</p>
<p>Where /use/bin/php is the path to you local php install</p>
<p><span id="more-337"></span></p>
<p>This is an example of replacing a text string with an imaginary tag &#8216;harry&#8217; around it.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">#!/usr/bin/php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* a coda plugin */</span>
&nbsp;
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://stdin&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$input</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$line</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// so now we have input text in $input</span>
&nbsp;
<span style="color: #990000;">print</span> <span style="">'&lt;harry&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$input</span> <span style="color: #339933;">.</span> <span style="">'&lt;/harry&gt;'</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>For much more information:<br />
<a href="http://www.panic.com/coda/developer/howto/plugins.php">http://www.panic.com/coda/developer/howto/plugins.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/06/writing-a-php-coda-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Preload FQL Help</title>
		<link>http://harrybailey.com/2009/05/facebook-preload-fql-help/</link>
		<comments>http://harrybailey.com/2009/05/facebook-preload-fql-help/#comments</comments>
		<pubDate>Thu, 07 May 2009 00:16:00 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=300</guid>
		<description><![CDATA[Facebook have a beta feature on their platform api called Preload FQL. It allows you to specify an FQL call to take place before the request for the page is made from your servers. This could save a full round trip of api request if you&#8217;re clever about it.
Well the wiki article is quite large [...]]]></description>
			<content:encoded><![CDATA[<p>Facebook have a beta feature on their platform api called <a href="http://wiki.developers.facebook.com/index.php/Preload_FQL">Preload FQL</a>. It allows you to specify an FQL call to take place before the request for the page is made from your servers. This could save a full round trip of api request if you&#8217;re clever about it.</p>
<p>Well the wiki article is quite large but misses a few points I thought it worth noting&#8230;</p>
<p><span id="more-300"></span></p>
<h3>1) You need to escape all forward slashes with a backslash</h3>
<p>In the regexp / always needs to be submitted as \/</p>
<h3>2) The url they match again is the full callback url you submitted for your app not the canvas url</h3>
<p>You aren&#8217;t matching just the file names, you can match back down the folders.<br />
So if you use a folder called myapp/ for your index page then your url regexp could be &#8216;myapp\/$&#8217; for the home page<br />
You could then do &#8216;myapp\/anotherpage\.php$ for a different file and &#8216;myapp\/folder\/$ for another folder</p>
<h3>3) Whatever keys you use in the submitted array are used as the new $_POST var keys</h3>
<p>They take your array key &#8216;user_app_friends&#8217; below and append &#8216;fb_sig_&#8217; to it. The final key would be &#8216;fb_sig_user_app_friends&#8217; in this case</p>
<h3>4) You can submit multiple matches in one api request</h3>
<p>Although the examples only show:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$fetch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="">'user_app_friends'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="">'pattern'</span> <span style="color: #339933;">=&gt;</span> <span style="">'myapp\/$'</span><span style="color: #339933;">,</span> 
				<span style="">'query'</span> <span style="color: #339933;">=&gt;</span> <span style="">'SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = {*user*})'</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">admin_setAppProperties</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'preload_fql'</span> <span style="color: #339933;">=&gt;</span> json_encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fetch</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>You could submit:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$fetch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="">'user_app_friends'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="">'pattern'</span> <span style="color: #339933;">=&gt;</span> <span style="">'myapp\/$'</span><span style="color: #339933;">,</span> 
				<span style="">'query'</span> <span style="color: #339933;">=&gt;</span> <span style="">'SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = {*user*})'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="">'user_friends'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="">'pattern'</span> <span style="color: #339933;">=&gt;</span> <span style="">'myapp\/.+$'</span><span style="color: #339933;">,</span>
				<span style="">'query'</span> <span style="color: #339933;">=&gt;</span> <span style="">'SELECT uid2 FROM friend WHERE uid1 = {*user*}'</span>
				<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">admin_setAppProperties</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'preload_fql'</span> <span style="color: #339933;">=&gt;</span> json_encode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fetch</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>5) The easiest way to get data back from the new $_POST variables in json_decode</h3>
<p>Stick the $_POST variable into json_decode(); and then use var_dump to see what you get. It&#8217;s usually pretty obvious.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$user_app_friends</span> <span style="color: #339933;">=</span> json_decode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="">'fb_sig_user_app_friends'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_app_friends</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/05/facebook-preload-fql-help/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dots Aren&#8217;t Allowed In PHP Cookie Names</title>
		<link>http://harrybailey.com/2009/04/dots-arent-allowed-in-php-cookie-names/</link>
		<comments>http://harrybailey.com/2009/04/dots-arent-allowed-in-php-cookie-names/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 09:25:55 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=273</guid>
		<description><![CDATA[When you try and use cookies in php, you can&#8217;t use dots / periods [.] in the names. So this won&#8217;t work:

$var = $_COOKIE&#91;'cookie.name.with.dots'&#93;;

You can set them with dots using Javascript, but when you go to access them, the dots magically become underscores [ _ ]

The reason for all this faffing is that terrible setting [...]]]></description>
			<content:encoded><![CDATA[<p>When you try and use cookies in php, you can&#8217;t use dots / periods [.] in the names. So this won&#8217;t work:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="">'cookie.name.with.dots'</span><span style="color: #009900;">&#93;</span>;</pre></div></div>

<p>You can set them with dots using Javascript, but when you go to access them, the dots magically become underscores [ _ ]</p>
<p><span id="more-273"></span></p>
<p>The reason for all this faffing is that terrible setting called Register Globals, which takes all $_POST, $_GET, $_COOKIE vars and sets them to real variable names. For example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#91;</span><span style="">'my_name'</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>Would be accessible with</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$my_name</span></pre></div></div>

<p>when register globals is turned on. It is a massive security issue and should be turned off at all times.</p>
<p>Register globals means that all cookie, post, get etc variables must meet php variable naming guidelines, which don&#8217;t include dots.</p>
<p>So there you go. Turn register globals off and only use underscores to separate words in cookie names.</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/04/dots-arent-allowed-in-php-cookie-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return Number Of Rows With PHP From SQL Database</title>
		<link>http://harrybailey.com/2009/04/return-number-of-rows-with-php-from-sql-database/</link>
		<comments>http://harrybailey.com/2009/04/return-number-of-rows-with-php-from-sql-database/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 20:52:29 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=254</guid>
		<description><![CDATA[Occasionally working with SQL databases (not my beloved MySQL) I always struggled getting the number of rows in a consistant manner.
Until now&#8230;

It&#8217;s a simple fix. Add &#8216;ORDER BY&#8217; to your SQL select. It doesn&#8217;t matter if you are just getting back items where the order doesn&#8217;t matter, use an order by clause in your select [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally working with SQL databases (not my beloved MySQL) I always struggled getting the number of rows in a consistant manner.</p>
<p>Until now&#8230;</p>
<p><span id="more-254"></span></p>
<p>It&#8217;s a simple fix. Add &#8216;ORDER BY&#8217; to your SQL select. It doesn&#8217;t matter if you are just getting back items where the order doesn&#8217;t matter, use an order by clause in your select and you will always get the number of rows back successfully.</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/04/return-number-of-rows-with-php-from-sql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtual Hosts for PHP5 on OS X 10.5 Leopard</title>
		<link>http://harrybailey.com/2009/03/virtual-hosts-for-php5-on-os-x-105-leopard/</link>
		<comments>http://harrybailey.com/2009/03/virtual-hosts-for-php5-on-os-x-105-leopard/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 14:21:35 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[OS X 10.5]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=244</guid>
		<description><![CDATA[I just spent a good hour trying to work out why virtual hosts wasn&#8217;t working for me on my local machine. Turns out I needed to:
1) Enable virtual hosts by uncommenting the line
Include /private/etc/apache2/extra/httpd-vhosts.conf
 in httpd.conf
2) Add the line
NameVirtualHost 127.0.0.1
 to httpd.conf. I put it under the line
Listen 80
 for now.
then&#8230;

3) Add my new hosts [...]]]></description>
			<content:encoded><![CDATA[<p>I just spent a good hour trying to work out why virtual hosts wasn&#8217;t working for me on my local machine. Turns out I needed to:</p>
<p>1) Enable virtual hosts by uncommenting the line
<pre>Include /private/etc/apache2/extra/httpd-vhosts.conf</pre>
<p> in httpd.conf</p>
<p>2) Add the line
<pre>NameVirtualHost 127.0.0.1</pre>
<p> to httpd.conf. I put it under the line
<pre>Listen 80</pre>
<p> for now.</p>
<p>then&#8230;</p>
<p><span id="more-244"></span></p>
<p>3) Add my new hosts to the bottom of the httpd-vhosts.conf file:</p>
<pre>
&lt;virtualhost mylocalsite.hjb&gt;
	DocumentRoot /Volumes/Drive/Sites/mylocalsite
	ServerName mylocalsite.hjb
	ServerAlias *.mylocalsite.hjb
&lt;/virtualhost&gt;
</pre>
<p>4) Add the sites as new lines to the /private/etc/hosts file as
<pre>127.0.0.1 mylocalsite.hjb</pre>
<p>You can also do this with subdomains if you like. subdomain.mylocalsite.hjb</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/03/virtual-hosts-for-php5-on-os-x-105-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pipe / Send Email to a PHP Script</title>
		<link>http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/</link>
		<comments>http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 16:52:31 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[Methods]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=211</guid>
		<description><![CDATA[Sending (or piping) emails to a php script would allows a whole world of fun. I had a spare 30 minutes the other night so I sat down, read a few blog posts and forums and set up emails to pipe to a php script.

The first step for me was to set up a new [...]]]></description>
			<content:encoded><![CDATA[<p>Sending (or piping) emails to a php script would allows a whole world of fun. I had a spare 30 minutes the other night so I sat down, read a few blog posts and forums and set up emails to pipe to a php script.</p>
<p><span id="more-211"></span></p>
<p>The first step for me was to set up a new subdomain in cPanel (a control panel my <a href="http://www.orchardhosting.com">affordable host</a> includes). This allows me to only send (or pipe) specific email to the script and means I can run a catch-all wildcard forwarder on all emails to that subdomain.</p>
<p>In cPanel I created the subdomain in the normal way. Then I navigated to &#8216;Email Management Tools -> Default E-mail account&#8217;, chose the new subdomain from the dropdown list, clicked &#8216;Advanced Options&#8217; and selected the radio button for &#8216;Pipe to a Program&#8217;.<br />
In the textbox I added the name of the file that would be used to process the emails. In my case <strong>catcher.php</strong> which I had already created in the <strong>root of my site</strong> (not in public_html).</p>
<p>Cpanel is clever enough to add your sites root folder to the name you specify, so will be altered to something like <strong>&#8216;/home/youraccount/catcher.php&#8217;</strong></p>
<p>The next step was to prepare catcher.php to deal with the emails being piped to it. Open the file and change the contents to something along the lines of:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">#!/usr/bin/php -q
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// read from stdin</span>
<span style="color: #000088;">$fd</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://stdin&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$email</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #009900;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="">'you@yoursite.com'</span><span style="color: #339933;">,</span><span style="">'From my email pipe!'</span><span style="color: #339933;">,</span><span style="">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$email</span> <span style="color: #339933;">.</span> <span style="">'&quot;'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The first line of the script is <strong>very</strong> important and will <strong>tell the email pipe to use php</strong>. If your php install is not found at the location show, change it so it is. The -q part tells the pipe not to bounce an email back to the sender. A good thing as we can do that manually in the script.</p>
<p>Make sure you change the permissions of the script to be executable by the email pipe. chmod 755 should do fine.</p>
<p>The script itself will grab the emails contents using fopen on <strong>php://stdin</strong> which is where the email is temporarily stored. You can then manipulate it using such functions as preg_match to grab the parts you want.</p>
<p>The beauty of this method is it will catch all email to that subdomain, so you can create addresses which include the users id and a hash which can be temporary:</p>
<p>1294710241-w98fqwfhi3ho2ih3f@emailin.mysite.com</p>
<p>which you can use to ensure you only act on emails from the intended user.</p>
<p>Note: If you are using -q but still getting bounces, be sure the script is not outputting any data. That means once you have tested it, no print, no echo, no var_dump. When you run the script directly you should see a blank screen and the source code should be empty.</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/02/send-or-pipe-an-email-to-a-php-script/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Delay Before Completion of File Load Solved</title>
		<link>http://harrybailey.com/2009/02/delay-before-completion-of-file-load-solved/</link>
		<comments>http://harrybailey.com/2009/02/delay-before-completion-of-file-load-solved/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 11:16:10 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=208</guid>
		<description><![CDATA[I was pulling my hair out this morning because although a css file I was loading was offering it&#8217;s contents immediately, the file didn&#8217;t complete loading to the browser for another 10+ seconds on each refresh.

After sanity checking everything I realised it&#8217;s because I was doing some string replacement on the file after calculating the [...]]]></description>
			<content:encoded><![CDATA[<p>I was pulling my hair out this morning because although a css file I was loading was offering it&#8217;s contents immediately, the file didn&#8217;t complete loading to the browser for another 10+ seconds on each refresh.</p>
<p><span id="more-208"></span></p>
<p>After sanity checking everything I realised it&#8217;s because I was doing some string replacement on the file after calculating the filesize.</p>
<p>Sending the wrong filesize / string length when you spit a file results in either the browser waiting for content that will never load or cutting the content short.</p>
<p>If you tweak a file before splitting it, be sure to use strlen on your file just before you print to browser.</p>
<p>Another one of <em>those</em> lessons learned</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/02/delay-before-completion-of-file-load-solved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Magic Quotes &#8211; Quickly and Safely Removing Slashes</title>
		<link>http://harrybailey.com/2009/02/magic-quotes-quickly-and-safely-removing-slashes/</link>
		<comments>http://harrybailey.com/2009/02/magic-quotes-quickly-and-safely-removing-slashes/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 15:14:54 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=198</guid>
		<description><![CDATA[I have seen loads of ways of looping though GET, POST and COOKIE and stripping slashes, but I finally think I found one I am happy with.
The benefits it offers include&#8230;


Only runs if magic quotes are turned on
Much faster than anything I have used before
Clean and tidy


	if &#40;get_magic_quotes_gpc&#40;&#41;&#41;
	&#123;
		$in = array&#40;&#38;$_GET, &#38;$_POST, &#38;$_COOKIE&#41;;
		while &#40;list&#40;$k,$v&#41; = each&#40;$in&#41;&#41;
		&#123;
			foreach [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen loads of ways of looping though GET, POST and COOKIE and stripping slashes, but I finally think I found one I am happy with.<br />
The benefits it offers include&#8230;</p>
<p><span id="more-198"></span></p>
<ul>
<li>Only runs if magic quotes are turned on</li>
<li>Much faster than anything I have used before</li>
<li>Clean and tidy</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;">	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$in</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$_GET</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$_POST</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$_COOKIE</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$in</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$in</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span>;
					<span style="color: #b1b100;">continue</span>;
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$in</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$in</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span>;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$in</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/02/magic-quotes-quickly-and-safely-removing-slashes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Location of hosts httpd.conf php.ini on Mac OSX</title>
		<link>http://harrybailey.com/2009/02/location-of-hosts-httpdconf-phpini-on-mac-osx/</link>
		<comments>http://harrybailey.com/2009/02/location-of-hosts-httpdconf-phpini-on-mac-osx/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 15:16:28 +0000</pubDate>
		<dc:creator>Harry</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[OS X 10.4]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[The Web]]></category>

		<guid isPermaLink="false">http://harrybailey.com/?p=194</guid>
		<description><![CDATA[I&#8217;m sick of forgetting where to find my local development files so here is a quick breakdown. It includes hosts file, httpd.conf and php.ini

hosts
The hosts file is used to point domains at an i.p although in most cases it will just be a list of 127.0.0.1 and a domain name such as mysite.local or mysite.hjb [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sick of forgetting where to find my local development files so here is a quick breakdown. It includes hosts file, httpd.conf and php.ini</p>
<p><span id="more-194"></span></p>
<h2>hosts</h2>
<p>The hosts file is used to point domains at an i.p although in most cases it will just be a list of 127.0.0.1 and a domain name such as mysite.local or mysite.hjb in my case.<br />
For < 10.5 you used to use netinfo manager to add extra domains and didn't really use the host file, although in 10.5 (and probably later) you do use the host file.</p>
<p><strong>locations:</strong></p>
<p>/private/etc/</p>
<h2>httpd.conf</h2>
<p>The main apache config file. This is where all the setting for apache are stored. After making changed you need to restart web sharing in system preferences sharing for them to have an effect.</p>
<p><strong>locations:</strong></p>
<p>/private/etc/httpd/</p>
<h2>php.ini</h2>
<p>The main php config file. This is where all the setting for php are stored. After making changed you need to restart web sharing in system preferences sharing for them to have an effect.</p>
<p><strong>locations:</strong></p>
<p>/usr/local/php[4|5]/lib/</p>
<h2>Sites folder</h2>
<p>This is where your actual site files are stored and depends on if you are using the default osx install or a 3rd party install. I strongly suggest <a href="http://www.entropy.ch/software/macosx/php/">Marc Liyanage</a>&#8217;s help here as it deals with plenty of plugins too including the always useful gd2 image library.</p>
<p><strong>locations:</strong></p>
<p>/Users/[username]/Sites/<br />
/Library/Webserver/Documents/</p>
]]></content:encoded>
			<wfw:commentRss>http://harrybailey.com/2009/02/location-of-hosts-httpdconf-phpini-on-mac-osx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
