<?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>Jory&#039;s blog &#187; PHP</title>
	<atom:link href="http://blog.jorygeerts.nl/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jorygeerts.nl</link>
	<description>Just another WordPress weblog, or something like that</description>
	<lastBuildDate>Tue, 09 Aug 2011 19:40:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The power of streams</title>
		<link>http://blog.jorygeerts.nl/2011/01/the-power-of-streams/</link>
		<comments>http://blog.jorygeerts.nl/2011/01/the-power-of-streams/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 21:33:41 +0000</pubDate>
		<dc:creator>Jory</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.jorygeerts.com/?p=100</guid>
		<description><![CDATA[A while ago at work, a customer asked that we add an export of a specific table in his database to his website. It wasn&#8217;t a difficult export, just get every row in the database and output it in a &#8230; <a href="http://blog.jorygeerts.nl/2011/01/the-power-of-streams/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A while ago at work, a customer asked that we add an export of a specific table in his database to his website. It wasn&#8217;t a difficult export, just get every row in the database and output it in <em>a format MS Excel understands</em>. I was assigned the task, so I went at it.</p>
<p>I started to just get the data out of the database. That meant having something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'php/bootstrap.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rResultset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oDatabase</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM mytable&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Simple enough, right? The next thing to do was decide what format the export should be in. All they said was <em>a format MS Excel can understand</em>, so the simples thing I could think of was the <em>CSV</em> format. And since I had recently used <a title="PHP: fgetcsv" href="http://nl2.php.net/fgetcsv" target="_blank">fgetcsv</a> to read some stuff from a csv export, I took a wild guess and typed <a title="PHP: fputcsv" href="http://nl2.php.net/fputcsv" target="_blank">fputcsv</a> in my editor.</p>
<p>So, I had to take the data I already got from the database, create a csv file, and send that to the user with the correct HTTP headers. Easy enough, that means this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Put the data in a file</span>
<span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;export.csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oDatabase</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rResultset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Send the headers</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=mytable-export.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Read the file, from the beginning</span>
<span style="color: #990000;">rewind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sOutput</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sOutput</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Okay, so that should work pretty well. Unless two people run the export at the same time. Better make the filename unique. Oh but then I&#8217;ll get huge load of files in my tmp directory, better delete the file afterwards.</p>
<p>Luckily, at this point, I hadn&#8217;t actually written any code, it was all just pseudo-code in my head. Writing to a file just so I can output it, and then delete that file&#8230; that&#8217;s stupid. (Not to mention a waist of IO, which is expensive enough as it is without me throwing it out the door like this.)</p>
<p>This meant I had a mission: I wanted to output the data right away, but using <a title="PHP: fputcsv" href="http://nl2.php.net/fputcsv" target="_blank">fputcsv</a> since I don&#8217;t want to bother with all the csv stuff myself.</p>
<p>Enter <em>fopen(&#8220;php://output&#8221;, &#8220;w&#8221;);</em> which allows you to write to stdout (read: the webserver, read: the browser) as if it where a file. With that, my script became as simple as this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Send the headers</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=mytable-export.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Pragma: no-cache&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://output&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oDatabase</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rResultset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Rather simple if you ask me. And to think I considered messing with files.</p>
<p>After that, the script needed some tweaking: as first row, they wanted the column names. And &#8220;America style&#8221; csv (where the separator as a comma) doesn&#8217;t work here in Europe, since we use the comma for decimal numbers. Neither of which was a problem:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bHeadersDone</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://output&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oDatabase</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>fetch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rResultset</span><span style="color: #009900;">&#41;</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: #000088;">$bHeadersDone</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$bHeadersDone</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
          <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">fputcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rHandle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Et voila, it does exactly what the customer wants, its just 29 lines long, and it learned me something new. <img src='http://blog.jorygeerts.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jorygeerts.nl/2011/01/the-power-of-streams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;s SoapServer and generating WSDL files</title>
		<link>http://blog.jorygeerts.nl/2010/09/phps-soapserver-and-generating-wsdl-files/</link>
		<comments>http://blog.jorygeerts.nl/2010/09/phps-soapserver-and-generating-wsdl-files/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 21:49:41 +0000</pubDate>
		<dc:creator>Jory</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[SoapServer]]></category>

		<guid isPermaLink="false">http://blog.jorygeerts.com/?p=88</guid>
		<description><![CDATA[Since PHP5, doing stuff with Soap became way easier. SoapServer and SoapClient where added, giving pretty much everybody the ability to create a simple SOAP provider and consumer. But, as always, there is a but. And the &#8216;but&#8217; in this &#8230; <a href="http://blog.jorygeerts.nl/2010/09/phps-soapserver-and-generating-wsdl-files/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since PHP5, doing stuff with Soap became way easier. <a href="http://php.net/soapserver">SoapServer</a> and <a href="http://php.net/soapclient">SoapClient</a> where added, giving pretty much everybody the ability to create a simple SOAP provider and consumer. But, as always, there is a but. And the &#8216;but&#8217; in this case, is that if you go to <em>server.php?wsdl</em>, you get an error stating &#8220;WSDL generation isn&#8217;t supported.&#8221; And anybody who has ever written a WSDL by hand knows that it sucks. So, my mission of tonight: to generate a WSDL file from my PHP code.</p>
<p>Obviously, I started with a search for something like &#8220;php generate wsdl from code&#8221;.  That exact search gives my over 2m results on Google. To bad non of them had the answer for me. Then, I read somebody directing somebody else in the direction of <a title="Zend Framework: Documentation: AutoDiscovery - Zend Framework Manual" href="http://framework.zend.com/manual/en/zend.soap.autodiscovery.html">Zend_Soap_AutoDiscover</a> and decided to take a look myself. It seemed to do what I wanted it to, so I figured, why not have a look at it.</p>
<p>After downloading ZendFramework to my laptop and setting up autoloading for it, the first thing I tried was creating a script separate from my <em>server.php</em> called <em>generate.php</em>. In that script, I included my service class, told <em>Zend_Soap_AutoDiscover</em> I wanted to use that and to <em>handle()</em> requests. Browsed to the file, and voila, I had my WSDL file. So, I saved the file. Then, I found out Zend_Soap_AutoDiscover has a <em>dump($filename)</em> method that can do that for me. Instructed SoapServer and SoapClient to use that WSDL, and it worked!</p>
<p>Well, somewhat. Calling <em>$client-&gt;__getFunctions()</em> showed me my functions. But if I tried to call one of them, it threw an exception. After some working, I noticed that the <em>targetNamespace</em> was <em>http://localhost/soap/generate.php</em>, which is plain wrong as I&#8217;m calling <em>http://localhost/soap/server.php</em>. Ah, that makes sence &#8211; <em>Zend_Soap_AutoDiscover</em> couldn&#8217;t have known I wanted to use <em>server.php</em> now could it?</p>
<p>So then I thought, why not put the <em>Zend_Soap_AutoDiscover</em> code in my <em>server.php</em>, detect <em>?wsdl</em> and give the WSDL in those cases. After first having a small codesnippet in <em>server.php</em> that detects <em>?wsdl</em> and lets <em>Zend_Soap_AutoDiscover</em> do its thing -which worked- I decided the final step was to extend <em>SoapServer</em> so that it would, in fact, support WSDL generation. (Sure, not native, but for native support it&#8217;d need to be done in C, which I don&#8217;t know anywhere near well enough to make an appempt.)</p>
<p>So I went ahead and wrote my class, <em>WsdlGeneratingSoapServer</em> (describing names for the win). It didn&#8217;t need to do much &#8211; just overwrite <em>setClass</em>, <em>setObject</em> and <em>addFunction</em> so that it can keep track of what the <em>SoapServer</em> can do (and then passing on the call to <em>parent::</em>, obviously) and overwrite <em>handle</em>, which should detect <em>?wsdl</em> and, if found, have <em>Zend_Soap_AutoDiscover</em> do the generating, calling <em>parent::handle</em> otherwise.</p>
<p>Sounds easy enough. And it was, I had the class done in about 10 minutes. And then, I tested it. WSDL generation worked perfectly, so goal accomplished! Or well, not really. Normal soapcalls now result in a <em>SoapFault</em>. So I started to try changing things. Long story short, <a title="Note to self: have .phps fixed" href="http://jorygeerts.com/bin/code/php/SoapServer2.phps">this simple code</a> caused a soapfault. And since thats crazy, copied that script from my laptop (which has PHP Version 5.3.2-1ubuntu4.2 + Suhosin Patch 0.9.9.1) to my VPS (which has PHP Version 5.3.3RC4-dev) and what do you know, it worked.</p>
<p>I didn&#8217;t feel like doing any more PHP tonight, so decided to write this blogpost instead. I&#8217;ll try the code at work tomorrow, hopefully it&#8217;ll work there as well. <img src='http://blog.jorygeerts.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In the mean time, <a title="class WsdlGeneratingSoapServer" href="http://jorygeerts.com/bin/code/php/WsdlGeneratingSoapServer.phps">here is the code</a>. If you change <em>new SoapServer(&#8230;);</em> to <em>new WsdlGeneratingSoapServer(&#8230;);</em> (and have <em>Zend_Soap_AutoDiscover</em>, which is part of Zend Framework), you&#8217;ll have WSDL generating abillities, too!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jorygeerts.nl/2010/09/phps-soapserver-and-generating-wsdl-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tricking browsers that are storing passwords</title>
		<link>http://blog.jorygeerts.nl/2008/02/tricking-browsers-that-are-storing-passwords/</link>
		<comments>http://blog.jorygeerts.nl/2008/02/tricking-browsers-that-are-storing-passwords/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 15:55:53 +0000</pubDate>
		<dc:creator>Jory</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[The Internet]]></category>

		<guid isPermaLink="false">http://blog.jorygeerts.com/2008/02/09/tricking-browsers-that-are-storing-passwords/</guid>
		<description><![CDATA[For a system that I&#8217;m working on that I plan to, at some point, release as open source product, I am going to implement a system so that, to get the specific pages, you need to enter your password even &#8230; <a href="http://blog.jorygeerts.nl/2008/02/tricking-browsers-that-are-storing-passwords/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a system that I&#8217;m working on that I plan to, at some point, release as open source product, I am going to implement a system so that, to get the specific pages, you need to enter your password even if you are logged in. If you do, you will remain &#8220;super-authed&#8221; untill you close the browser. This would prevent, ie, my little sister to use my laptop, go to my website and have full power because I&#8217;m still logged in. But as I was working on it, a question arose: How good is this, taking into account that most people will just end up storing the password in their browser. Obviously, there are ways around that. (Using a random name for the password field each time is probally enough.) But, by doing that, I will be breaking browser functionalities. Do I really want to do that?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jorygeerts.nl/2008/02/tricking-browsers-that-are-storing-passwords/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Error reporting</title>
		<link>http://blog.jorygeerts.nl/2007/12/error-reporting/</link>
		<comments>http://blog.jorygeerts.nl/2007/12/error-reporting/#comments</comments>
		<pubDate>Sat, 29 Dec 2007 20:30:45 +0000</pubDate>
		<dc:creator>Jory</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.jorygeerts.com/2007/12/29/error-reporting/</guid>
		<description><![CDATA[This has been a draft for a while, so things are probally outdated but I&#8217;m still wondering about this, so here goes&#8230; I have been doing a lot more PHP lately then I have in a long while and that, &#8230; <a href="http://blog.jorygeerts.nl/2007/12/error-reporting/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This has been a draft for a while, so things are probally outdated but I&#8217;m still wondering about this, so here goes&#8230;</em></p>
<p>I have been doing a lot more PHP lately then I have in a long while and that, plus the prospect of a pretty big project I will hopefully start on during the summer, made me think a bit about error reporting. I&#8217;m not talking about E_ALL vs E_, err, 0. I&#8217;m talking about what to tell the user when either (s)he or the application screws up.<br />
<span id="more-30"></span><br />
As explaining is easier by example, here&#8217;s one: your application has a login section which grants access to who knows what. A user wants to log in, has the correct username but the wrong password. Do you simple tell them &#8220;invalid credentials&#8221; or is it something along the lines of &#8220;Sorry, $prettyusername, but the password you provided does not match the one in our database. You may try again[link to #login]. You can also recover your password[link to password recovery]&#8220;?<br />
From where I stand, both have there up- and downsides. Just telling them they didn&#8217;t enter valid credentials isn&#8217;t anywhere near userfriendly, but if the user is someone who is trying to break into the application I don&#8217;t want to tell them anything.</p>
<p>So, what is more important, being friendly to your users or giving people that try to break in as little help as possible?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jorygeerts.nl/2007/12/error-reporting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

