<?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>Fireproof Socks &#187; MySQL</title>
	<atom:link href="http://www.fireproofsocks.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fireproofsocks.com</link>
	<description>Audio, Computers, and Stuff...</description>
	<lastBuildDate>Sun, 10 Aug 2008 07:47:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Preparing MySQL statements in PHP 5</title>
		<link>http://www.fireproofsocks.com/php/preparing-mysql-statements-in-php-5/</link>
		<comments>http://www.fireproofsocks.com/php/preparing-mysql-statements-in-php-5/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 07:47:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Mysqli]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.fireproofsocks.com/php/preparing-mysql-statements-in-php-5/</guid>
		<description><![CDATA[This revolves around the PHP mysqli module (for PHP 5).  Yeah, I know, it&#8217;s in the manual, but the examples aren&#8217;t straightforward enough, so here&#8217;s a couple of my own.  
The idea here is SECURE database interaction.  Follow these steps:
1.  Create a database user with ONLY the permissions that you need [...]]]></description>
			<content:encoded><![CDATA[<p>This revolves around the PHP <a href="http://www.php.net/manual/en/book.mysqli.php">mysqli</a> module (for PHP 5).  Yeah, I know, it&#8217;s in the manual, but the examples aren&#8217;t straightforward enough, so here&#8217;s a couple of my own.  </p>
<p>The idea here is SECURE database interaction.  Follow these steps:</p>
<p>1.  Create a database user with ONLY the permissions that you need for a given function. E.g. if you have a page that only needs to DISPLAY information, use a database user that only has SELECT privileges. </p>
<p>2.  Create your database columns so they can ONLY store the types of information they need.  E.g. if the field is for a phone number area code, make the column an INTEGER, not a CHAR, VARCHAR or TEXT field.  Nothing bad can end up in an integer column&#8230; but if you have a wide open data-type, you could be asking for trouble.</p>
<p>3.  Put your database connect functions ABOVE the html root directory.  E.g. most web sites should utilize a directory structure with something like the following:<br />
/html (contains all your html pages&#8230; this is the document root of the web site).<br />
/lib (contains your database connect function and other PHP code).</p>
<p>4.  Use regular expressions to sanitize any user input to a form.  Javascript enhances the user experience, but it cannot stop someone from posting data directly to your submission page.  E.g. you might have something like this to get only alpha-numeric input:</p>
<p><code>function get_alphanumeric_regex ($input) {<br />
    $pattern = '/\W/';<br />
    $input = preg_replace($pattern, ' ', $input);<br />
    return $input;<br />
}<br />
</code></p>
<p>5.  Finally, use prepared statements&#8230; they are MUCH safer than piecing together statements as strings.  If someone hijacks your string, then they&#8217;ve hijacked your statement.  A prepared statement can&#8217;t be messed with.  With mysqli, they look something like this:</p>
<p><code>< ?php</p>
<p>/*-------------------------------------------------------------------------*/<br />
define("DATABASE_HOST", 'localhost');<br />
define("DATABASE", 'my_database');</p>
<p>function connect_db ($handle) {<br />
        switch($handle) {<br />
        case 'form_insert':<br />
                $link = new mysqli(DATABASE_HOST, $handle, "some_p@ssword", DATABASE);<br />
                break;<br />
        case 'form_update':<br />
                $link = new mysqli(DATABASE_HOST, $handle, "some_oth3r_p@ssword", DATABASE);<br />
                break;<br />
        case 'form_select':<br />
                $link = new mysqli(DATABASE_HOST, $handle, "ke3p_gues$ing!", DATABASE);<br />
                break;<br />
        case 'form_delete':<br />
                $link = new mysqli(DATABASE_HOST, $handle, "still-KE3p-Gu3ssing", DATABASE);<br />
                break;<br />
        }</p>
<p>        return $link;</p>
<p>}</p>
<p>/*-------------------------------------------------------------------------*/<br />
function insert_something ($input) {<br />
/*<br />
INPUT:<br />
        Hash with values from form, e.g. $input['first_name'].<br />
OUTPUT:<br />
        id from database if successful insert; otherwise null.<br />
*/<br />
        $link = connect_db('form_insert');<br />
        /* check connection */<br />
        if (mysqli_connect_errno()) {<br />
                        printf("Connect failed: %s\n", mysqli_connect_error());<br />
                        exit();<br />
        }</p>
<p>        $sql = "INSERT INTO some_table<br />
        (<br />
        first_name,<br />
        last_name,<br />
        datestamp<br />
        ) VALUES (<br />
        ?,<br />
        ?,<br />
        NOW()<br />
        )";</p>
<p>        $statement = $link->prepare($sql);</p>
<p>        if (!$statement) {<br />
                printf("Error - SQLSTATE %s.\n", mysqli_sqlstate($db_connection));<br />
                exit();<br />
        };</p>
<p>        $first_name             = get_name_regex($input['first_name']);<br />
        $last_name              = get_name_regex($input['last_name']);</p>
<p>        $statement->bind_param("ss", $first_name, $last_name);</p>
<p>        $statement->execute();</p>
<p>        $link->close();</p>
<p>        return $result;<br />
}</p>
<p>/*-------------------------------------------------------------------------*/<br />
function get_name_regex ($input) {<br />
    $pattern = '/(;|\||`|=|--|\/|\.|>|< |&#038;|^|"|'."\n|\r".'|{|}|[|]|\)|\(|[0-9])/i';<br />
    $input = preg_replace($pattern, ' ', $input);<br />
    return trim(ucfirst($input));<br />
}<br />
?></code></p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.fireproofsocks.com%2Fphp%2Fpreparing-mysql-statements-in-php-5%2F';
  addthis_title  = 'Preparing+MySQL+statements+in+PHP+5';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.fireproofsocks.com/php/preparing-mysql-statements-in-php-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
