<?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; php</title>
	<atom:link href="http://stevenclark.com.au/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://stevenclark.com.au</link>
	<description></description>
	<lastBuildDate>Tue, 22 May 2012 09:55:26 +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>Web Security and Ways to Fix It</title>
		<link>http://stevenclark.com.au/2010/01/15/web-security-and-ways-to-fix-it/</link>
		<comments>http://stevenclark.com.au/2010/01/15/web-security-and-ways-to-fix-it/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 21:31:15 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[business]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/?p=4817</guid>
		<description><![CDATA[One issue that almost always crops up in web development work is the appreciation for web security. It&#8217;s probably what defines a good team from a bad team along a continuum from ignoring the security specialist to having complete faith in their advice. The first take-away from Christian Heilmann&#8217;s article on Smashing Magazine titled Web [...]]]></description>
			<content:encoded><![CDATA[<p>One issue that almost always crops up in web development work is the appreciation for web security. It&#8217;s probably what defines a good team from a bad team along a continuum from ignoring the security specialist to having complete faith in their advice.</p>
<p>The first take-away from Christian Heilmann&#8217;s article on Smashing Magazine titled <a href="http://www.smashingmagazine.com/2010/01/14/web-security-primer-are-you-part-of-the-problem/">Web Security: Are You Part of the Problem?</a> is that you need to make sure at least one member of your team is up to speed. Everybody else needs to appreciate the importance of what that person tells them.</p>
<p>The second take-away is that no matter how much the world looks like a rosy cake of graphic design skills in our industry, it isn&#8217;t. Its in the marrying of good interface design, graphic design, business acumen and coding skills that make a good website. Unfortunately we&#8217;re in a world that tends to judge almost entirely on the superficial 6 seconds after the user arrives on a website &#8211; how does it look trumps is it secure? In other words, there is always pressure to make things look good but nobody pressures about writing better code until after you&#8217;re butt-shovelled by a Russian spamster or three.</p>
<p>And the third take-away from Christian in this article is to trust nothing &#8211; all data needs sanitising before you use it &#8211; and that URIs should be treated with similar mistrust. This is where the crappy web person is vastly different from the great web person if you&#8217;re out there hiring, employing or getting hold of a freelancer&#8230; the crappy person being the one who comes out with phrases like <em>but it works, doesn&#8217;t it</em>? How often have you heard that smidgen of cop-out?</p>
<p>Seriously, when it comes to your business and the web professional then you need to know up front before everybody&#8217;s credit card information is compromised that <em>the web solution more than works, it works effectively</em>. Securely.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2010/01/15/web-security-and-ways-to-fix-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>PHP Tip: Don&#8217;t use Echo to Code XHTML</title>
		<link>http://stevenclark.com.au/2008/08/21/php-tip-dont-use-echo-to-code-xhtml/</link>
		<comments>http://stevenclark.com.au/2008/08/21/php-tip-dont-use-echo-to-code-xhtml/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 09:41:42 +0000</pubDate>
		<dc:creator>steven</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://stevenclark.com.au/2008/08/21/php-tip-dont-use-echo-to-code-xhtml/</guid>
		<description><![CDATA[Here is a simple tip for anyone new to PHP (Hypertext Preprocessor) programming on the Web. When you go to output XHTML (or HTML, for that matter) don&#8217;t use the echo function. For example, the short-sighted way to do this would be: echo "&#60;p&#62;Your $variable1; makes me want to go straight home&#60;/p&#62;"; You should be [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple tip for anyone new to PHP (Hypertext Preprocessor) programming on the Web. When you go to output <acronym title="eXtensible Hypertext Markup Language">XHTML</acronym> (or <acronym title="Hypertext Markup Language">HTML</acronym>, for that matter) don&#8217;t use the echo function. For example, the short-sighted way to do this would be:</p>
<p><code>echo "&lt;p&gt;Your $variable1; makes me want to go straight home&lt;/p&gt;";</code></p>
<p>You should be able to see that varying amounts of complex XHTML, including backslashes throughout all the extra syntax of XHTML attributes, is going to rapidly become harder to maintain. Instead what you should be doing is closing your PHP block at an appropriate point and then outputting XHTML as it&#8217;s born and raised. If you&#8217;ve got a variable to output just insert a PHP block in there to call the variable:</p>
<p><code>&lt;p&gt;Your &lt;?php echo $variable1; ?&gt; makes me want to go home.&lt;/p&gt;</code></p>
<p>The main reason for doing this is because the XHTML rapidly becomes unmaintainable if you&#8217;ve got to deal with all of the extra syntactic debugging involved with PHP. So, don&#8217;t go there. Continually outputting XHTML as PHP echo statements is something I&#8217;d consider a novice&#8217;s habit, and one that should be cast aside at the earliest point.</p>
<p>A small tip but hopefully one that might save a little grief for someone, somewhere. In short, place PHP variables into XHTML and not the other way around.</p>
]]></content:encoded>
			<wfw:commentRss>http://stevenclark.com.au/2008/08/21/php-tip-dont-use-echo-to-code-xhtml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

