<?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>Spare Pencil &#187; Programming</title>
	<atom:link href="http://sparepencil.com/archives/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://sparepencil.com</link>
	<description></description>
	<lastBuildDate>Sun, 06 Jun 2010 03:17:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Smokescreen visualisation using Flash AS 3.0</title>
		<link>http://sparepencil.com/archives/2008/05/04/smokescreen-visualisation-using-flash-as-30/</link>
		<comments>http://sparepencil.com/archives/2008/05/04/smokescreen-visualisation-using-flash-as-30/#comments</comments>
		<pubDate>Sun, 04 May 2008 15:32:29 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sparepencil.com/?p=123</guid>
		<description><![CDATA[<p>I have been messing around a bit in Flash recently. I actually managed to learn many things about the new version of the ActionScript language (AS 3.0). Codewise, it is a huge improvement over AS 2.0 (one reason why I&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>I have been messing around a bit in Flash recently. I actually managed to learn many things about the new version of the ActionScript language (AS 3.0). Codewise, it is a huge improvement over AS 2.0 (one reason why I was never very fond of programming with Flash) but on the other hand quite tricky to understand at first. I still find it a little bit confusing.  However, I did make something cool, so I am not totally lost.</p>
<p>Thanks to a number of useful tutorials over at <a href="http://gotandlearn.com">gotoAndLearn()</a> I was introduced to <a href="http://gotoandlearn.com/player.php?id=62">sound spectrums</a>, <a href="http://gotoandlearn.com/player.php?id=63">bitmap filters</a> and <a href="http://gotoandlearn.com/player.php?id=72">creating packages/classes</a> in AS3.0. Of course I had to test myself to see if I had actually learned something. A music visualisation was the result!</p>
<p>Here is what it looks like (if the SWF file is buggy, please tell me how much I suck).</p>
<p><script type="text/javascript" src="http://sparepencil.com/wp-content/plugins/pb-embedflash/js/swfobject.js"></script><span class="embedflash" id="swfid49dd83e40a524a41c01b3ed0294df070"><small>(Please open the article to see the flash file or player.)</small></span><script type="text/javascript">
				var flashvars = {}; var params = {}; var attributes = {};params.allowfullscreen = "true"; params.allowscriptaccess = "always";
				swfobject.embedSWF("http://sparepencil.com/wp-content/uploads/2008/05/smokescreendemo.swf","swfid49dd83e40a524a41c01b3ed0294df070","400","360","9.0.0","http://sparepencil.com/wp-content/plugins/pb-embedflash/swf/expressInstall.swf",flashvars,params,attributes);
		</script></p>
<p>What it does is actually quite simple (once you&#8217;ve completed the previously mentioned tutorials, that is). Every time a frame is rendered it computes the sound spectrum. Flash has a function that does the comlicated stuff and then returns a set of values which describe how the sound is distributed in the frequency spectrum. Basically the same thing as those &#8216;jumping bars&#8217; in your media player (or stereo). The visualisation plots this data on a bitmap object, which is then blurred and scrolled up depending on the overall volume. The process repeats itself over and over, which results in a smoke effect. Variations in volume usually create nice puffs of smoke.</p>
<p>I thought this was quite an interesting little project, so I created an ActionScript class which makes the visualisation easy to reuse in other Flash applications. Admittedly, this is my first well-structured ActionScript code ever. Please <a href="http://sparepencil.com/wp-content/uploads/2008/05/smokescreen.as">have a look at it</a> and then proceed to critise my attempt at programming in the comments.</p>
<p>If you happen to like my creation and want to put it to good use, you can <a href="http://sparepencil.com/wp-content/uploads/2008/05/smokescreen.zip">download the AS3.0 package</a>. You are free to use and modify it without any limitations, but please let me know in case it&#8217;s a public project.</p>
<p>The music in the demo was made by <a href="http://www.ocremix.org/remix/OCR01328/">PriZm at OC ReMix</a>. A great website by the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2008/05/04/smokescreen-visualisation-using-flash-as-30/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Did you know this about PHP?</title>
		<link>http://sparepencil.com/archives/2007/08/18/did-you-know-this-about-php/</link>
		<comments>http://sparepencil.com/archives/2007/08/18/did-you-know-this-about-php/#comments</comments>
		<pubDate>Sat, 18 Aug 2007 15:43:44 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sparepencil.com/archives/2007/08/18/did-you-know-this-about-php/</guid>
		<description><![CDATA[<p>It occurred to me today that there is actually a difference between using the <code>$GLOBALS</code> array and the <code>global</code> keyword in PHP.</p>
<p>You write:</p>
<div class="geshi no php">
<div class="head">function foo()</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div</li></ol></div><p>&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>It occurred to me today that there is actually a difference between using the <code>$GLOBALS</code> array and the <code>global</code> keyword in PHP.</p>
<p>You write:</p>
<div class="geshi no php">
<div class="head">function foo()</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw3">global</span> <span class="re1">$var</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>PHP does:</p>
<div class="geshi no php">
<div class="head">function foo()</div>
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$var</span> <span class="sy0">=&amp;</span> <span class="re1">$GLOBALS</span><span class="br0">&#91;</span><span class="st0">&#39;var&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Why does this matter? The manual <a href="http://php.net/manual/en/language.references.whatdo.php#id2550420">knows the answer</a>.</p>
<p>On another note: I have been working on a new theme for a while now. It&#8217;s probably going to be my best one yet, but I&#8217;ll let you judge about that. I am also developing a WordPress plugin at the moment, it will be released when I launch the theme. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2007/08/18/did-you-know-this-about-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Developing with Symfony</title>
		<link>http://sparepencil.com/archives/2007/06/30/developing-with-symfony/</link>
		<comments>http://sparepencil.com/archives/2007/06/30/developing-with-symfony/#comments</comments>
		<pubDate>Sat, 30 Jun 2007 16:13:03 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://sparepencil.com/archives/2007/06/30/developing-with-symfony/</guid>
		<description><![CDATA[<p>After reading many promising stories about development frameworks such as <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, I decided that I actually had to see it for myself. PHP is probably the one programming language that I know best (<a href="/archives/2007/03/04/programming-languages-i-want-to-learn/">at the moment</a>,&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>After reading many promising stories about development frameworks such as <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, I decided that I actually had to see it for myself. PHP is probably the one programming language that I know best (<a href="/archives/2007/03/04/programming-languages-i-want-to-learn/">at the moment</a>, that is), so the options were limited to, well, <a href="http://en.wikipedia.org/wiki/List_of_web_application_frameworks#PHP">a dozen or so frameworks</a>.</p>
<p><img src="http://sparepencil.com/wp-content/uploads/2007/06/sf-logo.gif" class="alignright" alt="Symfony logo" /><br />
<a href="http://symfony-project.com/">Symfony</a> was chosen for, as far as I am concerned, no apparent reason. But I think it came to my attention due to its growing popularity. Anyway, Symfony has a number of features that are not present in most of the other frameworks.</p>
<p>First of all, Javascript integration. Symfony allows you to enhance your application with Javascript, using mostly PHP!. AJAX is also pretty straightforward and <a href="http://www.prototypejs.org/">Prototype</a> is part of the package.<br />
Furthermore, there are a few useful generators. The most notable is the <a href="http://downloads.symfony-project.com/demo/admin/admin_generator_commented.mov">admin generator</a>.</p>
<p>Besides these features, Symfony has all the conveniences you would expect from a framework. It can really speed up a project once you know how it all works. I am currently in the process of doing just that, learning how it works. This is my first time actually using a framework, though, but I got the hang of it quite fast. I do have to look up all of the built-in functions, which is slightly annoying (Symfony&#8217;s reference documentation is not as good as, for example, PHP&#8217;s).</p>
<p>By the way, if you like using <a href="http://jedit.org/">jEdit</a>, like I do, there are <a href="http://trac.symfony-project.com/trac/wiki/UsingJEdit">some nice tips</a> on how to use it with Symfony. Those tips should be useful for almost any framework, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2007/06/30/developing-with-symfony/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
<enclosure url="http://downloads.symfony-project.com/demo/admin/admin_generator_commented.mov" length="18537976" type="video/quicktime" />
		</item>
		<item>
		<title>Wordpress and the cookies</title>
		<link>http://sparepencil.com/archives/2007/06/03/wordpress-and-the-cookies/</link>
		<comments>http://sparepencil.com/archives/2007/06/03/wordpress-and-the-cookies/#comments</comments>
		<pubDate>Sun, 03 Jun 2007 21:09:14 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sparepencil.com/archives/2007/06/03/wordpress-and-the-cookies/</guid>
		<description><![CDATA[<p><ins datetime="2007-08-21T17:03:59+00:00"><strong>Important:</strong> The workaround provided in this post is not reliable enough, there is now a more secure way to fix this problem. The new fix is available as <a href="http://sparepencil.com/code/ajax-referer-fix/">a convenient plugin</a>.</ins></p>
<p>Since the release of <a href="http://wordpress.org/">Wordpress</a> 2.0, a&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><ins datetime="2007-08-21T17:03:59+00:00"><strong>Important:</strong> The workaround provided in this post is not reliable enough, there is now a more secure way to fix this problem. The new fix is available as <a href="http://sparepencil.com/code/ajax-referer-fix/">a convenient plugin</a>.</ins></p>
<p>Since the release of <a href="http://wordpress.org/">Wordpress</a> 2.0, a pesky bug has been annoying several bloggers. The nice AJAX effects in the administrator panel stopped working; even a fully privileged user would receive a <strong>&#8220;You don&#8217;t have permission to do that&#8221;</strong> message when trying to add or remove categories, posts and more.</p>
<p>The Wordpress developers are <a href="http://trac.wordpress.org/ticket/3807">aware of this bug</a>, but the ticket has recently been moved to milestone 2.4. Which basically means we bloggers will have to wait another few months for an official fix.</p>
<p>So far, it has been unclear as to what is causing this problem. I recently upgraded to Wordpress version 2.2 (many important bugs have been fixed, I recommend it) but I was disappointed to find out that the AJAX problem was still there. That&#8217;s when I decided to check the root of this bug myself.</p>
<h2>Suhosin</h2>
<p><a href="http://www.hardened-php.net/suhosin/">Suhosin</a> is a patch for PHP that hardens it against <a href="http://www.hardened-php.net/suhosin/a_feature_list.html">a wide range of web attacks</a>. It&#8217;s got a truly amazing set of precautions for problems of which you didn&#8217;t even know they existed.</p>
<p>One of its features is cookie encryption. This basically encrypts the cookie using both server and client specific information (a custom key, user agent and more) before sending it to the client. This feature is useful, because if some malicious code on the client-side (<a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> is most common) manages to get a hold of the cookie it can not do anything with it.</p>
<p>In addition to encrypting the cookie before sending it to the client, Suhosin will also decrypt cookies it receives so you can use it in PHP without problems.</p>
<h2>Or can we&#8230;?</h2>
<p>All of the AJAX-powered actions performed in the Wordpress admin panel go to one script: <em>/wp-admin/admin-ajax.php</em><br />
The first thing this script does is calling the function <em>check_ajax_referer()</em> (located in <em>/wp-includes/pluggable.php</em>).<br />
This is what the function looks like:</p>
<pre><code lang="php">function check_ajax_referer() {
	$cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
	foreach ( $cookie as $tasty ) {
		if ( false !== strpos($tasty, USER_COOKIE) )
			$user = substr(strstr($tasty, '='), 1);
		if ( false !== strpos($tasty, PASS_COOKIE) )
			$pass = substr(strstr($tasty, '='), 1);
	}
	if ( !sp_login( $user, $pass, true ) )
		die('-1');
	do_action('check_ajax_referer');
}</code></pre>
<p>As you can see, Wordpress avoids using the HTTP Cookie header for AJAX requests (it&#8217;s even mentioned in the comment). Instead, the cookie is appended to the request data.This is done, of course, on the client side using Javascript.</p>
<p>When <a href="http://developer.mozilla.org/en/docs/DOM:document.cookie">document.cookie</a> is appended to the request, there is still no problem. The Cookie header and the <em>document.cookie</em> are practically the same. But once the request arrives at the server, Suhosin will only decrypt the Cookie. Not the appended <em>document.cookie</em>, because it is not recognised as a cookie at all (it&#8217;s just an encoded variable).</p>
<p>So when Wordpress reads the cookie from the request data, it actually reads the encrypted cookie. And you can&#8217;t log in with an encrypted cookie, so you are denied permission.</p>
<h2>There is a workaround</h2>
<p>Fortunately, most browsers (as far as I know) also send the Cookie header. Suhosin flawlessly decrypts the data contained in this header and puts everything in the <em>$_COOKIE</em> variable.</p>
<p>I am not really sure why the Wordpress developers chose to send cookies via the request body, but until they come up with a better fix for this problem, I am providing the following workaround:</p>
<pre><code lang="php">function check_ajax_referer() {
	// Suhosin workaround
	$dough = ini_get('suhosin.cookie.encrypt');
	if ( 1 == $dough || 'On' == $dough || 'on' == $dough ) {
		$user = $_COOKIE[USER_COOKIE];
		$pass = $_COOKIE[PASS_COOKIE];
	} else {
		$cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
		foreach ( $cookie as $tasty ) {
			if ( false !== strpos($tasty, USER_COOKIE) )
				$user = substr(strstr($tasty, '='), 1);
			if ( false !== strpos($tasty, PASS_COOKIE) )
				$pass = substr(strstr($tasty, '='), 1);
		}
	}
	if ( !sp_login( $user, $pass, true ) )
		die('-1');
	do_action('check_ajax_referer');
}</code></pre>
<p>This can be applied to any Wordpress installation. Servers without Suhosin will run Wordpress the regular way. Servers with Suhosin cookie encryption enabled will make Wordpress fall back to using the standard cookie.</p>
<p>I might look into writing a PHP function that simulates Suhosin cookie decryption. This will allow Wordpress to use the cookie in the request in both cases. Unfortunately, I was unable to find sufficient information for this.</p>
<h2><del datetime="2007-08-21T17:03:59+00:00">Patch for Wordpress 2.2</del></h2>
<p><del datetime="2007-08-21T17:03:59+00:00">I created a patch file for Wordpress 2.2. It must be applied to <em>/wp-includes/pluggable.php</em>.</del></p>
<p><del datetime="2007-08-21T17:03:59+00:00"><a href="">Download the file</a>.<br />
Linux users can patch using:</del><br />
<del datetime="2007-08-21T17:03:59+00:00"><code lang="bash">$ patch /path/to/wp-includes/pluggable.php /path/to/pluggable.diff</code><br />
Windows users can download <a href="http://gnuwin32.sourceforge.net/packages/patch.htm">GNU patch for Windows</a>. And run it from the Command line interface:</del></p>
<pre><code lang="DOS">C:\path\to\patch.exe \path\to\wp-includes\pluggable.php \path\to\pluggable.diff</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2007/06/03/wordpress-and-the-cookies/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>PHP function reference in XML</title>
		<link>http://sparepencil.com/archives/2007/05/11/php-function-reference-in-xml/</link>
		<comments>http://sparepencil.com/archives/2007/05/11/php-function-reference-in-xml/#comments</comments>
		<pubDate>Fri, 11 May 2007 18:08:18 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sparepencil.com/archives/2007/05/11/php-function-reference-in-xml/</guid>
		<description><![CDATA[<p>Sorry for the lack of posts recently, some things have been consuming my time. I do have time for a quick post now, though. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I recently wrote a quick and dirty script that retrieves&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Sorry for the lack of posts recently, some things have been consuming my time. I do have time for a quick post now, though. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I recently wrote a quick and dirty script that retrieves some useful information from the PHP manual docbook files. It parses the function reference documents, to be precise, and it compiles the data into a &#8216;convenient&#8217; XML file.</p>
<p>The XML file was for someone who wanted to integrate PHP function lookup into his IRC bot. But other than that, I have no particular use for it. So might as well publish it on the blog. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="/files/phpref.xml">The file is here</a>. I suggest not clicking that link, unless you want your browser to render 1.5 MB of XML data. Download it some other way, instead.</p>
<p>The documentation is <a href=""http://www.php.net/manual/en/copyright.php>copyrighted by the PHP Documentation Group</a>, but you are free to use it under the terms of the <a href="http://www.php.net/manual/en/opl.license.php">Open Documentation License</a>. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2007/05/11/php-function-reference-in-xml/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Airtight sessions</title>
		<link>http://sparepencil.com/archives/2007/04/15/airtight-sessions/</link>
		<comments>http://sparepencil.com/archives/2007/04/15/airtight-sessions/#comments</comments>
		<pubDate>Sat, 14 Apr 2007 22:13:39 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://sparepencil.com/archives/2007/04/15/airtight-sessions/</guid>
		<description><![CDATA[<p>I recently read <a href="http://www.cbronline.com/article_news.asp?guid=484BC88B-630F-4E74-94E9-8D89DD0E6606">an article</a> which warns about the fact that cookie based authentication can be exploited using JavaScript. I am not going into detail about the problem itself, but I&#8217;ll try to give a brief explanation using an&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>I recently read <a href="http://www.cbronline.com/article_news.asp?guid=484BC88B-630F-4E74-94E9-8D89DD0E6606">an article</a> which warns about the fact that cookie based authentication can be exploited using JavaScript. I am not going into detail about the problem itself, but I&#8217;ll try to give a brief explanation using an example:<br />
Alice is a user browsing the web. She is logged in on site A using a session ID which is conveniently stored in a cookie on her PC. Meanwhile, she browses around a bit and stumbles upon site B. Site B contains an evil piece of JavaScript code that makes a request at site A. There is a loophole in Alice&#8217;s browser&#8217;s (which could be any modern browser) <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>: The request is submitted <strong>with the session cookie</strong> for site A. The request is automatically accepted by site A, because the session was authorised, but Alice has no idea (until she finds out that something disastrous has happened to her account).</p>
<p>You might think the browser is the weak link here, because it doesn&#8217;t handle the cookie correctly. That is true, but apparently it is difficult to flawlessly apply the same origin policy. I might be wrong though, but it doesn&#8217;t really matter for now, because this article focuses on something else.</p>
<p>The server side application at site A could have been protected against the attack. In this article, I will highlight a few methods that could make the process safer. Airtight sessions.</p>
<p>As developers of website applications, we will have to avoid relying on cookie data contained in the HTTP Cookie header when performing certain actions. These certain actions are basically the ones that require authentication (e.g.. changing user preferences, making a blog post, <a href="http://digg.com/">digg</a>ing a story, etc.)<br />
There are two ways in which this can occur: 1) using an HTML form (with <code>POST</code> data) and 2) using the <code><a href="http://www.w3.org/TR/XMLHttpRequest/">XMLHttpRequest</a></code> object. The difference is that the latter uses JavaScript and the first does not (or does not have to, at least).<br />
We&#8217;ll have a look at the HTML form scenario first.</p>
<h2>The HTML form way</h2>
<p>The following form allows an authenticated user to change his or her email address (just try to imagine it, if you don&#8217;t agree).<br />
It is generated by a PHP script (doesn&#8217;t matter for now, but will be useful later),</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$form</span> <span class="sy0">=</span> <span class="st0">&#39;&lt;form action=&quot;change_email.php&quot; method=&quot;post&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt; &lt;input name=&quot;email&quot; type=&quot;text&quot; /&gt; &lt;input name=&quot;change_email&quot; value=&quot;Change&quot; type=&quot;submit&quot; /&gt; &lt;/form&gt;&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="re1">$form</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The relevant part of the PHP script that takes care of this form (<code>change_email.php</code>) looks like this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">session_start</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw2">true</span> <span class="sy0">!=</span> <span class="re1">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#39;logged_in&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">exit</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Deal with the form&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// &#8230;</span></div>
</li>
</ol>
</div>
<p>Note that the user is authenticated by means of the session variable <code>logged_in</code>. The session ID is, of course, obtained via a cookie.</p>
<p>We don&#8217;t want this, because the cookie might not be safe. Instead, we will use a custom solution. (There may be other possibilities to make this form safer, but I will demonstrate just one.)<br />
To avoid using a cookie, we will make the session ID part of the form. The form code snippet will look like this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$form</span> <span class="sy0">=</span> <span class="st0">&#39;&lt;form action=&quot;change_email.php&quot; method=&quot;post&quot; enctype=&quot;application/x-www-form-urlencoded&quot;&gt; &lt;input name=&quot;email&quot; type=&quot;text&quot; /&gt; &lt;input name=&quot;change_email&quot; value=&quot;Change&quot; type=&quot;submit&quot; /&gt; &lt;input name=&quot;session_id&quot; value=&quot;&#39;</span> <span class="sy0">.</span> <span class="kw3">session_id</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st0">&#39;&quot; type=&quot;hidden&quot; /&gt; &lt;/form&gt;&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="re1">$form</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>When the form is submitted, it will also pass the session ID. Easy, right?</p>
<p>Now on to the <code>change_email.php</code> script. We must tell the session handler that we want to use the <code>POST</code>ed session ID instead of the one in the cookie (which is the default).<br />
The relevant part is slightly rewritten:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$safe_id</span> <span class="sy0">=</span> <span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#91;</span><span class="st0">&#39;session_id&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re1">$_POST</span><span class="br0">&#91;</span><span class="st0">&#39;session_id&#39;</span><span class="br0">&#93;</span> <span class="sy0">:</span> <span class="kw2">null</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">session_id</span><span class="br0">&#40;</span><span class="re1">$safe_id</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">session_start</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw2">true</span> <span class="sy0">!=</span> <span class="re1">$_SESSION</span><span class="br0">&#91;</span><span class="st0">&#39;logged_in&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">exit</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// Deal with the form&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// &#8230;</span></div>
</li>
</ol>
</div>
<p>Lines 2 through 6 make sure the <code>POST</code> request is handled using the session that was set in the form. Line 4 may seem kind of useless, but it prevents an <code>E_NOTICE</code> from being triggered (it&#8217;s become a habit of mine to code like this&#8230;)<br />
The <code><a href="http://php.net/manual/en/function.session-id.php">session_id()</a></code> function allows us to change the session ID manually before starting the session.</p>
<p>This method should stop malicious JavaScript on external sites from abusing your cookies.</p>
<h2>The <code>XMLHttpRequest</code> way</h2>
<p>Imagine the previous situation of changing an email address, but then in AJAX style. I am not going into detail regarding the usage of the <code>XMLHttpRequest</code> object, because there are a bunch of frameworks that all work a bit differently.<br />
Just keep the following points in mind:</p>
<ul>
<li>The request method is <code>POST</code> (the recommended method for this kind of stuff, by the way)</li>
<li>The server side file is the same as the last version of <code>change_email.php</code></li>
</ul>
<p>So, basically, you will need to add the session ID cookie to the <code>POST</code> data. Luckily, you can use JavaScript to extract the ID from the existing cookies:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> read_cookie<span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> nameEQ = <span class="kw3">name</span> + <span class="st0">&quot;=&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> ca = document.<span class="me1">cookie</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&#39;;&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i=<span class="nu0">0</span>;i <span class="sy0">&amp;</span>lt; ca.<span class="me1">length</span>;i++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> c = ca<span class="br0">&#91;</span>i<span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>c.<span class="me1">charAt</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>==<span class="st0">&#39; &#39;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; c = c.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">1</span>,c.<span class="me1">length</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>c.<span class="me1">indexOf</span><span class="br0">&#40;</span>nameEQ<span class="br0">&#41;</span> == <span class="nu0">0</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> c.<span class="me1">substring</span><span class="br0">&#40;</span>nameEQ.<span class="me1">length</span>,c.<span class="me1">length</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">session_id = read_cookie<span class="br0">&#40;</span><span class="st0">&#39;PHPSESSID&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// Include session_id in your POST data&#8230;</span></div>
</li>
</ol>
</div>
<p>On line 14, <code>PHPSESSID</code> is the name of the session. It is the default, but if you use a different one, you should keep that in mind. (You can use the <code><a href="http://php.net/manual/en/function.session-name.php">session_name()</a></code> function to find it out.)</p>
<p>This code works because <code>document.cookie</code> is only &#8216;allowed to look at&#8217; cookies that are associated with the current document (so external sites don&#8217;t stand a chance).</p>
<h2>Conclusion</h2>
<p>Cookies are a nice means of keeping sessions going. But when users of your web application perform sensitive tasks, you should not simply rely on a cookie for authentication.<br />
The increasing possibilities of JavaScript can greatly enhance the usability of your application, but it also makes it harder for the browser (but also the developer) to keep things secure. Therefore, make sure sessions are airtight whenever there is interaction between the client and the server.</p>
<p><small>This is my first real article here, so if I forgot to mention basic things or something else, please tell me.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2007/04/15/airtight-sessions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flashy PHP scripts</title>
		<link>http://sparepencil.com/archives/2006/04/07/flashy-php-scripts/</link>
		<comments>http://sparepencil.com/archives/2006/04/07/flashy-php-scripts/#comments</comments>
		<pubDate>Fri, 07 Apr 2006 09:26:29 +0000</pubDate>
		<dc:creator>Bas</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://poisongrafix.com.ru/archives/2006/04/07/flashy-php-scripts/</guid>
		<description><![CDATA[<p>My <a href="http://lifelesspeople.com/">awesome webhost</a> has once again improved their product by adding a new extension for PHP.<br />
It&#8217;s called <a href="http://ming.sourceforge.net/">ming</a> and it can be used to make Flash clips, yes that&#8217;s right, SWF files that work. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_smile.gif'&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://lifelesspeople.com/">awesome webhost</a> has once again improved their product by adding a new extension for PHP.<br />
It&#8217;s called <a href="http://ming.sourceforge.net/">ming</a> and it can be used to make Flash clips, yes that&#8217;s right, SWF files that work. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Ming works similar to image libraries such as GD, but then for Flash.<br />
I don&#8217;t exactly know how much of Flash it actually supports, but from what I&#8217;ve seen it does most of the things that you could possibly be interested in.</p>
<p>On a side-note, ming doesn&#8217;t seem to work very well with PHP4. However, this is not a problem because the <a href="http://lifelesswiki.com/wiki/Technical_Info:Hosting#The_Servers">LLP servers</a> use a technology that allows you to use PHP5 (or even 6) whenever you like.</p>
<p>You can see an on-site example of a &#8216;ming script&#8217; <a href="http://poisongrafix.com.ru/sandbox/ming-trail.php5">here</a>.<br />
Or, if you are interested in seeing more cool stuff, there is <a href="http://www16.brinkster.com/gazb/ming/index.html">a large list of examples here</a>.</p>
<p>Anyway, I think ming is a really nice extra. <img src='http://sparepencil.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><strong>Edit:</strong> Forget what I just said about PHP5. Ming works fine with PHP4 too now. Thanks, Trel!</p>
]]></content:encoded>
			<wfw:commentRss>http://sparepencil.com/archives/2006/04/07/flashy-php-scripts/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
