<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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/"
> <channel><title>Comments on: Protecting MySQL from SQL Injection Attacks with PHP.</title> <atom:link href="http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/feed/" rel="self" type="application/rss+xml" /><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/</link> <description>Dr. Simon J. Greenhill&#039;s website</description> <lastBuildDate>Wed, 27 Jan 2010 10:46:11 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=</generator> <item><title>By: Simon</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-25</link> <dc:creator>Simon</dc:creator> <pubDate>Sun, 22 Feb 2009 19:57:59 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-25</guid> <description>Eberhard  - yeah, this article is really old now! There are many better ways of doing things, like PDO.
--Simon</description> <content:encoded><![CDATA[<p>Eberhard  &#8211; yeah, this article is really old now! There are many better ways of doing things, like PDO.</p><p>&#8211;Simon</p> ]]></content:encoded> </item> <item><title>By: Eberhard Doerr</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-24</link> <dc:creator>Eberhard Doerr</dc:creator> <pubDate>Fri, 20 Feb 2009 15:47:16 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-24</guid> <description>In PHP5 the PDO library is recommended for DB access.
It contains a quote() method:
&quot;PDO::quote() places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.&quot;
http://de2.php.net/manual/en/pdo.quote.php
So this makes your code independent of the specific database, like when you want to switch from MySQL to PostgreSQL (which would be wise IMO ;-)
There is also a PDO::prepare() method that might be considered in this context.</description> <content:encoded><![CDATA[<p>In PHP5 the PDO library is recommended for DB access.<br
/> It contains a quote() method:<br
/> &#8220;PDO::quote() places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.&#8221;<br
/> <a
href="http://de2.php.net/manual/en/pdo.quote.php" rel="nofollow">http://de2.php.net/manual/en/pdo.quote.php</a></p><p>So this makes your code independent of the specific database, like when you want to switch from MySQL to PostgreSQL (which would be wise IMO <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p><p>There is also a PDO::prepare() method that might be considered in this context.</p> ]]></content:encoded> </item> <item><title>By: jalone</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-23</link> <dc:creator>jalone</dc:creator> <pubDate>Mon, 01 Dec 2008 00:28:26 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-23</guid> <description>Thanks!at last i understand evrything well!  there&#039;s so much confusion about this argumentù
Lorenzo</description> <content:encoded><![CDATA[<p>Thanks!at last i understand evrything well!  there&#8217;s so much confusion about this argumentù<br
/> Lorenzo</p> ]]></content:encoded> </item> <item><title>By: Simon</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-22</link> <dc:creator>Simon</dc:creator> <pubDate>Sat, 25 Aug 2007 01:51:01 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-22</guid> <description>Thanks Tux,
I agree with you there (which is why I have that &lt;i&gt;clean_int&lt;/i&gt; function described above - I also have an uncomfortable feeling about relying on &lt;i&gt;quote_smart&lt;/i&gt; to do the quoting of string fields for me).
The programmer MUST know what the value is before s/he sends it off to the database, and what s/he&#039;s trying to store. As a programmer, it&#039;s a really good idea to think &lt;em&gt;very&lt;/em&gt; carefully about what data you are likely to get entered, and what you&#039;ll do if it&#039;s &lt;strong&gt;not&lt;/strong&gt; alright, and most importantly, how can you deal with this situation gracefully.
--Simon</description> <content:encoded><![CDATA[<p>Thanks Tux,</p><p>I agree with you there (which is why I have that <i>clean_int</i> function described above &#8211; I also have an uncomfortable feeling about relying on <i>quote_smart</i> to do the quoting of string fields for me).</p><p>The programmer MUST know what the value is before s/he sends it off to the database, and what s/he&#8217;s trying to store. As a programmer, it&#8217;s a really good idea to think <em>very</em> carefully about what data you are likely to get entered, and what you&#8217;ll do if it&#8217;s <strong>not</strong> alright, and most importantly, how can you deal with this situation gracefully.</p><p>&#8211;Simon</p> ]]></content:encoded> </item> <item><title>By: TuxLives</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-21</link> <dc:creator>TuxLives</dc:creator> <pubDate>Fri, 24 Aug 2007 15:10:53 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-21</guid> <description>My only issue lies here: quote_smart($value); The programmer should know what kind of data (and key) is expected so there really is no reason to throw in another evaluation into the mix (IE: &quot;guessing about the data&quot;).
I would instead have one to clean strings (clean_string( $value, $DB );)  and a second one that handles integer based data. Use them when and where you need them.
Programmers should be careful and precise.
But, we need more articles that are thorough in the examples, well done.</description> <content:encoded><![CDATA[<p>My only issue lies here: quote_smart($value); The programmer should know what kind of data (and key) is expected so there really is no reason to throw in another evaluation into the mix (IE: &#8220;guessing about the data&#8221;).</p><p>I would instead have one to clean strings (clean_string( $value, $DB );)  and a second one that handles integer based data. Use them when and where you need them.</p><p>Programmers should be careful and precise.</p><p>But, we need more articles that are thorough in the examples, well done.</p> ]]></content:encoded> </item> <item><title>By: Simon</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-20</link> <dc:creator>Simon</dc:creator> <pubDate>Fri, 20 Apr 2007 05:49:30 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-20</guid> <description>Mmm... I must have broken things when I customised wordpress - I&#039;ll look into fixing it when I get some time over the next few days :)
--Simon</description> <content:encoded><![CDATA[<p>Mmm&#8230; I must have broken things when I customised wordpress &#8211; I&#8217;ll look into fixing it when I get some time over the next few days <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>&#8211;Simon</p> ]]></content:encoded> </item> <item><title>By: Jerome Doby</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-19</link> <dc:creator>Jerome Doby</dc:creator> <pubDate>Mon, 16 Apr 2007 16:43:58 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-19</guid> <description>Wow, your site script really buggers things up when people try to post info relating to code :)</description> <content:encoded><![CDATA[<p>Wow, your site script really buggers things up when people try to post info relating to code <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Jerome Doby</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-18</link> <dc:creator>Jerome Doby</dc:creator> <pubDate>Mon, 16 Apr 2007 16:39:46 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-18</guid> <description>I do it because I don&#039;t need users posting stuff in the database that they shouldn&#039;t be. If you don&#039;t want the htmlEntities, you can easily remove that bit from the code and the script is just as effective ;)
The htmlEntities strips all of the html content from the post and turns it into SAFE html codes. Users can try and post  if they want, but when sent to the database, it is converted to &lt;font color &amp;quote;blah&amp;quote;&amp;gt and when printed from a database it would show as if they were simply  characters instead of them trying to become html tags.
I find this a very effective way to eliminate attacks such as javascript or php hacks which would have to use</description> <content:encoded><![CDATA[<p>I do it because I don&#8217;t need users posting stuff in the database that they shouldn&#8217;t be. If you don&#8217;t want the htmlEntities, you can easily remove that bit from the code and the script is just as effective <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>The htmlEntities strips all of the html content from the post and turns it into SAFE html codes. Users can try and post  if they want, but when sent to the database, it is converted to &lt;font color &quote;blah&quote;&amp;gt and when printed from a database it would show as if they were simply  characters instead of them trying to become html tags.</p><p>I find this a very effective way to eliminate attacks such as javascript or php hacks which would have to use</p> ]]></content:encoded> </item> <item><title>By: Simon</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-17</link> <dc:creator>Simon</dc:creator> <pubDate>Mon, 16 Apr 2007 13:08:03 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-17</guid> <description>Here&#039;s Jerome&#039;s code:
[php]
//You must already have an open mysql connection
//for this to work
mysql_connect(&quot;localhost&quot;,&quot;user&quot;,&quot;pass&quot;);
mysql_select_db(&quot;database&quot;);
// Quote variable to make safe
function quote_smart($value){
// Stripslashes
if ( get_magic_quotes_gpc() ){
$value = stripslashes( $value );
}
// Quote if not a number or a numeric string
/* I edited this area because I didn&#039;t want this
function to automatically place single quotes for
me since I have done a lot of queries and I would have
to re do them all.
Replace with:
if(!is_numeric($value){
$value = &quot;&#039;&quot;.mysql_real_escape_string($value).&quot;&#039;&quot;;
}
if you prefer it.
*/
$value = mysql_real_escape_string($value);
return $value;
}
//Capture all of the post variables, and run their values
//through the function. I pased it through htmlentities too
//To stop people from posting html into you db. Remove if you wish.
if ($_POST) {
foreach (array_keys($_POST) as $var) {
${$var} = htmlentities(quote_smart(${$var}));
}
}
//Capture all of the post variables, and run their values
//through the function. I pased it through htmlentities too
//To stop people from posting html into you db. Remove if you wish.
if ($_GET) {
foreach (array_keys($_GET) as $var) {
${$var} = htmlentities(quote_smart(${$var}));
}
}
//Say you posted $inputField = &quot;G&#039;day folks!&quot;
//it will automatically come out &quot;G\&#039;day folks!&quot;
//No need to update any of your queries!
//Just include this file in a header on all
//the pages you want to protect :)
//Enjoy.
[/php]
Personally I wouldn&#039;t store html-entity&#039;ed data in the database, but have that as a last action before it&#039;s displayed to the user (what if you don&#039;t want html? what if there are other routes into your database?). But, your mileage may vary :)
--Simon</description> <content:encoded><![CDATA[<p>Here&#8217;s Jerome&#8217;s code:</p><p>[php]<br
/> //You must already have an open mysql connection<br
/> //for this to work</p><p>mysql_connect(&#8220;localhost&#8221;,&#8221;user&#8221;,&#8221;pass&#8221;);<br
/> mysql_select_db(&#8220;database&#8221;);</p><p>// Quote variable to make safe<br
/> function quote_smart($value){<br
/> // Stripslashes<br
/> if ( get_magic_quotes_gpc() ){<br
/> $value = stripslashes( $value );<br
/> }<br
/> // Quote if not a number or a numeric string</p><p> /* I edited this area because I didn&#8217;t want this<br
/> function to automatically place single quotes for<br
/> me since I have done a lot of queries and I would have<br
/> to re do them all.<br
/> Replace with:<br
/> if(!is_numeric($value){<br
/> $value = &#8220;&#8216;&#8221;.mysql_real_escape_string($value).&#8221;&#8216;&#8221;;<br
/> }<br
/> if you prefer it.<br
/> */<br
/> $value = mysql_real_escape_string($value);<br
/> return $value;<br
/> }</p><p>//Capture all of the post variables, and run their values<br
/> //through the function. I pased it through htmlentities too<br
/> //To stop people from posting html into you db. Remove if you wish.<br
/> if ($_POST) {<br
/> foreach (array_keys($_POST) as $var) {<br
/> ${$var} = htmlentities(quote_smart(${$var}));<br
/> }<br
/> }<br
/> //Capture all of the post variables, and run their values<br
/> //through the function. I pased it through htmlentities too<br
/> //To stop people from posting html into you db. Remove if you wish.<br
/> if ($_GET) {<br
/> foreach (array_keys($_GET) as $var) {<br
/> ${$var} = htmlentities(quote_smart(${$var}));<br
/> }<br
/> }<br
/> //Say you posted $inputField = &#8220;G&#8217;day folks!&#8221;<br
/> //it will automatically come out &#8220;G\&#8217;day folks!&#8221;<br
/> //No need to update any of your queries!<br
/> //Just include this file in a header on all<br
/> //the pages you want to protect <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br
/> //Enjoy.<br
/> [/php]</p><p>Personally I wouldn&#8217;t store html-entity&#8217;ed data in the database, but have that as a last action before it&#8217;s displayed to the user (what if you don&#8217;t want html? what if there are other routes into your database?). But, your mileage may vary <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>&#8211;Simon</p> ]]></content:encoded> </item> <item><title>By: Jerome Doby</title><link>http://simon.net.nz/articles/protecting-mysql-sql-injection-attacks-using-php/comment-page-1/#comment-16</link> <dc:creator>Jerome Doby</dc:creator> <pubDate>Sun, 15 Apr 2007 20:11:58 +0000</pubDate> <guid
isPermaLink="false">http://simon.net.nz/php/protecting_mysql_from_sql_injection_attacks_in_php/#comment-16</guid> <description>Is there a special way to post code here? After I sent the thins, I went to php.net and posted. Don&#039;t know if it went through. I&#039;ll post a link to the file in text instead :)
http://olar.getkilled.net/test2.txt
If I knew how your system here worked I would re post :) Perhaps you could do it for me on your site if my script seems sound :)</description> <content:encoded><![CDATA[<p>Is there a special way to post code here? After I sent the thins, I went to php.net and posted. Don&#8217;t know if it went through. I&#8217;ll post a link to the file in text instead <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p><a
href="http://olar.getkilled.net/test2.txt" rel="nofollow">http://olar.getkilled.net/test2.txt</a></p><p>If I knew how your system here worked I would re post <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Perhaps you could do it for me on your site if my script seems sound <img
src='http://simon.net.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> </channel> </rss>
