<?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>Krio Media &#187; MySQL</title>
	<atom:link href="http://www.krio.me/category/development-info/database-development/mysql-php-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krio.me</link>
	<description></description>
	<lastBuildDate>Mon, 05 Jul 2010 06:27:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Loop twice through a PHP / MySQL result set</title>
		<link>http://www.krio.me/loop-twice-through-a-php-mysql-result-set/</link>
		<comments>http://www.krio.me/loop-twice-through-a-php-mysql-result-set/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 22:39:29 +0000</pubDate>
		<dc:creator>Kevin Rio</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.krio.me/?p=1125</guid>
		<description><![CDATA[I get asked all the time if it is possible to loop multiple times through a MySQL result set in PHP. Here&#8217;s the answer. First, you&#8217;ll need a MySQL result set. To do that you&#8217;ll need to select something from your database and loop through it like so: $result = mysql_query(&#34;SELECT * FROM my_table&#34;); while($row [...]]]></description>
			<content:encoded><![CDATA[<p>I get asked all the time if it is possible to loop multiple times through a MySQL result set in PHP. Here&#8217;s the answer.</p>
<p><span id="more-1125"></span></p>
<p>First, you&#8217;ll need a MySQL result set. To do that you&#8217;ll need to select something from your database and loop through it like so:</p>
<pre class="brush: php;">

$result = mysql_query(&quot;SELECT * FROM my_table&quot;);

while($row = mysql_fetch_assoc($result)) {

// inside the loop

}
</pre>
<p>The problem is, if you want to loop through the same result set again, you will get an error because the internal pointer is currently at the end of the result. You will need to put the pointer back at the beginning of the result set so that you can loop through it again.</p>
<p>You can do it with a helpful php function named mysql_data_seek.</p>
<pre class="brush: php;">

mysql_data_seek($result, 0); // set the pointer of the result set back to the beginning.

while($row2 = mysql_fetch_assoc($result)) {

// inside the loop

}
</pre>
<p>This demonstrates how, as a PHP website developer, you can reset the pointer of a MySQL result set, which will allow you to iterate through the result set for a second time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krio.me/loop-twice-through-a-php-mysql-result-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cleaning PHP Input Variables Before MySQL Insertion</title>
		<link>http://www.krio.me/cleaning-php-input-variables-before-mysql-insertion/</link>
		<comments>http://www.krio.me/cleaning-php-input-variables-before-mysql-insertion/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 03:23:14 +0000</pubDate>
		<dc:creator>Kevin Rio</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[MySQL Security]]></category>
		<category><![CDATA[PHP Security]]></category>

		<guid isPermaLink="false">http://kriomedia.com/?p=120</guid>
		<description><![CDATA[Ensuring that you properly sanitize your PHP input variables is an essential first-step in making your website secure. Unfortunately, thousands of websites and scripts on the internet do not properly filter variables, causing a very large security vulnerability in these websites. This article is designed to help you both understand why you need to filter [...]]]></description>
			<content:encoded><![CDATA[<p>Ensuring that you properly sanitize your PHP input variables is an essential first-step in making your website secure. Unfortunately, thousands of websites and scripts on the internet do not properly filter variables, causing a very large security vulnerability in these websites. <span id="more-120"></span>This article is designed to help you both understand why you need to filter your variables and how to filter your variables.</p>
<h1>Gaining Understanding</h1>
<p>This article will provide information related to securing your input variables to keep your site and database secure. This is not an entire run-down related to internet security. The first step in understanding how to secure your input variables, is to understand why they need to be secure. As a developer, you should realize that user input should never be trusted, no matter what the situation is. This, combined with an understanding of how PHP and MySQL work, will give you the tools necessary to secure all of your PHP scripts and forms.</p>
<p>Lets get started by creating a simple HTML form.</p>
<pre class="brush: xml;">

&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;

&lt;input type=&quot;text&quot; name=&quot;fName&quot; maxlength=&quot;20&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;text&quot; name=&quot;email&quot; maxlength=&quot;50&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;text&quot; name=&quot;age&quot; maxlength=&quot;3&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;submit&quot; value=&quot;addUser&quot; /&gt;&lt;br /&gt;

&lt;/form&gt;
</pre>
<p>Right off the bat you should notice that we are limited the number of characters that the user can enter in the input boxes. This is to ensure that the user does not enter a long string that would most likely be for the sole purpose of attempting an SQL Injection attack (look for a follow-up post related to this soon). However, we still need to check the length of values on the server-side with PHP to ensure that the attacker is not submitting a form from another machine.</p>
<h1>PHP Sanitization Functions</h1>
<p>Some essential built-in PHP functions that you need to know when getting started with PHP input sanitation are:</p>
<ul>
<li>mysql_real_escape_string (you need to have an active MySQL connection to use this)</li>
<li>strip_tags</li>
<li>htmlspecialchars</li>
<li>htmlentities</li>
<li>html_entity_decode</li>
<li>add_slashes</li>
<li>preg_match (once you gain an understanding of regular expressions.. tutorial coming soon)</li>
</ul>
<h1>Your Custom Functions</h1>
<p>When sanitizing variables, I find it best to create a specific function for each type of variable that I am sanitizing. For example, a function named sanitizeName will be created for the users&#8217; names and so on. Lets make some functions to sanitize each of our inputs. First we&#8217;ll start with the users age, then their name, and finally their email address.</p>
<pre class="brush: php;">

if (isset($_POST['name']))
{
 $name = sanitizeName($_POST['name']);

 if ($name == false)
 {
 echo 'Please input a valid name!';
 }
 else
 {
 // insert MySQL Query Here
 }
}

function sanitizeName($name)
{
 if (strlen($name) &gt; 50)
 {
 return false;
 }
 else if (empty($name))
 {
 return false;
 }
 else
 {
 $name = strip_tags($name);
 $name = mysql_real_escape_string($name);
 return $name;
 }

}
</pre>
<p>So first things first, the script checks to see if the POST was submitted. If so, it runs the variable through the sanitizeName function. The function first checks to see if the name is longer than 50 characters. If it is, it returns a false value, signifying it is not valid. Next, it check to make sure that the variable actually has data inside of it. If not, it also returns a false value. Finally, it actually sanitizes the input to ensure that it is safe to place into the database. The following functions work in the same way.</p>
<pre class="brush: php;">

if (isset($_POST['age']))
{
 $age = sanitizeAge($_POST['age']);
 function sanitizeAge($age)
 {
 $age = mysql_real_escape_string(intval(strip_tags($age)));

 return $age;
 }
}
</pre>
<p>This function is very similar to the previous one, however you might notice the intval() function, which ensures that the value being checked is turned into an integer, since an age should always be an integer. intval() is pretty good at ensuring that there are no unwanted values in the variable, the strip_tags and mysql_real_escape_string functions are just for good measure.</p>
<p>For validating email addresses there are numerous regular expressions available on the internet, however there is also a built-in PHP function that does a a pretty good job at this.</p>
<pre class="brush: php;">
if (isset($_POST['email']))
{
 $email = sanitizeEmail($_POST['email']);
 function sanitizeEmail($email)
 {
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
    return $email;
 }
}
</pre>
<p>The filter_validate_email function is criticized by some, but for 99% of sites, it will function perfectly. If you want a perfect solution, you should look into learning about how regular expressions work. I&#8217;m going to write a feature on them soon.</p>
<p>I hope this article has helped you learn some techniques and functions that you can use to filter your input variables. I hope you don&#8217;t just copy these functions and throw them into your scripts, but instead learn about how each PHP function can be best used and integrated into your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krio.me/cleaning-php-input-variables-before-mysql-insertion/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
