<?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>unitstep.net &#187; PHP</title>
	<atom:link href="http://unitstep.net/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<lastBuildDate>Mon, 19 Mar 2012 01:49:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using cURL in PHP to access HTTPS (SSL/TLS) protected sites</title>
		<link>http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</link>
		<comments>http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/#comments</comments>
		<pubDate>Wed, 06 May 2009 01:22:12 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[curl]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[pki]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=877</guid>
		<description><![CDATA[From PHP, you can access the useful cURL Library (libcurl) to make requests to URLs using a variety of protocols such as HTTP, FTP, LDAP and even Gopher. (If you&#8217;ve spent time on the *nix command line, most environments also have the curl command available that uses the libcurl library) In practice, however, the most [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-padlock.jpg" alt="curl-https-padlock" title="curl-https-padlock" width="100" height="116" class="alignnone size-full wp-image-895" /></p>
<p>From <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>, you can access the useful <a href="http://ca2.php.net/manual/en/book.curl.php">cURL Library (libcurl)</a> to make requests to URLs using a variety of protocols such as <acronym class="uttInitialism" title="HyperText Transfer Protocol">HTTP</acronym>, FTP, LDAP and even <a href="http://blog.delicious.com/blog/2009/04/delicious-now-supports-gopher.html">Gopher</a>.  (If you&#8217;ve spent time on the *nix command line, most environments also have the <code>curl</code> command available that uses the libcurl library)</p>
<p>In practice, however, the most commonly-used protocol tends to be <acronym class="uttInitialism" title="HyperText Transfer Protocol">HTTP</acronym>, especially when using <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> for server-to-server communication.  Typically this involves accessing another web server as part of a web service call, using some method such as <a href="http://www.w3.org/XML/" class="ubernym uttInitialism"><acronym class="uttInitialism" title="eXtensible Markup Language">XML</acronym></a>-RPC or REST to query a resource.  For example, <a href="http://delicious.com/">Delicious</a> offers <a href="http://delicious.com/help/api">a <acronym class="uttInitialism" title="HyperText Transfer Protocol">HTTP</acronym>-based API</a> to manipulate and read a user&#8217;s posts.  However, when trying to access a HTTPS resource (such as the delicious API), there&#8217;s a little more configuration you have to do before you can get cURL working right in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.</p>
<h2>The problem</h2>
<p>If you simply try to access a HTTPS (SSL or TLS-protected resource) in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> using cURL, you&#8217;re likely to run into some difficulty.  Say you have the following code: (Error handling omitted for brevity)</p>
<pre><code>// Initialize session and set <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym>.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);</code></pre>
<p>If <code>$url</code> points toward an HTTPS resource, you&#8217;re likely to encounter an error like the one below:</p>
<pre><code>Failed: Error Number: 60. Reason: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed</code></pre>
<p>The problem is that cURL has not been configured to trust the server&#8217;s HTTPS certificate.  The concepts of certificates and PKI revolves around the trust of Certificate Authorities (CAs), and by default, cURL is setup to <strong>not trust any CAs</strong>, thus it won&#8217;t trust any web server&#8217;s certificate.  So why don&#8217;t you have problems visiting HTTPs sites through your web browser? As it happens, the browser developers were nice enough to <a href="/blog/2009/03/16/using-the-basic-constraints-extension-in-x509-v3-certificates-for-intermediate-cas/">include a list of default CAs to trust</a>, covering most situations, so as long as the website operator purchased a certificate from one of these CAs.</p>
<h2>The quick fix</h2>
<p>There are two ways to solve this problem.  Firstly, we can simply configure cURL to accept <strong>any server(peer) certificate</strong>.  This isn&#8217;t optimal from a security point of view, but if you&#8217;re not passing sensitive information back and forth, this is probably alright.  Simply add the following line before calling <code>curl_exec()</code>:</p>
<pre><code>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);</code></pre>
<p>This basically causes cURL to blindly accept any server certificate, without doing any verification as to which CA signed it, and whether or not that CA is trusted.  If you&#8217;re at all concerned about the data you&#8217;re passing to or receiving from the server, you&#8217;ll want to enable this peer verification properly.  Doing so is a bit more complicated.</p>
<h2>The proper fix</h2>
<p>The proper fix involves setting the <code>CURLOPT_CAINFO</code> parameter.  This is used to point towards a CA certificate that cURL should trust.  Thus, any server/peer certificates issued by this CA will also be trusted.  In order to do this, we first need to get the CA certificate.  In this example, I&#8217;ll be using the <a href="https://api.del.icio.us/">https://api.del.icio.us/</a> server as a reference.</p>
<p>First, you&#8217;ll need to visit the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> with your web browser in order to grab the CA certificate.  Then, (in Firefox) open up the security details for the site by double-clicking on the padlock icon in the lower right corner:</p>
<p class="image">
<img src="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-1.jpg" alt="curl-https-1" title="curl-https-1" width="263" height="84" class="alignnone size-full wp-image-891" />
</p>
<p>Then click on &#8220;View Certificate&#8221;:</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-2.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-2-300x250.jpg" alt="curl-https-2" title="curl-https-2" width="300" height="250" class="alignnone size-medium wp-image-890" /></a>
</p>
<p>Bring up the &#8220;Details&#8221; tab of the cerficates page, and <strong>select the certificate at the top of the hierarchy</strong>.  This is the CA certificate.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-3.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-3-255x300.jpg" alt="curl-https-3" title="curl-https-3" width="255" height="300" class="alignnone size-medium wp-image-892" /></a>
</p>
<p>Then click &#8220;Export&#8221;, and save the CA certificate to your selected location, making sure to select the <strong>X.509 Certificate (PEM)</strong> as the save type/format.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-4.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/05/curl-https-4-300x223.jpg" alt="curl-https-4" title="curl-https-4" width="300" height="223" class="alignnone size-medium wp-image-893" /></a>
</p>
<p>Now we need to modify the cURL setup to use this CA certificate, with <code>CURLOPT_CAINFO</code> set to point to where we saved the CA certificate file to.</p>
<pre><code>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");</code></pre>
<p>The other option I&#8217;ve included, <code>CURLOPT_SSL_VERIFYHOST</code> can be set to the following integer values:</p>
<ul>
<li>0: Don&#8217;t check the common name (CN) attribute</li>
<li>1: Check that the common name attribute at least exists</li>
<li>2: Check that the common name exists and that it matches the host name of the server</li>
</ul>
<p>If you have <code>CURLOPT_SSL_VERIFYPEER</code> set to false, then from a security perspective, it doesn&#8217;t really matter what you&#8217;ve set <code>CURLOPT_SSL_VERIFYHOST</code> to, since without peer certificate verification, the server could use any certificate, including a self-signed one that was guaranteed to have a CN that matched the server&#8217;s host name.  So this setting is really only relevant if you&#8217;ve enabled certificate verification.</p>
<p>This ensures that not just any server certificate will be trusted by your cURL session.  For example, if an attacker were to somehow redirect traffic from <strong>api.delicious.com</strong> to their own server, the cURL session here would not properly initialize, since the attacker would not have access to a server certificate (i.e. would not have the private key) trusted by the CA we added.  These steps effectively export the trusted CA from the web browser to the cURL configuration.</p>
<h2>More information</h2>
<p>If you have the CA certificate, but it is not in the PEM format (i.e. it is in a binary or DER format that isn&#8217;t Base64-encoded), you&#8217;ll need to use something like OpenSSL to convert it to the PEM format.  The exact command differs depending on whether you&#8217;re converting from PKCS12 or DER format.</p>
<p>There is a <code>CURLOPT_CAPATH</code> option that allows you to specify a directory that holds multiple CA certificates to trust.  But it&#8217;s not as simple as dumping every single CA certificate in this directory.  Instead, they CA certificates must be named properly, and the <a href="http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html">OpenSSL <code>c_rehash</code> utility can be used</a> to properly setup this directory for use by cURL.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/feed/</wfw:commentRss>
		<slash:comments>113</slash:comments>
		</item>
		<item>
		<title>Getting Xdebug to work with Apache/XAMPP to debug PHP</title>
		<link>http://unitstep.net/blog/2009/01/26/getting-xdebug-to-work-with-apachexampp-to-debug-php/</link>
		<comments>http://unitstep.net/blog/2009/01/26/getting-xdebug-to-work-with-apachexampp-to-debug-php/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 03:00:11 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[debug]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[guides]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=661</guid>
		<description><![CDATA[I&#8217;ve written about Eclipse and how useful it can be, with its extensible plugin-based system. It&#8217;s so useful that I use it everyday for almost any language &#8211; Java, PHP, JavaScript to name a few. It&#8217;s even great for things like CSS and XHTML. PHP is currently my favourite &#8220;hobby&#8221; language and has been for [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><a href="http://xdebug.org/"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/01/xdebug-logo.png" alt="xdebug-logo" title="xdebug-logo" width="200" height="116" /></a></p>
<p>I&#8217;ve written <a href="/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/">about Eclipse</a> and how <a href="/blog/2008/01/19/using-assemblas-trac-with-eclipse-mylyn-xml-rpc-access/">useful it can be</a>, with its extensible plugin-based system. It&#8217;s so useful that I use it everyday for almost any language &#8211; Java, <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>, JavaScript to name a few.  It&#8217;s even great for things like <acronym class="uttInitialism" title="Cascading Style Sheets">CSS</acronym> and <a href="http://www.w3.org/MarkUp/" class="ubernym uttInitialism"><acronym class="uttInitialism" title="eXtensible HyperText Markup Language - HTML reformulated as XML">XHTML</acronym></a>. </p>
<p><acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> is currently my favourite &#8220;hobby&#8221; language and has been for some time.  While I like <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>, one of the things that hasn&#8217;t been straightforward with it is setting up a proper debug session, where you can step through code.  This contrasts heavily with a language like Java, which has always had strong developer tools.  This has resulted in a mass of third-party tools aimed at facilitating <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> debugging.  A while ago, a reader emailed me asking about this very topic, so I decided to put together how-to detailing my experience with the topic and how I went about learning it.</p>
<h2>Xdebug for <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and XAMPP</h2>
<p>The debugger I&#8217;ll be using will be <a href="http://www.xdebug.org/">Xdebug</a>. Because <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> provide no built-in debugging tools, there are many third-party options for debugging. (See the &#8220;Debugging Tools&#8221; section of <a href="http://www.smashingmagazine.com/2009/01/20/50-extremely-useful-php-tools/">this article</a> for more) However, Xdebug seems to be one of the more popular ones, and <a href="http://www.eclipse.org/pdt/">Eclipse PDT</a> already has support for it.</p>
<p>This guide also assumes use of <a href="http://www.apachefriends.org/en/xampp.html">XAMPP</a>, the great all-in-one solution for quickly setting up a web development environment and to get your code running on the server. XAMPP is great for hitting the ground running, though you&#8217;ll probably not want to use it in a production environment &#8211; though you likely won&#8217;t be debugging there either.  Nevertheless, the instructions provided here should work even if you&#8217;ve setup Apache and <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> separately on your own.</p>
<h2>Getting started</h2>
<p>The first thing you&#8217;ll want to do is head over the <a href="http://www.xdebug.org/download.php">Xdebug</a> page and download the appropriate Zend extension of Xdebug corresponding to the version of <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> you&#8217;re running.  Save the file into your <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> extension path/folder.  Now you&#8217;ll have to edit your <code>php.ini</code> file to begin using the plugin.  The plugin basically exposes or provides an interface for the client debugger (running in Eclipse or your IDE) to attach to the server and debug/trace through the code that&#8217;s running on it.  If you&#8217;re from the Java world, you&#8217;ll know this as &#8220;remote debugging&#8221;, which is provided by most J2EE application servers.</p>
<p>You&#8217;ll also want to have downloaded <a href="http://www.eclipse.org/pdt/">Eclipse PDT</a> have that installed as your IDE, if you haven&#8217;t already done so.  <a href="http://www.zend.com/en/products/studio/">Zend Studio for Eclipse</a> also works, since it&#8217;s based on Eclipse PDT, and offers quite a few more features, out of the box.</p>
<h2>Setting up Xdebug</h2>
<p>You should have already saved the Xdebug extension DLL file to your <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> extension folder.  Record down the full path of it.  Now, open up your <code>php.ini</code> file and go down to the <code>[XDebug]</code> section, or create it if it&#8217;s not there.  Uncomment or add the following lines:</p>
<pre><code>;; Only Zend OR (!) XDebug
zend_extension_ts="D:\XAMPP\php\ext\php_xdebug.dll"
xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler=dbgp</code></pre>
<p>The <code>zend_extension_ts</code> should point to location of your Xdebug extension DLL that you downloaded earlier; modify as appropriate.</p>
<p>Then, you <strong>should disable the Xdebug entry in the list of dynamic extensions</strong>. This is confusing, but since we are already setting up Xdebug as a Zend extension, we don&#8217;t need another entry.  Disable the Xdebug dynamic extension by ensuring the following line is commented out, like below:</p>
<pre><code>;extension=php_xdebug.dll</code></pre>
<p>There is one last very important step you need to do, particularly if you are running XAMPP.  <strong>Current versions of Xdebug are incompatible with the Zend optimizer that is enabled by default in XAMPP, so you must disable that if you want Xdebug to work</strong>.  If you don&#8217;t, you&#8217;ll notice that Apache will crash every time you try to load it with Xdebug enabled.  To disable the Zend optimizer, find the <code>[Zend]</code> section in <code>php.ini</code> and comment out all of the entries under it, like so: (This is an example, there may be more to comment out)</p>
<pre><code>[Zend]
;zend_extension_ts = "D:\XAMPP\php\zendOptimizer\lib\ZendExtensionManager.dll"
;zend_extension_manager.optimizer_ts = "D:\XAMPP\php\zendOptimizer\lib\Optimizer"
;zend_optimizer.enable_loader = 0
;zend_optimizer.optimization_level=15
;zend_optimizer.license_path =</code></pre>
<p>You should be able to start Apache now without troubles.</p>
<h2>Configuring Eclipse for <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> debugging</h2>
<p>The next part will be configuring Eclipse as a debugging client.  Since the code will be executing on the web server (Apache), you&#8217;ll need Eclipse to &#8220;hook in&#8221; using the Xdebug protocol.  Thankfully, configuring Eclipse is fairly straightforward.</p>
<p>Open up Eclipse&#8217;s preferences and go to <strong>PHP -> Debug</strong>, and ensure that XDebug is selected as the <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> debugger.  This sets the default for debugging sessions and lessens the configuration required for each debug session.  You can also make sure that the default web server is <code>localhost</code> if that&#8217;s the case, which it&#8217;ll likely be for a lot of people doing development.</p>
<p><strong>Note that if you are running Zend Studio</strong>, you&#8217;ll need to <a href="http://www.maxhorvath.com/2008/08/how-to-enable-the-xdebug-debugger-in-zend-studio-for-eclipse.html">follow the steps in this article to enable Xdebug support</a>.  It seems that some versions of Zend Studio by default disabled support for the Xdebug plugin in lieu of their own Zend Debugger. </p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-1.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-1-300x241.jpg" alt="php-debug-1" title="php-debug-1" width="300" height="241" class="alignnone size-medium wp-image-695" /></a>
</p>
<p>Now you can select a file from a project you&#8217;d like to debug.  In my case, I&#8217;ve selected <code>src/demo/index.php</code> from my <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">Challenge-Response <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Login System</a> project.  Open the file, and then go to the <strong>Run Menu</strong> and select <strong>Debug Configurations&#8230;</strong> or <strong>Open Debug Dialog</strong>.</p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-2.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-2-300x202.jpg" alt="php-debug-2" title="php-debug-2" width="300" height="202" class="alignnone size-medium wp-image-697" /></a>
</p>
<p>Double click the the &#8220;<acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Web Page&#8221; entry on the left side bar to create a new debug profile.  Here, I&#8217;ve named it &#8220;CHAP-PHP&#8221;.  You should see a dialog like the one above.  Make sure the &#8220;Server Debugger&#8221; is again set to Xdebug and that the <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Server is set to the localhost configuration you set up previously.  </p>
<p>Then you have to select the file you want to debug.  Click on &#8220;Browse&#8221;, and you&#8217;re confusingly taken to another view of your Eclipse projects; simply select the same file as before &#8211; you have to select a specific file, and not just a project or folder.</p>
<p>After that, you&#8217;ll need to adjust the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> mapping.  You&#8217;ll probably need to uncheck &#8220;Auto Generate&#8221;, and then <strong>enter the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> that corresponds to the <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> file you&#8217;re debugging</strong>.  Here, I&#8217;ve manually entered <code>/projects/CHAP/trunk/src/demo/</code> as the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> fragment that triggers execution of the script.</p>
<p>If you want the debugger to stop right at the first line to allow you to immediately begin stepping through code, check &#8220;Break at First Line&#8221;. (It may be checked by default) Otherwise, uncheck it if you only want the debugger to stop at the breakpoints you&#8217;ve specified in Eclipse, which is the normal behaviour most developers will expect. </p>
<p>You should now be able to click &#8220;Debug&#8221;, and a debug session will launch, opening up the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> you&#8217;ve specified and allowing you to step through code.  If you don&#8217;t like Eclipse using its own internal web browser (which appears just be a front for IE), you can configure which web browser you&#8217;d like it to launch URLs with by opening up Preferences and then going to <strong>General -> Web Browser</strong> and changing the setting to use an external web browser of your choice.  Personally, Firefox is my preference.</p>
<h2>Start your debugging engines!</h2>
<p>You can now get acquainted with stepping through code, which in my opinion, is one of the best ways to learn! When you launch a debug session, Eclipse should prompt you to switch to a new &#8220;perspective&#8221;, which is just a different layout of Eclipse&#8217;s internal windows that many believe better suit debugging through code.  </p>
<p class="image">
<a href="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-3.jpg"><img src="http://unitstep.net/wordpress/wp-content/uploads/2009/01/php-debug-3-300x227.jpg" alt="php-debug-3" title="php-debug-3" width="300" height="227" class="alignnone size-medium wp-image-698" /></a>
</p>
<p>You&#8217;re provided with an informative view of the script your currently debugging, along with the highlighted line that execution has paused on.  You can set debug breakpoints by double-clicking in the left margin of your source code view window; debug breakpoints show up as blue circles here.  The buttons at the top (green &#8220;Play&#8221;, red &#8220;Stop&#8221; and others) provide control over execution of the code, allowing you to step through code line-by-line, step into functions/methods and return from them.  I encourage you to experiment with all of the controls and get acquainted with the keyboard shortcuts.</p>
<p>Another panel also shows all the current variables available to the script as well as their values.  This is useful since you now do not need to <code>echo</code> anything to output or change any of the code to see values.  </p>
<p>When you&#8217;re done, you can just click the red &#8220;stop&#8221; button to disconnect from the server and end the debug session.  If you&#8217;ve completely stepped through a script, you will not be automatically disconnected from the server.  Instead, the debug client in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> will patiently wait to debug the script again the next time it is executed.  This is useful to know, since you can just go back to the <acronym class="uttInitialism" title="Uniform Resource Locator">URL</acronym> in your web browser and reload the page to trigger the debug session to resume again. </p>
<h2>Conclusion</h2>
<p>I hope you found this useful, as when I was starting out trying to get a <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> debug session to work, it was somewhat frustrating. As always, I welcome your comments, suggestions and questions below via the comments form!</p>
<h3>References</h3>
<ol class="less note">
<li><a href="http://stackoverflow.com/questions/206788/why-does-xdebug-crash-apache-on-every-xampp-install-ive-tried">Why does xdebug crash apache on every XAMPP install I’ve tried?</a></li>
<li><a href="http://xdebug.org/docs/">Xdebug: Documentation</a></li>
<li><a href="http://www.maxhorvath.com/2008/08/how-to-enable-the-xdebug-debugger-in-zend-studio-for-eclipse.html">How to enable the Xdebug debugger in Zend Studio for Eclipse</a></li>
<li><a href="http://devzone.zend.com/article/2930-Debugging-PHP-applications-with-xdebug">Debugging <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> applications with Xdebug</a></li>
</ol>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2009/01/26/getting-xdebug-to-work-with-apachexampp-to-debug-php/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Decoding Google Maps Encoded Polylines using PHP</title>
		<link>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/</link>
		<comments>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 20:32:39 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[polylines]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=361</guid>
		<description><![CDATA[I&#8217;ve talked about the Google Maps encoded polyline format before. While there&#8217;s some nice utilities for encoding polylines that take the work out of implementing it yourself, I couldn&#8217;t find many polyline decoders. This made it somewhat tedious to decode them, as the only way to get the original list of points was to create [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about the <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html">Google Maps encoded polyline format</a> <a href="/blog/2008/05/11/playing-with-google-maps-and-encoded-polylines/">before</a>. While there&#8217;s some nice utilities for <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/">encoding polylines</a> that take the work out of implementing it yourself, I couldn&#8217;t find many polyline <em>decoders</em>.  </p>
<p>This made it somewhat tedious to decode them, as the only way to get the original list of points was to create a <a href="http://code.google.com/apis/maps/documentation/overlays.html#Encoded_Polylines"><code>GPolyline</code></a> and then pull out the points from that object.  This is not ideal since the work must always be done on the client side with JavaScript and using Google Maps.</p>
<p>To solve this, I quickly ported the algorithm over to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> from the JavaScript source.  Please feel free to download/modify/use this script.</p>
<div class="download">
<a class="icon" href="http://unitstep.net/wordpress/wp-content/uploads/2008/08/decodepolylinetoarray.zip">Google Maps Polyline Decoder in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym></a>
</div>
<p>Since the encoded polyline format offers numerous benefits (and because I had data already stored in this format) I did not want to move away from it. At the same time, I needed access to the points for working with things like <a href="http://code.google.com/apis/maps/documentation/staticmaps/">Google Static Maps</a>, which curiously does not accept the encoded polyline format for displaying paths. (Probably to reduce resource usage on their end, since decoding takes CPU time)</p>
<p>Thankfully the polyline decoding algorithm was <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.js">already available</a> at Mark McClure&#8217;s site.  I spent a few minutes understanding the process and porting it over to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.  The source is attached above and is released under an MIT license.  Basically, the only change I had to make was to use some <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> functions to convert characters to their ASCII code, since <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> doesn&#8217;t have a <code>charCodeAt()</code> function.</p>
<p>Please let me know if you find it to be useful.</p>
<h4>Projects using this</h4>
<ul>
<li><a href="http://www.willemstoker.nl/VOR/">VORG Tool</a></li>
</ul>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>CakePHP and error/exception handling</title>
		<link>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/</link>
		<comments>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/#comments</comments>
		<pubDate>Sun, 11 May 2008 18:40:08 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=321</guid>
		<description><![CDATA[I&#8217;m currently using CakePHP and finding it to be quite useful. The &#8220;automagic&#8221; handling of tables is useful for basic relationships, though more complicated setups usually require manual work. The MVC implementation has also clearly drawn inspiration from Ruby on Rails, which may be advantageous to some, though this has no bearing on me. Though [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/05/cakephp.png" alt="Copyright CakePHP" title="cakephp" width="150" height="139" /></p>
<p>I&#8217;m currently using <a href="http://cakephp.org/">CakePHP</a> and finding it to be quite useful.  The &#8220;automagic&#8221; handling of tables is useful for basic relationships, though more complicated setups usually require manual work.  The MVC implementation has also clearly drawn inspiration from <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, which may be advantageous to some, though this has no bearing on me.  Though there are a few things that nag me about CakePHP (such as lack of a good testing suite, though that&#8217;s supposedly <a href="http://bakery.cakephp.org/articles/view/testing-models-with-cakephp-1-2-test-suite">fixed in 1.2</a>, which really should be marked as version 2.0), overall it&#8217;s a great framework that adds badly-needed structure to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and has saved me time.</p>
<p>One thing I&#8217;d like to see, however, is a proper exception handling model.  I realize this would <a href="http://gophp5.org/">require making it <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5-only</a>, but in my opinion, <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5 adds some sorely-need features, such as the aforementioned exception handling model and a class/object system more in line with other languages.</p>
<h3>Signaling intent</h3>
<p>I&#8217;ve written about the <a href="/blog/2007/11/25/exception-handling-in-javascript/">benefits of exception handling</a> before, so I won&#8217;t delve into those details again.  In short, using exceptions is a better way for a method to signal to a caller that something went wrong rather than just returning a special/reserved value as an error code.</p>
<p>In particular, saving of models would benefit from this.  Currently, if you want to check if the model was saved properly, you have to do something like this:</p>
<pre><code>if ($this-&gt;ModelName-&gt;save($this-&gt;data))
{
  // Model saved properly, can continue normally here.
  ...
}
else
{
 // Model did not save properly.
}</code></pre>
<p>It&#8217;s a little bit kludgy, but isn&#8217;t actually that bad since Cake simplifies form validation a lot with the <acronym class="uttInitialism" title="HyperText Markup Language">HTML</acronym> helper in views.  You can simply put something like <code>$html-&gt;tagErrorMsg('Post/title', 'Title is required.')</code> in your view to display that error message when a particular field does not validate.  This removes a lot of the tedium out of making your forms return proper error messages in situations with bad input.</p>
<h3>Exceptional cases</h3>
<p>However, in some situations you might want to execute your own logic when a call to <code>Model::save()</code> fails.  In this case, having that method throw a proper exception would allow the error to be propagated to the controller nicely and would remove the need to make a call to <code>Model::invalidFields()</code> to get the reason.</p>
<p>Coming from a Java background, this makes more sense to me, since I consider its exception handling model to be one of the language&#8217;s strong points.  It has numerous benefits to improving readability and maintainability of code.</p>
<p>However, there are some drawbacks, since this requires the use of <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5.  As CakePHP is designed to be <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 and 5 compatible, exception handling is really not an option.  However, since <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 is so old, I&#8217;d eventually like to see a <a href="http://gophp5.org/">move to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5</a>, especially for its better OOP model and exception handling.  Additionally, <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 6 is just around the corner, so I don&#8217;t really see a need to stick with <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 for that much longer.</p>
<p>Additionally, if you&#8217;re just saving one model (as the result of a form submission) it doesn&#8217;t really make sense, nor is it necessary, to have exception handling since CakePHP already takes care of displaying error message for the invalid fields:</p>
<pre><code>// Controller:
...
if ($this-&gt;Post-&gt;save($this-&gt;data))
{
  $this-&gt;flash('Your post has been saved.','/posts');
}
...

// View:
...
&lt;?php echo $html-&gt;input('Post/title', array('size' =&gt; '40'))?&gt;
&lt;?php echo $html-&gt;tagErrorMsg('Post/title', 'Title is required.') ?&gt;
...</code></pre>
<p>In the above example, taken from <a href="http://manual.cakephp.org/view/326/the-cake-blog-tutorial">CakePHP&#8217;s own blog tutorial</a>, you don&#8217;t really need exception handling since the appropriate actions are automatically handling by Cake.  If the save succeeds, a positive message will be displayed and the user forwarded on.  Otherwise, the controller proceeds to render the given view, where <code>$html-&gt;tagErrorMsg</code> displays the given error message if the associated field was marked as invalid by the model during the save attempt.</p>
<p>For more complicated situations where you&#8217;re saving multiple related models in a single controller action, exception handling would be helpful in reducing the number of branches, since you wouldn&#8217;t need to have so many <code>if ... else</code> blocks muddling up the flow of the code.  However, it&#8217;s not straightforward to integrate this into Cake, since for most of the time, people won&#8217;t need the exception handling capability if they&#8217;re just doing a simple save as outlined above. </p>
<p>The easiest way I see of accomplishing this is to define your own method that wraps around the existing <code>Model::save()</code> method so that proper exceptions can be thrown.  This would, of course, create confusion as then there would be <em>two</em> &#8216;save&#8217; methods. </p>
<p>All things considered, Cake is still a strong framework, so I will continue using it.  For all the slight concerns I have, it&#8217;s still very much beneficial to me.  If there&#8217;s one more thing I could ask for from Cake though, it would be for better documentation!  Maybe I should start contributing&#8230;</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Password salting and the modified Challenge-Response system</title>
		<link>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/</link>
		<comments>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 01:12:42 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[CHAP]]></category>
		<category><![CDATA[chap-php]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[challenge-response]]></category>
		<category><![CDATA[salting]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=312</guid>
		<description><![CDATA[Since I released my demo/example modified Challenge-Response Ajax PHP Login System about a month ago (which was based on ideas from Paul Johnston), I&#8217;ve been receiving some questions about why salting was not incorporated into the system. In particular, there was a discussion at a Dutch-language forums, which I somewhat understood after Google translation. Here, [...]]]></description>
			<content:encoded><![CDATA[<p>Since I released my demo/example <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified Challenge-Response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Login System</a> about a month ago (which was based on ideas from <a href="http://pajhome.org.uk/crypt/md5/auth.html">Paul Johnston</a>), I&#8217;ve been receiving some questions about why <a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salting</a> was not incorporated into the system.  In particular, there was a <a href="http://gathering.tweakers.net/forum/list_messages/1284731///salt">discussion</a> at a Dutch-language forums, which I somewhat understood after <a href="http://www.google.com/translate?u=http%3A%2F%2Fgathering.tweakers.net%2Fforum%2Flist_messages%2F1284731%2F%2F%2Fsalt&#038;langpair=nl%7Cen&#038;hl=en&#038;ie=UTF8">Google translation</a>. </p>
<p>Here, I&#8217;ll try to address some of these concerns and answer some questions.</p>
<h3>Salt of the earth</h3>
<p>To understand why salting is used, we must first understand exactly what it is.  Just like regular salt, in cryptography, salting is used to alter the &#8220;taste&#8221; or output of some process. </p>
<p>In the case of password storage, most people would realize that you should <em>never</em> store lists of users&#8217; passwords in plaintext, because if that data is ever compromised, attackers will gain access not only the compromised accounts but could potentially use the passwords to gain access to user accounts on other services/sites.  This is because people often reuse the same passwords across multiple account/services.</p>
<p>The easiest way to avoid this is to store the <em>hash</em> of the password.  A hash is a one-way function, that is, once you compute the hash of a value, you cannot obtain the original from just the hashed value.  Some typical hash functions are MD5 or the more secure family of SHA hash functions.</p>
<p>However, this still doesn&#8217;t fully conceal passwords.  If an attacker were to obtain the list of hashed passwords, they could try a dictionary-based attack to discover the original inputs.  This involves hashing common words to see if they hash to the correct value.  Since people often use common words or combinations of such, a dictionary-based attack has the advantage of having far fewer combinations that the attacker needs to try compared to a true brute-force attack.</p>
<h3>Adding variability</h3>
<p>This problem can be mitigated by salting, which basically amounts to combining the password with additional input before passing it into the hash function.  This alters the end output from the hash function so that a dictionary-based attack cannot be used, <strong>provided the salt is kept a secret</strong>.  A comparison example:</p>
<pre><code>Without salting:
hash(A) -&gt; B;

With salting:
hash (A + S) -&gt; C;</code></pre>
<p>In the first example, salting was not used before hashing.  Assuming the value &#8216;A&#8217; is a common word or phrase, an attacker can use a dictionary-attack to determine what the value of &#8216;A&#8217; was. (I.E. what value hashes to the value of &#8216;C&#8217;)</p>
<p>With salting, things become more difficult.  If the attacker does not know the value of the salt (S), they cannot use a dictionary-based attack because the actual input will not be a common word or phrase.  Instead, they must try all values in the dictionary <em>with</em> all possible values of the salt.  In fact, every bit of the added salt value doubles the number of computations in a dictionary-based attack.  The important point to retain here is the <strong>salt value must be kept a secret</strong> in order to obtain this benefit.</p>
<p>One other benefit of salting is to ensure that two accounts with the same password don&#8217;t produce the same hash.  This can be accomplished by making different accounts have different salts.  The obvious benefit of this is to decrease the information leakage from the hashes, as otherwise, equivalent hashes would infer equivalent passwords.</p>
<h3>Salting and the modified Challenge-Response System</h3>
<p>With the <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified challenge-response system</a>, it isn&#8217;t clear to me how salting could be used to improve it.  Here&#8217;s a quick re-cap of how it works:</p>
<blockquote><p>
Signup:</p>
<p>1. Server sends random1<br />
2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>1. Server sends random1 and random2<br />
2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>To login, the user must present two values: One to verify that they know the password, and the second value, which is used to set the response that must be computed for the <em>next login</em>.</p>
<p>Because the client must compute the next response value, it&#8217;s a bit tricky to implement salting in a way that&#8217;s beneficial. </p>
<p>One possible method would be to further hash the second value (<code>hex_sha1(hex_hmac_sha1(password, random1))</code>) with a <strong>server secret</strong> before storing it in the database.  This would complicate things should the database be compromised but not the server secret, since a dictionary attack would become much harder with the extra variability of a server secret.  In this case, the work flow would look something like this:</p>
<h4>Signup</h4>
<ol>
<li>Server sends random1</li>
<li>Client sends hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Let's call this value `hashed_challenge_response` for brevity</strong>]</li>
<li>Server stores hex_hmac_sha1(server_secret, hashed_challenge_response)</li>
</ol>
<h4>Login:</h4>
<ol>
<li>Server sends random1 and random2</li>
<li>Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))</li>
<li>Server computes hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Call this `hashed_challenge_response_received`</strong>]</li>
<li>Server checks if hex_hmac_sha1(server_secret, hashed_challenge_response_received) equals the value previously stored.  If so, authentication was successful and a new challenge-response is stored based on the second value received.</li>
</ol>
<p>Note that this method <strong>would not improve the login security anymore</strong>, since an attacker who captured the intermediate traffic of a successful login could still conduct an offline dictionary attack, which this challenge-response system is unfortunately susceptible to.</p>
<p>However, the modified system does benefit from the use of the `random1` and `random2` challenge strings, which are stored alongside the response values.  Since these are random and different for each user (and for each subsequent login), accounts with the same password will not have the same hash-response.  This effectively gives the second lesser benefit of salting.</p>
<p>The modified challenge-response system also suffers from the inability of the server to enforce strong passwords.  Because the initial value sent during registration is a hashed value of the password and a random value, the server cannot be aware of any properties of the password such as its length or composition.  The only aspect that could be enforced server-side would be to make sure the password was not blank!  My suggestion is to (attempt to) enforce password complexity using JavaScript on the client side.  Such rules can obviously be circumvented but are better than nothing.</p>
<h3>Conclusion</h3>
<p>Hopefully I&#8217;ve shed some light on the topic of salting in relation to the modified challenge-response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> login system.  I&#8217;m no security expert, so you should not take my advise as scripture.  Please don&#8217;t hesitate to give me your comments or feedback!</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Challenge-Response Ajax PHP Login System</title>
		<link>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</link>
		<comments>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 01:53:11 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[CHAP]]></category>
		<category><![CDATA[chap-php]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</guid>
		<description><![CDATA[A while ago, (okay, a long while ago) I wrote about a way to improve the security of login/authentication with web applications. The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext. The idea was not mine, but instead the work of a smart fellow named Paul Johnston. At the [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, (okay, a <em>long</em> while ago) I wrote about <a href="/blog/2006/09/19/using-a-chap-login-system-to-improve-security/">a way to improve the security</a> of login/authentication with web applications.  The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext.  The <a href="http://pajhome.org.uk/crypt/md5/auth.html">idea was not mine</a>, but instead the work of a smart fellow named <a href="http://pajhome.org.uk/">Paul Johnston</a>.   At the time, I &#8220;hoped to present an actual implementation&#8221; sometime in the future, but never got around to it.  I finally had some time and decided to put together a working example using <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and JavaScript. </p>
<h3>Download the source</h3>
<p>Please feel free to download and try out the first public release of the CHAP-PHP login system.  The zip file has the full source and provides an example how to implement the system both on the client and server side.</p>
<div class="download">
<a class="icon" href='http://unitstep.net/wordpress/wp-content/uploads/2008/03/chap-php-051.zip' title='CHAP-PHP-0.5.1'>Download CHAP-PHP-0.5.1</a>
</div>
<p>An <a href="/projects/CHAP-PHP/src/demo/index.php">online demo of the system</a> is also available.</p>
<h3>Improving authentication security with Challenge-Response</h3>
<p>Challenge-Response is the basis for many authentication systems.  In such a situation, a server may have to authenticate a user by verifying their credentials, usually in the form of a password.  However, transmitting plaintext passwords over connections that are not secure can lead to compromises.  In such a situation, <dfn>challenge-response</dfn> may be used.  This usually involves the server sending a <strong>random challenge</strong> string to the client, which must then produce an appropriate <strong>response</strong> that can only be computed using the challenge and the password.  This response is then sent to the server, which can then verify if the right password was used to generate it.  The response is usually computed by hashing a value that depends on the challenge and the password, thus it is not possible to obtain the password from the response, which might have been sniffed on an insecure connection. </p>
<p>However, such a traditional challenge-response has the downside that the plaintext password (or a password-equivalent) must be known to the server at some point.  Paul Johnston came up with <a href="http://pajhome.org.uk/crypt/md5/auth.html">an idea for an alternative system</a> a while ago that overcomes these shortcomings.  (Though it is not free from weaknesses itself)  It is this &#8220;Alternative System&#8221; that the above release is based upon.  Here is a quick explanation of the system, adapted from Johnston&#8217;s site:</p>
<blockquote cite="http://pajhome.org.uk/crypt/md5/auth.html"><p>
Signup:</p>
<p>   1. Server sends random1<br />
   2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>   1. Server sends random1 and random2<br />
   2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>During registration, the value sent by the client is stored.  During login, the user must present a value <em>that when hashed</em> produces the value provided at registration.  Because of the non-reversibility property of hashes, knowing the value passed during registration does not allow an attacker to login.  The only way to produce the valid response is to know the actual plaintext password, which is never transmitted or known by the server.  In this system, challenges are linked to a user and must be stored, since it must be known what challenge was used to produce the response.</p>
<p>The second random challenge (random2) and the second value sent by the client during login are used to prevent replay attacks.  Upon successful login, the second value provided by the client becomes the next response, equivalent to the value first provided during registration.  Thus, for the next login, the value that is sent must hash to equal this value.  This also means that the challenges are updated/changed each time a login is successful.  This has the unfortunate downside of revealing when a user has logged in, since the challenge presented at login will be different.  (Challenges must be publicly available)</p>
<p>For a more thorough explanation, I suggest that you <a href="http://pajhome.org.uk/crypt/md5/auth.html">read Johnston&#8217;s article on the subject</a>.</p>
<h3>Getting it to work</h3>
<p>Understanding the process above leads to the conclusion that user-login is now a two-stage process.  Since the challenges are tied to a user, the username must first be known to the server in order to retrieve the challenge for that user.  The client can then use the challenge to produce the response to send to the server for authentication.</p>
<p>Having a two-stage login form would be very unfriendly to users.  Thus, the main challenge is to make it <em>appear</em> as if nothing out of the ordinary is happening.  This is where Ajax comes into play.  When the form is submitted, the event is prevented from occurring normally.  Instead, the username is first retrieved from the form and sent to the server via an Ajax call in order to retrieve the associated challenge.  Once the challenge is received, the appropriate responses are computed, inserted into the form and then the form is submitted.</p>
<p>The current system also works in the case that JavaScript is not enabled/available on the client side.  In this case, challenge-response will not be available, since JavaScript is used to compute the responses.  The server-side <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> scripts infer that JavaScript <em>was not</em> enabled on the client-side if proper challenge-responses are not received, and thus treat the password as plaintext.  In this case, passwords are transmitted in plaintext.  With this code, you have the choice of allowing this &#8220;insecure&#8221; login to proceed or not.</p>
<p>Note that the CHAP-PHP is more of a module than a full-fledged system, since it&#8217;s not intended to be used on its own but instead as part of some application.  It might be a bit confusing if you&#8217;re a non-developer, but I&#8217;ve tried to make it as straightforward and simple as possible so that it will be easy to integrate with existing code bases/frameworks/sites. </p>
<p>Please don&#8217;t hesitate to <a href="/contact">contact me</a> with your questions, comments or suggestions.</p>
<h3>Disclaimer and warning</h3>
<p>You should not use this as the basis for authentication for sensitive data/websites.  I am not a security expert.  At this point, this is more of a proof-of-concept then something concrete.  It is intended to be the starting point for perhaps something more secure and to show that there are alternatives for more secure authentication when SSL is not available.</p>
<h4>Revision History</h4>
<ul class="note less">
<li>0.5 &#8211; First Public Release &#8211; 2008-03-29</li>
<li>0.5.1 &#8211; Fixed file-based storage to be more robust &#8211; 2008-03-30</li>
</ul>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Eclipse: The best and only IDE you&#8217;ll ever need*</title>
		<link>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/</link>
		<comments>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 00:39:16 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/</guid>
		<description><![CDATA[* Sensationalist headline inspired by previous posts Eclipse is my IDE of choice, as you&#8217;l probably have noticed from some of my previous articles. I had been wanting to write an article about why I use it (and why I switched to it), but kept putting it off. Recently, Matt Mullenweg wrote about his problems [...]]]></description>
			<content:encoded><![CDATA[<p class="img align-right"><img src='http://unitstep.net/wordpress/wp-content/uploads/2008/02/eclipse-ide.thumbnail.jpg' alt='Copyright The Eclipse Foundation' /></p>
<p class="note less">* Sensationalist headline inspired by <a href="/blog/2007/10/16/sirreals-g15-plugin-the-best-and-only-logitech-g15-sdk-applet-youll-ever-need/">previous posts</a></p>
<p>Eclipse is my IDE of choice, as you&#8217;l probably have noticed from some of my <a href="/blog/2008/01/19/using-assemblas-trac-with-eclipse-mylyn-xml-rpc-access/">previous articles</a>.  I had been wanting to write an article about why I use it (and why I switched to it), but kept putting it off.  Recently, Matt Mullenweg <a href="http://ma.tt/2008/02/wither-dreamweaver/">wrote about his problems with Dreamweaver</a>, and this perhaps prompted me to organize my notes on why I&#8217;ve chosen to use Eclipse.  Don&#8217;t get me wrong &#8211; I&#8217;m not advocating that you immediately switch and throw out your current editing tool (the headline above, as noted, is purely for sensationalism) &#8211; but rather I&#8217;m just urging you to consider Eclipse for your next project.</p>
<h3>Changing Gears</h3>
<p>Like many, before switching to Eclipse I had been using a pure text editor, Ultraedit, for most of my web-development activities.  Ultraedit seemed fine for <a href="/blog/2007/06/10/ultraedit-php-5-and-the-function-list/">most things</a>, offering basic features like code highlight and autocompletion.  However, it lacked a certain finesse when it came to dealing with larger projects.  For example, if you&#8217;d defined a class, its members wouldn&#8217;t be available for autocompletion.  Something else was needed.  I finally decided to take the plunge, and switch over to Eclipse for all my development towards the end of the summer last year.</p>
<p>Some might wonder why I was even using a text-editor in the first place for development.  For those coming from a traditional programming/development background, the idea of not using an IDE (Integrated Development Environment) is silly.  This is because a lot of programming languages are compiled, and in this case, it just makes sense to use an IDE since it&#8217;s easier to write code, compile and debug using one tool instead of multiple ones. </p>
<p>However, for scripting languages, especially those meant to run on a web server, one can &#8220;get away&#8221; with not using an IDE quite easily.  This is because the scripts are not run standalone but are almost always executed in the context of a web server; thus you&#8217;re usually editing code that you then run on a development web server, without the need for a special tool like a compiler.  Additionally, it&#8217;s easy to view the output using any web browser.  These reasons are what allowed me to persist in using a text-editor for so long.</p>
<h3>No turning back</h3>
<p>However, once I started using Eclipse, I was hooked.  I downloaded Eclipse PDT (<acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Development Tools), which is basically a version of Eclipse bundled with the tools/plugins necessary for setting up a <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> development environment.  Besides offering everything Ultraedit did, it also offered nice features like easy &#8216;Todo&#8217; lists, (just type &#8216;todo&#8217; anywhere in a comment and it&#8217;s automatically indexed by Eclipse into a list), code completion for built-in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> functions and your own as well as a multitude of other advanced features that IDEs have.  Oh, and it&#8217;s also FOSS. (Free and Open Source Software)</p>
<p>However, perhaps the best part about Eclipse is its robust and well-supported plugin system.  This allows Eclipse to pretty much assume any feature that someone is willing to write a plugin for.  This is what really sold me on Eclipse, because this almost makes its abilities endless.  Some of the plugins I use are <a href="http://subclipse.tigris.org/">Subclipse</a> for SVN integration, <a href="http://www.eclipse.org/mylyn/">Mylyn for Trac integration</a> and <a href="http://labs.adobe.com/technologies/jseclipse/">JSEclipse</a> for JavaScript editing.  This is part of the reason why Eclipse is the <a href="http://en.wikipedia.org/wiki/List_of_Eclipse-based_software">basis for many other IDEs</a> out there.</p>
<p>Some other nice features are the ability to link the IDE in with the <a href="http://www.zend.com/en/community/pdt#debugger">Zend Debugger</a>, thus allowing for proper debugging sessions with <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.</p>
<h3>Spoiled</h3>
<p>However, I&#8217;ve been pampered somewhat and have found a few things to complain about, at least when it comes to Eclipse PDT.  I use Eclipse JDT (Java) a work and its advanced <a href="http://en.wikipedia.org/wiki/Refactoring">refactoring abilities</a> are a feature I find myself wanting in the PDT version.  Have you ever found yourself wanting to rename a variable to something more descriptive but putting it off because you&#8217;re afraid you&#8217;ll mess something up by forgetting to change the name somewhere?</p>
<p>With some IDEs, you&#8217;re left having to just do a search-and-replace in order to accomplish what should be a trivial name refactor.  Even if your editor supports regex searches, things can still be tricky &#8211; what if you&#8217;ve used the same name, but in a different context, and thus shouldn&#8217;t change the variable there?  The point is, the process still has to be human-supervised and is tedious.  With Eclipse JDT&#8217;s advanced refactoring, you can rename the variable once &#8211; and the IDE is smart enough to know where else to change it to keep the code consistent &#8211; <strong>very</strong> neat, and I was amazed when I first used it.  Other refactoring abilities include extracting methods out of blocks of code in order to clean up lengthy methods.  All of this makes your life 10 times easier and allows you focus on real programming rather than annoying tasks.</p>
<p>However, Eclipse PDT doesn&#8217;t support this for <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> code, yet.  I hear that it may be supported in a <a href="http://wiki.eclipse.org/PDT/1.5_Features_Proposal">later release</a>, so I have my fingers crossed.  Perhaps accomplishing these refactoring tasks is easier in Java because of its compiled nature or because the JDT project has received more attention.  It&#8217;s definitely possible in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>, as some IDEs, such as the <a href="http://www.zend.com/en/products/studio/">Zend Studio</a> (which is based on Eclipse) support this ability.  Zend Studio, however, is a commercial solution and I haven&#8217;t tried it out yet.</p>
<h3>Nothing&#8217;s perfect</h3>
<p>Eclipse does have its downsides as compared to a traditional text editor.  First of all, it&#8217;s a memory hog &#8211; though most IDEs are.  I have regularly seen Eclipse eat up 300-400 MB of RAM if I&#8217;ve been using it for a long time.  However, it should be noted that I have not had it crash once, so it&#8217;s been rock-solid as far as stability goes.  Nonetheless, I recommend you to have at least 2 GB of memory if you really want to use it properly, since you&#8217;re likely to have other programs open.  This is especially important if you&#8217;re running Windows Vista.  RAM is quiet cheap nowadays, and you can easily pick up 2 GB for $50 or less and upgrading is a painless process, so there&#8217;s no reason not to.</p>
<h3>Finishing up</h3>
<p>Eclipse has changed my life.  Okay, so perhaps I&#8217;m exaggerating a bit.  But, I can say that development, at least for me, would be much harder without Eclipse.  If you&#8217;re still using a text editor for development, I urge you to give Eclipse a try &#8211; just for 30 days, and see how you like it.  I don&#8217;t guarantee results as good as mine, but you may be pleasantly surprised. </p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

