<?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>stevenclark.com.au &#187; sql</title>
	<atom:link href="http://stevenclark.com.au/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://stevenclark.com.au</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 06:58:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Prevent SQL Injection Attacks in PHP</title>
		<link>http://stevenclark.com.au/2008/08/24/prevent-sql-injection-attacks-in-php/</link>
		<comments>http://stevenclark.com.au/2008/08/24/prevent-sql-injection-attacks-in-php/#comments</comments>
		<pubDate>Sun, 24 Aug 2008 09:27:16 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/24/prevent-sql-injection-attacks-in-php/</guid>
		<description><![CDATA[Recently I posted about connecting to a MySQL database and provided a simple function that comes in handy &#8211; keep your username and password in one place. This advice is all relative to PHP (Hypertext Preprocessor) programming. But how secure is your database from user input? One of the biggest threats to your security will [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I posted about <a href="http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/">connecting to a MySQL database</a> and provided a simple function that comes in handy &#8211; keep your username and password in one place. This advice is all relative to PHP (Hypertext Preprocessor) programming. But how secure is your database from user input? One of the biggest threats to your security will be right at that front door where the rubber meets the road &#8211; username and password login.</p>
<p>The following <a href="http://www.youtube.com/watch?v=MJNJjh4jORY">YouTube video</a> (3 minutes) shows a straight forward MySQL injection. It should give you the idea. A read of Wikipedia&#8217;s <a href="http://en.wikipedia.org/wiki/SQL_injection">SQL injection resource</a> and Steve Friedl&#8217;s <a href="http://www.unixwiz.net/techtips/sql-injection.html">tips on SQL injection attacks</a> (particularly in Mitigations towards the bottom of the page) should take you a little further. It&#8217;s amazing how many web developers remain unaware of the problem &#8211; and I freely admit it only became something on my radar about 4 years ago when a client&#8217;s guestbook was hacked this way by script kiddies in the United States. Lesson two was to always keep open source software up-to-date.</p>
<p>The short answer for protecting yourself from this type of user manipulation of your database can involve a few reasonably simple steps. As a first measure, you might want to use <a href="http://au2.php.net/pcre">regular expressions</a> in your programming to ensure what you receive from the user is valid input. I do admit regular expressions might be a bit hardcore for some, but there are plenty of freely available snippets out there now you know what to look for.</p>
<p><span id="more-677"></span></p>
<p>You also need to use functions to <a href="http://au2.php.net/manual/en/function.addslashes.php">addslashes</a> and <a href="http://au2.php.net/manual/en/function.stripslashes.php">stripslashes</a> when you&#8217;re putting user generated data in and out of your database. You addslashes before you put user generated data into the database, and stripslashes when it comes back out. Although you might be better just using the newer <a href="http://www.php.net/manual/en/function.mysql-real-escape-string.php">mysql_real_escape_string</a> function, which I&#8217;m led to believe is a little more efficient. I should point out that adding and stripping slashes alone won&#8217;t protect you, nor will simply using regular expressions. You should use both strategies together.</p>
<p>I&#8217;d say those two measures are the hard and fast must-haves if you&#8217;re going to keep your database safe from this type of attack. But you can go a little further given a hardcore attitude and a wanton need to stop up even more holes in your defences.</p>
<p>Your queries might be run with only the necessary privileges from an account that does not have permissions to add new users, delete tables or do other significant modifications. This makes a lot of sense. Also, you might consider keeping your username and password in Apache rather than within your PHP code (although I don&#8217;t have a link to that one off-hand). Another thing to consider is error suppression in PHP &#8211; the last thing you want is for a hacker to generate an error that prints out on his screen explaining exactly what tripped up your defenses. Basically, never trust any input. The PHP Manual also has a good page in there on <a href="http://www.php.net/manual/en/security.database.sql-injection.php">SQL injection</a>.</p>
<p>Once you&#8217;re aware of this vulnerability you tend to just code a little differently and it stops being a real issue. This mostly happens when programmers are either unaware or apathetic about basic secure programming practices. I hope this has been of some help.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/24/prevent-sql-injection-attacks-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL ConnectToDatabase Function</title>
		<link>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/</link>
		<comments>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 00:49:41 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/</guid>
		<description><![CDATA[Carrying on from my last post about connecting to an SQL database using PHP, here are the functions I use to open and close database connections (Note: » just extends the line, you&#8217;ll need to remove that for this to work correctly). // to open the database function connectToDatabase () { $link = mysql_connect("localhost", "database_name", [...]]]></description>
			<content:encoded><![CDATA[<p>Carrying on from my last post about <a href="http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/">connecting to an <acronym title="Structured Query Language">SQL</acronym> database</a> using <acronym title="Hypertext Preprocessor">PHP</acronym>, here are the functions I use to open and close database connections (Note: » just extends the line, you&#8217;ll need to remove that for this to work correctly).</p>
<pre>
<code>// to open the database
function connectToDatabase ()	{
$link = mysql_connect("localhost", "database_name", »
                                  "database_password");
    if ($link &amp;&amp; mysql_select_db("database_name"));
        return ($link);
	    return (FALSE);
}</code>

<code>// to close the database
function db_close($connecter)  {
    mysql_close($connecter);
}</code></pre>
<p>It&#8217;s a good idea to keep these database connection functions in one place and then reuse them as you need, rather than repeating them everywhere.</p>
<p><span id="more-676"></span></p>
<p>Maintainability of your code is important even on smaller websites, if you only need to change a password in one place you&#8217;ll find life a lot easier. This code is very close to that found in PHP docs for <a href="http://au2.php.net/mysql_connect">mysql_connect</a>, by the way. The connectToDatabase function returns a link identifier on success or FALSE on failure.</p>
<p>Further information in the mysql_connect documentation should take you an extra step as well.</p>
<p>So, in your PHP code where you want to run your SQL query all you need to do is something like the following snippet:</p>
<pre>
<code>
// connect to the database
include("down/in_some/folder/db_function_file.php");
$link = connectToDatabase();
    if(!link) {
        print "&lt;p&gt;database connection error&lt;/p&gt;";
        mysql_close($link);
	exit();
}
</code></pre>
<p>Now run your query and put the values into variables, then explicitly close the link, and you&#8217;re done. Hopefully this snippet of advice might be useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/23/sql-connecttodatabase-function/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL Tip: Open Connection, Query, Close</title>
		<link>http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/</link>
		<comments>http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 00:44:29 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/</guid>
		<description><![CDATA[A very simple tip to novice backend PHP (Hypertext Preprocessor) programmers is to understand that database connections have an overhead &#8211; a database connection should be opened, the tables queried with SQL (Structured Query Language), and then the connection is closed. Generally I keep the database connection in two functions within a discrete file that [...]]]></description>
			<content:encoded><![CDATA[<p>A very simple tip to novice backend PHP (Hypertext Preprocessor) programmers is to understand that database connections have an overhead &#8211; a database connection should be opened, the tables queried with SQL (Structured Query Language), and then the connection is closed. Generally I keep the database connection in two functions within a discrete file that is re-usable throughout the website. Open, run my queries, and then close the connection. It&#8217;s hardly rocket science.</p>
<p>One novice mistake is to forget, or decide not to, close database connections. And another is to keep opening new database connections for every query.</p>
<p>A more seasoned approach to running queries is to open the door once, grab what you really need, and close it again as you leave. If you&#8217;re using those variables on another page then store them for ongoing use in a <a href="http://au.php.net/session">session</a>, a <a href="http://au.php.net/setcookie">cookie</a> or pass them as a <a href="http://au.php.net/urlencode"><acronym title="Universal Resource Locator">URL</acronym> encoded</a> string in name / value pairs.</p>
<p>So my small tip for the day is that if you&#8217;re going to run SQL queries in your PHP keep the process as simple and mechanical as possible. Open a connection, query the database, and close the connection on your way out.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/22/sql-tip-open-connection-query-close/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL for Comments and Pings in WordPress</title>
		<link>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/</link>
		<comments>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 00:44:41 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/</guid>
		<description><![CDATA[Just a snippet of SQL (Structured Query Language) that comes in handy for WordPress. If you have phpMyAdmin on your account you can just cut and paste these two queries into the SQL area and you&#8217;re away. What do they do? If you&#8217;re being hammered by spam you don&#8217;t want to have to revisit every [...]]]></description>
			<content:encoded><![CDATA[<p>Just a snippet of SQL (Structured Query Language) that comes in handy for <a href="http://wordpress.org">WordPress</a>. If you have <a href="http://www.phpmyadmin.net/">phpMyAdmin</a> on your account you can just cut and paste these two queries into the SQL area and you&#8217;re away. What do they do? If you&#8217;re being hammered by spam you don&#8217;t want to have to revisit every article and shut off comments and pings &#8211; in the past I&#8217;ve found the WordPress administration area gave unreliable results at times when turning them on and off. This method offers fine grained control.</p>
<p>So the first query just closes all comments and pings on every article. The second opens comments and pings on twenty articles &#8211; change the number on the end to suit your needs. So to close them:</p>
<p><code>update wp_posts set comment_status='closed', ping_status='closed' where comment_status='open' or ping_status='open'</code></p>
<p>and to reopen them:</p>
<p><code>update wp_posts set comment_status='open', ping_status='open' where comment_status='closed' or ping_status='closed' order by wp_posts.ID desc limit 20;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/17/sql-for-comments-and-pings-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

