<?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>XDnet Web Hosting Blog &#187; sql</title>
	<atom:link href="http://xdnet.co.uk/blog/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://xdnet.co.uk/blog</link>
	<description>Honest, Reliable Webhosting</description>
	<lastBuildDate>Fri, 11 May 2012 21:08:02 +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>SQL Injections &#8211; Quick and Easy ways to protect yourself with PHP</title>
		<link>http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/</link>
		<comments>http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 14:10:16 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Top Tips]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://xdnet.co.uk/blog/?p=292</guid>
		<description><![CDATA[MySQL is a lovely way to store data, however as soon as we let users adjust the queries we use it can go pear shaped. Here are some really easy ways to protect yourself from those evil users. Check Everything! Seriously, never trust user input. They may not intend to do a SQL inject, but [...]]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fxdnet.co.uk%2Fblog%2F2008%2F11%2F23%2Fsql-injections-quick-and-easy-ways-to-protect-yourself-with-php%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"></g:plusone>
			</div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"  data-text="SQL Injections &#8211; Quick and Easy ways to protect yourself with PHP" data-count="horizontal" data-via="xdnet"></a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>MySQL is a lovely way to store data, however as soon as we let users adjust the queries we use it can go pear shaped. Here are some really easy ways to protect yourself from those evil users.</p>
<p><strong>Check Everything!</strong></p>
<p>Seriously, <span style="text-decoration: underline;">never</span> trust user input. They may not intend to do a SQL inject, but they could still end up doing it anyway. Any data coming from an uncontrolled source should be checked. In the below example I am going to use the <a href="http://uk2.php.net/manual/en/control-structures.if.php">if()</a> function:</p>
<p><code>&lt;?php<br />
</code><code>if(!is_numeric($_GET['ID'])){ // If it's not a number<br />
echo 'Sorry you ID is not a number.';</code><code> die();<br />
}<br />
// Do the SQL here.<br />
?&gt; </code></p>
<p><strong>Hash/Encrypt It</strong></p>
<p>Hashing or encrypting something is essentially changing a string of data into something more manageable. In the below example, I&#8217;ll use <a href="http://uk2.php.net/manual/en/function.md5.php">MD5</a> as my hashing method. This should remove any dangerous characters which could lead to a SQL error.</p>
<p><code>&lt;?php<br />
$password = md5($_POST['password']); // run a MD5 on the password<br />
// If the password was 1234, it will now be 81dc9bdb52d04dc20036dbd8313ed055</code></p>
<p>// do the SQL Query<br />
?&gt;</p>
<p>Of course there are other functions to hash or encrypt something. Take a look at the <a href="http://uk2.php.net/manual/en/index.php">PHP manual</a> for more of them.</p>
<p><strong>Serialize</strong><br />
<a href="http://uk2.php.net/manual/en/function.serialize.php">Serializing</a> a string creates a storable representation of a value, personally this is my favorite way of putting some data into SQL because it can store arrays (So when I select the field, I get a big array of data). Here is a very quick example on how to do it.</p>
<p><code>&lt;?php<br />
$data = serialize($_POST['data']);<br />
// do the SQL Query<br />
?&gt;</code></p>
<div class="bottomcontainerBox" style="">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fxdnet.co.uk%2Fblog%2F2008%2F11%2F23%2Fsql-injections-quick-and-easy-ways-to-protect-yourself-with-php%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:85px; height:21px;"></iframe></div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"></g:plusone>
			</div>
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"  data-text="SQL Injections &#8211; Quick and Easy ways to protect yourself with PHP" data-count="horizontal" data-via="xdnet"></a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div>]]></content:encoded>
			<wfw:commentRss>http://xdnet.co.uk/blog/2008/11/23/sql-injections-quick-and-easy-ways-to-protect-yourself-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

