<?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; php</title>
	<atom:link href="http://xdnet.co.uk/blog/tag/php/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>
		<item>
		<title>Protecting yourself from MySQL Injections</title>
		<link>http://xdnet.co.uk/blog/2008/09/24/protecting-yourself-from-mysql-injections/</link>
		<comments>http://xdnet.co.uk/blog/2008/09/24/protecting-yourself-from-mysql-injections/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 23:07:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Top Tips]]></category>
		<category><![CDATA[injections]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://xdnet.co.uk/blog/?p=104</guid>
		<description><![CDATA[It seems like everyone is concerned about MySQL injections when it comes to programming, however their is an easy solution out there! Here is the functions I wrote and have been using for a good few months and I&#8217;ve never had any problems. &#60;?php // The SQL Input Function This validates whats  inputted and 'cleans' it. function SQL_Input($input, $is_numeric=FALSE){ if(is_numeric($input) == TRUE &#38;&#38; $is_numeric == TRUE){ [...]]]></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%2F09%2F24%2Fprotecting-yourself-from-mysql-injections%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/09/24/protecting-yourself-from-mysql-injections/"></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/09/24/protecting-yourself-from-mysql-injections/"  data-text="Protecting yourself from MySQL Injections" 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/09/24/protecting-yourself-from-mysql-injections/" 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/09/24/protecting-yourself-from-mysql-injections/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>It seems like everyone is concerned about MySQL injections when it comes to programming, however their is an easy solution out there! Here is the functions I wrote and have been using for a good few months and I&#8217;ve never had any problems.</p>
<blockquote><p> <code><span style="#000000;"><span style="#0000bb;">&lt;?php<br />
</span><span style="#ff8000;">// The SQL Input Function This validates whats <br />
inputted and 'cleans' it.<br />
</span><span style="#007700;">function </span><span style="#0000bb;">SQL_Input</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">, </span><span style="#0000bb;">$is_numeric</span><span style="#007700;">=</span><span style="#0000bb;">FALSE</span><span style="#007700;">){<br />
if(</span><span style="#0000bb;">is_numeric</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">) == </span><span style="#0000bb;">TRUE </span><span style="#007700;">&amp;&amp; </span><span style="#0000bb;">$is_numeric </span><span style="#007700;">== </span><span style="#0000bb;">TRUE</span><span style="#007700;">){ </span><span style="#ff8000;">// If it's a number and it's ment to be a number.<br />
</span><span style="#007700;">return </span><span style="#0000bb;">$input</span><span style="#007700;">;<br />
}else{ </span><span style="#ff8000;">// Anything else<br />
</span><span style="#007700;">return </span><span style="#0000bb;">serialize</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">); </span><span style="#ff8000;">// You can replace this for a base64,<br />
 but I like serialize's.<br />
</span><span style="#007700;">}<br />
}</span></span></code></p>
<p><span style="#ff8000;">// The SQL output function. This convets the stuff pulled from the <br />
SQL back to normal text.<br />
</span><span style="#007700;">function </span><span style="#0000bb;">SQL_Output</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">, </span><span style="#0000bb;">$is_numeric</span><span style="#007700;">=</span><span style="#0000bb;">FALSE</span><span style="#007700;">){<br />
if(</span><span style="#0000bb;">is_numeric</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">) == </span><span style="#0000bb;">TRUE </span><span style="#007700;">&amp;&amp; </span><span style="#0000bb;">$is_numeric </span><span style="#007700;">== </span><span style="#0000bb;">TRUE</span><span style="#007700;">){ </span><span style="#ff8000;">// If it&#8217;s a number and it&#8217;s ment to be a number.<br />
</span><span style="#007700;">return </span><span style="#0000bb;">$input</span><span style="#007700;">;<br />
}else{ </span><span style="#ff8000;">// Anything else<br />
</span><span style="#007700;">return </span><span style="#0000bb;">unserialize</span><span style="#007700;">(</span><span style="#0000bb;">$input</span><span style="#007700;">);<br />
}<br />
}</span></p>
<p><span style="#ff8000;">### Example ###<br />
/* In the next example I&#8217;m going to pull some data from a table called &#8217;table&#8217;. <br />
Here is some info about the table:<br />
ID - Should only be numbers.<br />
Text - Is some plain text.</span></p>
<p>And the input is:<br />
$_POST['ID'] - should be a number.<br />
$_POST['Text'] - is some general text.<br />
*/<br />
<span style="#0000bb;">$result </span><span style="#007700;">= </span><span style="#0000bb;">mysql_query</span><span style="#007700;">(</span><span style="#dd0000;">&#8220;SELECT * FROM &#8217;table&#8217; WHERE ID=&#8217;&#8221;</span><span style="#007700;">.</span><span style="#0000bb;">SQL_Input</span><span style="#007700;">(</span><span style="#0000bb;">$_POST</span><span style="#007700;">[</span><span style="#dd0000;">'ID'</span><span style="#007700;">], </span><span style="#0000bb;">TRUE</span><span style="#007700;">).</span><span style="#dd0000;">&#8220;&#8216; AND Text=&#8217;&#8221;</span><span style="#007700;">.</span><span style="#0000bb;">SQL_Input</span><span style="#007700;">(</span><span style="#0000bb;">$_POST</span><span style="#007700;">[</span><span style="#dd0000;">'Text'</span><span style="#007700;">], </span><span style="#0000bb;">FALSE</span><span style="#007700;">).</span><span style="#dd0000;">&#8220;&#8216;&#8221;</span><span style="#007700;">);<br />
while (</span><span style="#0000bb;">$row </span><span style="#007700;">= </span><span style="#0000bb;">mysql_fetch_assoc</span><span style="#007700;">(</span><span style="#0000bb;">$result</span><span style="#007700;">)) {<br />
echo </span><span style="#dd0000;">&#8216;&lt;strong&gt;ID:&lt;/strong&gt;&#8217;</span><span style="#007700;">.</span><span style="#0000bb;">SQL_Output</span><span style="#007700;">(</span><span style="#0000bb;">$row</span><span style="#007700;">[</span><span style="#dd0000;">'ID'</span><span style="#007700;">], </span><span style="#0000bb;">TRUE</span><span style="#007700;">).</span><span style="#dd0000;">&#8220;\n&#8221;</span><span style="#007700;">;<br />
echo </span><span style="#dd0000;">&#8216;&lt;strong&gt;Text:&lt;/strong&gt;&#8217;</span><span style="#007700;">.</span><span style="#0000bb;">SQL_Output</span><span style="#007700;">(</span><span style="#0000bb;">$row</span><span style="#007700;">[</span><span style="#dd0000;">'Text'</span><span style="#007700;">], </span><span style="#0000bb;">TRUE</span><span style="#007700;">).</span><span style="#dd0000;">&#8220;\n&#8221;</span><span style="#007700;">;<br />
}<br />
</span><span style="#0000bb;">?&gt;</span></p></blockquote>
<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%2F09%2F24%2Fprotecting-yourself-from-mysql-injections%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/09/24/protecting-yourself-from-mysql-injections/"></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/09/24/protecting-yourself-from-mysql-injections/"  data-text="Protecting yourself from MySQL Injections" 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/09/24/protecting-yourself-from-mysql-injections/" 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/09/24/protecting-yourself-from-mysql-injections/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div>]]></content:encoded>
			<wfw:commentRss>http://xdnet.co.uk/blog/2008/09/24/protecting-yourself-from-mysql-injections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

