<?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>simon.net.nz &#187; mysql</title> <atom:link href="http://simon.net.nz/articles/category/mysql/feed/" rel="self" type="application/rss+xml" /><link>http://simon.net.nz</link> <description>Dr. Simon J. Greenhill&#039;s website</description> <lastBuildDate>Mon, 26 Mar 2012 23:59:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=</generator> <item><title>Protecting MySQL from SQL Injection Attacks with PHP.</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/</link> <comments>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/#comments</comments> <pubDate>Sun, 04 Jun 2006 12:58:07 +0000</pubDate> <dc:creator>Simon</dc:creator> <category><![CDATA[code]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/</guid> <description><![CDATA[This is intended as a brief guide to protecting your MySQL database from SQL injection attacks. Unfortunately, a large amount of the code that I've seen written by people on forums, and in countless crappy PHP tutorials lurking around on the net, and in the many websites that display the "magic breeding slashed-quote" show that many people just do not understand what's going on and how to protect themselves against SQL injection attacks.]]></description> <content:encoded><![CDATA[<p>This is intended as a brief guide to protecting your MySQL database from SQL injection attacks. Unfortunately, a large amount of the code that I've seen written by people on <a
href="http://forums.devshed.com/" title="Devshed Forums">forums</a>, and in countless crappy PHP tutorials lurking around on the net, and in the many websites that display the magic breeding slashed-quote (\' - <a
href="#mbsq" title="whats a Magic Breeding Slashed Quote?">see below</a>) show that many people just do not understand what's going on and how to protect themselves against SQL injection attacks.</p><p>In fact, the only reason that many websites are "protected" is due to magic quotes, and given that this is due to be disabled in the forthcoming PHP6, then there's going to be some major problems cropping up.</p><p>I'll talk about the problem of SQL injection, the half-hearted attempt to fix it with these "magic quotes", and what you should really be doing EVERY TIME you send user inputted data to your database.</p><h2> The Problem - What is SQL Injection:</h2><p>As the name suggests, SQL Injection is quite simply, when the user injects SQL into your application. How does this happen? Say we have a nice simple login form that takes a username and password, and checks if that's in the database. If it is, the user is logged into an admin section or something. The code for this could look something like this:</p><div
class="igBar"><span
id="lphp-1"><a
href="#" onclick="javascript:showPlainTxt('php-1'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-1"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// user and password come from a simple POST'ed form</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$user</span> = <span
style="color:#0000FF;">$_POST</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'user'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$password</span> = <span
style="color:#0000FF;">$_POST</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'password'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT name, age, credit_card</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">FROM usertable</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">WHERE username = '$user' AND password = '$password' "</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$result</span> = <a
href="http://www.php.net/mysql_query"><span
style="color:#000066;">mysql_query</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$query</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// check if mysql found anything, and get the record if it did</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/mysql_num_rows"><span
style="color:#000066;">mysql_num_rows</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$result</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>&amp;gt; <span
style="color:#CC66CC;color:#800000;">0</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$data</span> = <a
href="http://www.php.net/mysql_fetch_assoc"><span
style="color:#000066;">mysql_fetch_assoc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$result</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-weight: bold;color:#26536A;"></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Hello '</span>.<span
style="color:#0000FF;">$user</span>.<span
style="color:#FF0000;">'!</span></div></li><li
style="font-weight: bold;color:#26536A;"></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Your credit card number is '</span>.<span
style="color:#0000FF;">$data</span><span
style="color:#006600; font-weight:bold;">&#91;</span> <span
style="color:#FF0000;">'credit_card'</span> <span
style="color:#006600; font-weight:bold;">&#93;</span>.<span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">else</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">'</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">Incorrect Username or Password! Go Away!</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&amp;gt;</div></li></ol></div></div></div><p></p><p>Ok. This works, BUT it's about as safe as juggling with scalpels. If I enter "simon" as my username, and "secret" as my password, then the query that goes to MySQL looks like this:</p><div
class="igBar"><span
id="lphp-2"><a
href="#" onclick="javascript:showPlainTxt('php-2'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-2"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">'secret'</span></div></li></ol></div></div></div><p></p><p>and I get logged in quite happily. Fantastic.</p><p>The problem comes when I start entering other characters. Let's say that the next user who trys to login is Peter O'Reilly. Naturally he'll want a username something like PeterO'Reilly. If we plug that into our query we get this:</p><div
class="igBar"><span
id="lphp-3"><a
href="#" onclick="javascript:showPlainTxt('php-3'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-3"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'PeterO'</span>Reilly<span
style="color:#FF0000;">' AND password = '</span>secret<span
style="color:#FF0000;">' </span></div></li></ol></div></div></div><p></p><p>MySQL blasts along quite happily and hits username='PeterO', and then it gets this "Reilly" thing which it doesn't know what to do with and this happens:</p><div
class="igBar"><span
id="lphp-4"><a
href="#" onclick="javascript:showPlainTxt('php-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-4"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ERROR <span
style="color:#CC66CC;color:#800000;">1064</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#CC66CC;color:#800000;">42000</span><span
style="color:#006600; font-weight:bold;">&#41;</span>: You have an error in your SQL syntax; check the manual that corresponds to your <a
href="http://www.php.net/mysql"><span
style="color:#000066;">MySQL</span></a> server version <span
style="color:#616100;">for</span> the right syntax to use near <span
style="color:#FF0000;">'Reilly'</span> and password=<span
style="color:#FF0000;">"secret"</span>; <span
style="color:#FF0000;">''</span> at line <span
style="color:#CC66CC;color:#800000;">1</span></div></li></ol></div></div></div><p></p><p>Nice. We have a broken website, Pete can't login, and he's left with misgivings about our web programming skills.</p><p>Even worse - what happens if I enter my password as this?</p><div
class="igBar"><span
id="lphp-5"><a
href="#" onclick="javascript:showPlainTxt('php-5'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-5"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">' or 1=1 ; -- </span></div></li></ol></div></div></div><p></p><p>The query that gets sent to MySQL will look like this:</p><div
class="igBar"><span
id="lphp-6"><a
href="#" onclick="javascript:showPlainTxt('php-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-6"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">''</span> or <span
style="color:#CC66CC;color:#800000;">1</span>=<span
style="color:#CC66CC;color:#800000;">1</span> ; -- <span
style="color:#FF0000;">' </span></div></li></ol></div></div></div><p></p><p>What does this mean? It tells MySQL to find all rows with a username equal to "simon" and a password equal to an empty string OR "1=1". To represent that a bit more logically:</p><p>( username = "simon" and password = "" ) || ( 1 = 1 )</p><p>Now, 1=1 is always going to be true, so this is equal to:</p><p>( false ) || ( true )</p><p>Which means that ALL the records in the table will get returned. Our login processer above is going to log me on with someone else's credentials - in fact, those of the first record returned.</p><p>Keep in mind, however, that we don't need to escape numbers, and we shouldn't put quote marks around them (it's not standard SQL)- if a variable is a number, then it'll be fine.</p><h2> The crap attempt to fix it ( magic quotes ):</h2><p>How to fix this? We need to be escape these quote characters ( both single and double quotes, as well as backslashes). This is done by putting a slash in front of them, e.g. so a ' becomes a \', and MySQL can work out that that quote mark is "protected" by the slash, and is part of the value and ignores it. So, Peter's attempt to login becomes:</p><div
class="igBar"><span
id="lphp-7"><a
href="#" onclick="javascript:showPlainTxt('php-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-7"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'PeterO<span
style="color:#000099; font-weight:bold;">\'</span>Reilly'</span> AND password = <span
style="color:#FF0000;">'secret'</span>;</div></li></ol></div></div></div><p></p><p>and my attempt to hack my way in becomes:</p><div
class="igBar"><span
id="lphp-8"><a
href="#" onclick="javascript:showPlainTxt('php-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-8"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">SELECT name, age, credit_card FROM usertable WHERE username = <span
style="color:#FF0000;">'simon'</span> AND password = <span
style="color:#FF0000;">'<span
style="color:#000099; font-weight:bold;">\'</span> or 1=1 ; -- '</span>;</div></li></ol></div></div></div><p></p><p>MySQL now thinks my password is the string</p><div
class="igBar"><span
id="lphp-9"><a
href="#" onclick="javascript:showPlainTxt('php-9'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-9"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">' or 1=1 ; </span></div></li></ol></div></div></div><p></p><p>and I won't be able to login.</p><p>So where do we get these slashes? Since around PHP version 3.06, PHP tries to do this for you, with a setting called <a
href="http://www.php.net/magic_quotes" title="magic_quotes">"magic_quotes"</a> . What this does is to automatically add slashes to anything coming in via HTTP get or post requests and via cookies. You can also do this manually using the <em><a
href="http://www.php.net/addslashes" title="addslashes()">addslashes()</a></em> function.</p><h3>But do not rely on this!</h3><ul><li>It could be turned off or on, or not present in your version (and it won't be in PHP6). Therefore YOU CANNOT RELY ON IT, AND HAVE TO HANDLE THIS YOURSELF.</li><li> This means that if you rely on magic quotes, then your code is not portable. Unfortunately, escaping things with a slash is one of those irritating non-standard MySQL features. Most other databases which follow the SQL standards (like Postgres), escape things with another single quote ( O'Reilly =&gt; O''Reilly ).</li><li> It's crappy. It doesn't work well with extended characters, and these can be used to get around the slashes. <a
href="http://shiflett.org/archive/184" title="shiflett.org">See Chris Shiflett's discussion of this problem</a></li><li> It's irritating - it pollutes your data with stuff that the user didn't enter. This is the major cause of the magic breeding slashed-quote.</li></ul><h3> (Aside) The Magic Breeding Slashed-Quote:</h3><p><a
id="mbsq"></a></p><p>I'm sure you've all seen websites that have this really annoying habit of messing up their user\'s post\'s quote marks ( just like that ). I'm calling this the magic breeding slashed quote, because these things propagate like crazy. What's happening here is that the hard working web developer is adding slashes to the data they send to their database - great! BUT, they're not checking for magic quotes, so PHP is escaping the ' once to \', and then when the website runs addslashes() again, php sees a backslash AND a single quote which need escaping ( remember that the three characters that get escaped are \, ', and " ). This therefore becomes \\\'. MySQL comes along and sees an escaped backslash AND an escaped single quote.</p><p>Here's what's happening:</p><ol><li> user input: O'Reilly</li><li> magic quotes: O\'Reilly</li><li> addslashes: O\\\'Reilly</li><li> MySQL processes this to: O\'Reilly</li></ol><p>and we end up with O\'Reilly stored when we really want O'Reilly.</p><p>I've actually seen applications which quite happily store O\'Reilly, and <a
href="http://www.php.net/stripslashes" title="stripslashes()">stripslashes()</a> before they display the data - this is just blindingly stupid.</p><h2> Fixing it.</h2><p>So, we need a way of escaping data that isn't crappy, isn't as prone to character set issues, and isn't prone to magic breeding slashed quotes.</p><p>There are two ways to do this.</p><ul><li> Use better slashes ( PHP4, old mysql client library using the mysql_* functions )</li><li> Use a better technique - bound parameters ( PHP5 with the new mysqli_* client library)</li></ul><h2> Using better slashes - mysql_real_escape_string( ) (PHP4, mysql_* )</h2><p>If you're using the old mysql client library ( i.e. the <a
href="http://www.php.net/mysql" title="mysql_* functions">mysql_* functions</a> ), then you have to use the hideously named <a
href="http://www.php.net/mysql_real_escape_string" title="mysql_real_escape_string in the PHP manual">mysql_real_escape_string()</a> function. This takes into account the character set of the database connection and should handle things appropriately.</p><p><strong>Note:</strong> mysql_real_escape_string needs an active database connection, or anything sent to it will disappear ( WTF? ), or it will generate an error.</p><p>BUT we still need to check for the evil magic_quotes setting, which and remove it. We can do this with the <a
href="http://www.php.net/get_magic_quotes_gpc" title="get_magic_quotes_gpc on php.net">get_magic_quotes_gpc()</a> function ( "gpc" refers to Get, Post, and Cookies which magic quotes operates on ).</p><p>So - something like this:</p><div
class="igBar"><span
id="lphp-10"><a
href="#" onclick="javascript:showPlainTxt('php-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-10"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// remove the pesky slashes from magic quotes if it's turned on</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> clean_string<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span>, <span
style="color:#0000FF;">$DB</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/get_magic_quotes_gpc"><span
style="color:#000066;">get_magic_quotes_gpc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <a
href="http://www.php.net/stripslashes"><span
style="color:#000066;">stripslashes</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// escape things properly</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span>, <span
style="color:#0000FF;">$DB</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$string</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// where $db is your active database connection resource id.</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$safe_string</span> = clean_string<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$string</span>, <span
style="color:#0000FF;">$db</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>There's a function described in the <a
href="http://www.php.net/mysql_real_escape_string" title="quote_smart">PHP manual called quote_smart</a>, that handles this and handles both strings and integers:</p><div
class="igBar"><span
id="lphp-11"><a
href="#" onclick="javascript:showPlainTxt('php-11'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-11"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Quote variable to make safe</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> quote_smart<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$value</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Stripslashes</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/get_magic_quotes_gpc"><span
style="color:#000066;">get_magic_quotes_gpc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <a
href="http://www.php.net/stripslashes"><span
style="color:#000066;">stripslashes</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// Quote if not a number or a numeric string</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> !<a
href="http://www.php.net/is_numeric"><span
style="color:#000066;">is_numeric</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$value</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$value</span> = <span
style="color:#FF0000;">"'"</span> . <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$value</span><span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">"'"</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#0000FF;">$value</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><p>Note that you'll need to implement this yourself, and you'll have to rewrite your queries to not have quotes in them eg:</p><div
class="igBar"><span
id="lphp-12"><a
href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-12"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = quote_smart<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// note that we haven't surrounded $variable with quote marks in</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// the query below since quote_smart does that for us.</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT x, y, z FROM tablename WHERE user = $variable"</span>;</div></li></ol></div></div></div><p></p><p>However, this leaving quote marks out of the query irritates me enough, that I generally just type-cast anything which should be a number to a number:</p><div
class="igBar"><span
id="lphp-13"><a
href="#" onclick="javascript:showPlainTxt('php-13'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-13"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> clean_int<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$i</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/is_numeric"><span
style="color:#000066;">is_numeric</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$i</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> int <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#0000FF;">$i</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// return False if we don't get a number</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">else</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#616100;">return</span> <span
style="color:#000000; font-weight:bold;">False</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><h3>Warning:</h3><p>This is NOT foolproof. In fact, if the attacker can change the character set on the fly, then this whole system can be avoided. <a
href="http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html" title="mysql_real_escape_string versus Prepared Statements">Ilia Alshanetsky has an excellent write up on this</a>.</p><h2> Fixing it with better technique - bound parameters ( PHP5, MySQLi ):</h2><p>So - the best solution? Use bound parameters. To use these you'll need to be using the <a
href="http://www.php.net/mysqli" title="PHP Documentation for MySQLi"> improved <strong>mysqli</strong> </a>library that comes with PHP5. This technique differs slightly in that you define a query "template" first with placeholders, and then "bind" the parameters to it, and the mysqli library takes care of  the appropriate escaping for us:</p><div
class="igBar"><span
id="lphp-14"><a
href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-14"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$variable</span> = <span
style="color:#FF0000;">"O'Reilly"</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// prepare the query</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#0000FF;">$mysqli</span>-&amp;gt;prepare<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">"SELECT x, y, z FROM tablename WHERE user = ?"</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// bind a parameter - here the first parameter is a short string that specifies the type that the</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// subsequent arguments should be:</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 's' means a string</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'd' means a double</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'i' means an integer</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// 'b' is a blob</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;bind_param<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'s'</span>,&nbsp; <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// execute query:</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;execute<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// so if we had a more complex query, which updated the user info with</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// &quot;favorite_color&quot; (a string), &quot;age&quot; ( an integer ) and &quot;description&quot;, a blob:</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#0000FF;">$mysqli</span>-&amp;gt;prepare<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">"UPDATE tablename SET favorite_color = ?, age = ?, description = ? WHERE user = ?"</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// we would have a bind looking like this:</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;bind_param<span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#FF0000;">'sibs'</span>, <span
style="color:#FF0000;">'red'</span>, <span
style="color:#CC66CC;color:#800000;">27</span>, <span
style="color:#0000FF;">$some_blob</span>, <span
style="color:#0000FF;">$variable</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span>-&amp;gt;execute<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Another benefit of this method is that it's faster to transfer data to the db server. <a
href="http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html" title="MySQLs Prepared Statements">Harrison Fisk has a good discussion of these here</a>.</p><h2> Another thing to keep in mind:</h2><p>Now, properly using mysql_real_escape_string or prepared statements should keep you pretty safe, but there are a few characters you might also want to watch out for:</p><h3> The Percentage Sign (%)</h3><p>The percentage symbol is commonly used by MySQL to perform LIKE queries - this WON'T get escaped. If your application is doing LIKE comparisons, and your database is large, then it's worth checking for this specifically to avoid a friendly user entering "%" and making your database grind to a halt - e.g.</p><div
class="igBar"><span
id="lphp-15"><a
href="#" onclick="javascript:showPlainTxt('php-15'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-15"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$user_input</span> = <span
style="color:#FF0000;">'%'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$query</span> = <span
style="color:#FF0000;">"SELECT x,y,z FROM tablename WHERE user LIKE '%$user_input%';</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF0000;">// becomes LIKE %%% -&amp;gt; and returns all rows in tablename. </span></div></li></ol></div></div></div><p></p><h3>Edit: 15th August, 2006 -</h3><p>James Laver has written <a
href="http://www.sourcemill.org/2006-08-11/a-clean-php5-mysqli-wrapper/" title="A Clean PHP5 MySQLi Wrapper"> a nice lightweight database access class for MySQLi</a> which takes care of the binding of parameters for you.</p><h3>Edit: 9th November, 2006 -</h3><p>Fixing a link, <a
href="http://www.thecodergeek.com/blog/">thanks Peter <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a></p><p>--Simon</p> ]]></content:encoded> <wfw:commentRss>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/feed/</wfw:commentRss> <slash:comments>23</slash:comments> </item> </channel> </rss>
