<?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: Clean input variable PHP</title>
	<atom:link href="http://ditio.net/2008/06/29/clean-input-variable-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://ditio.net/2008/06/29/clean-input-variable-php/</link>
	<description>Practical guide to WordPress development, blogging and programming</description>
	<lastBuildDate>Sun, 11 Dec 2011 21:19:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Viper-7</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-181530</link>
		<dc:creator>Viper-7</dc:creator>
		<pubDate>Fri, 09 Sep 2011 07:22:10 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-181530</guid>
		<description>&quot;Keep in mind that if you escape data before inserting it into database then you must unescape it when you want to get it out of database, a lot more effort then before, but security is worth it.&quot;

&quot;Remember when i said we need to unescape, slashes when getting data out of database? Here is how to do it:&quot;

Sorry, but both of those are quite wrong :&lt;

Escaping data is for the *TRANSPORT* between PHP, and your database - once your database has received that data, it will store it without the escaping slashes. When you SELECT it back out, the slashes won&#039;t be there. If you&#039;re seeing extra slashes in your data, it means you&#039;re escaping TWICE, which is not a good idea, and can actually open some rather nasty security HOLES.

What you&#039;re seeing is because Wordpress automatically escapes all inputs, just like magic_quotes_gpc does, and yes, it suffers from the same issues ( as detailed here http://php.net/magic_quotes ).

If you&#039;re using stripslashes at all, you should be using it BEFORE you use your database specific escaping function, like mysql_real_escape_string(). This way mysql_real_escape_string() is fed the actual content you want to store in the database, and it&#039;ll work correctly.

You shouldn&#039;t ever need stripslashes() when SELECT&#039;ing data out from your database.</description>
		<content:encoded><![CDATA[<p>&#8220;Keep in mind that if you escape data before inserting it into database then you must unescape it when you want to get it out of database, a lot more effort then before, but security is worth it.&#8221;</p>
<p>&#8220;Remember when i said we need to unescape, slashes when getting data out of database? Here is how to do it:&#8221;</p>
<p>Sorry, but both of those are quite wrong :&lt;</p>
<p>Escaping data is for the *TRANSPORT* between PHP, and your database &#8211; once your database has received that data, it will store it without the escaping slashes. When you SELECT it back out, the slashes won&#039;t be there. If you&#039;re seeing extra slashes in your data, it means you&#039;re escaping TWICE, which is not a good idea, and can actually open some rather nasty security HOLES.</p>
<p>What you&#039;re seeing is because WordPress automatically escapes all inputs, just like magic_quotes_gpc does, and yes, it suffers from the same issues ( as detailed here <a href="http://php.net/magic_quotes" rel="nofollow">http://php.net/magic_quotes</a> ).</p>
<p>If you&#039;re using stripslashes at all, you should be using it BEFORE you use your database specific escaping function, like mysql_real_escape_string(). This way mysql_real_escape_string() is fed the actual content you want to store in the database, and it&#039;ll work correctly.</p>
<p>You shouldn&#039;t ever need stripslashes() when SELECT&#039;ing data out from your database.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SMS</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-118650</link>
		<dc:creator>SMS</dc:creator>
		<pubDate>Wed, 27 Apr 2011 15:34:06 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-118650</guid>
		<description>I knew I needed to protect my form processor from the data submitted to it, but wasn&#039;t sure how to do it with PHP.  This helps a bunch, thanks!</description>
		<content:encoded><![CDATA[<p>I knew I needed to protect my form processor from the data submitted to it, but wasn&#8217;t sure how to do it with PHP.  This helps a bunch, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-116367</link>
		<dc:creator>Karl</dc:creator>
		<pubDate>Thu, 21 Apr 2011 08:13:38 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-116367</guid>
		<description>Hi Thanks been looking for a simple to understand and logical way to clean entries in to a database.
This article was very helpful is there any other tips about cleaning code or making it more secure before it get to a database , currently have dreamweaver code with your code slotted in to try and stop attacks
Any help would be much appreciated.
Code:
if (!function_exists(&quot;GetSQLValueString&quot;)) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = &quot;&quot;, $theNotDefinedValue = &quot;&quot;) 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists(&quot;mysql_real_escape_string&quot;) ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
$theValue = htmlentities($theValue, ENT_QUOTES, &#039;UTF-8&#039;);
trim($theValue);
  switch ($theType) {
    case &quot;text&quot;:
      $theValue = ($theValue != &quot;&quot;) ? &quot;&#039;&quot; . $theValue . &quot;&#039;&quot; : &quot;NULL&quot;;
      break;       case &quot;long&quot;:
    case &quot;int&quot;:
      $theValue = ($theValue != &quot;&quot;) ? intval($theValue) : &quot;NULL&quot;;
      break;    case &quot;double&quot;:
      $theValue = ($theValue != &quot;&quot;) ? &quot;&#039;&quot; . doubleval($theValue) . &quot;&#039;&quot; : &quot;NULL&quot;;
      break;    case &quot;date&quot;:
      $theValue = ($theValue != &quot;&quot;) ? &quot;&#039;&quot; . $theValue . &quot;&#039;&quot; : &quot;NULL&quot;;
      break;    case &quot;defined&quot;:
      $theValue = ($theValue != &quot;&quot;) ? $theDefinedValue : $theNotDefinedValue;
      break;  }  return $theValue;}}</description>
		<content:encoded><![CDATA[<p>Hi Thanks been looking for a simple to understand and logical way to clean entries in to a database.<br />
This article was very helpful is there any other tips about cleaning code or making it more secure before it get to a database , currently have dreamweaver code with your code slotted in to try and stop attacks<br />
Any help would be much appreciated.<br />
Code:<br />
if (!function_exists(&#8220;GetSQLValueString&#8221;)) {<br />
function GetSQLValueString($theValue, $theType, $theDefinedValue = &#8220;&#8221;, $theNotDefinedValue = &#8220;&#8221;)<br />
{<br />
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;</p>
<p>  $theValue = function_exists(&#8220;mysql_real_escape_string&#8221;) ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);<br />
$theValue = htmlentities($theValue, ENT_QUOTES, &#8216;UTF-8&#8242;);<br />
trim($theValue);<br />
  switch ($theType) {<br />
    case &#8220;text&#8221;:<br />
      $theValue = ($theValue != &#8220;&#8221;) ? &#8220;&#8216;&#8221; . $theValue . &#8220;&#8216;&#8221; : &#8220;NULL&#8221;;<br />
      break;       case &#8220;long&#8221;:<br />
    case &#8220;int&#8221;:<br />
      $theValue = ($theValue != &#8220;&#8221;) ? intval($theValue) : &#8220;NULL&#8221;;<br />
      break;    case &#8220;double&#8221;:<br />
      $theValue = ($theValue != &#8220;&#8221;) ? &#8220;&#8216;&#8221; . doubleval($theValue) . &#8220;&#8216;&#8221; : &#8220;NULL&#8221;;<br />
      break;    case &#8220;date&#8221;:<br />
      $theValue = ($theValue != &#8220;&#8221;) ? &#8220;&#8216;&#8221; . $theValue . &#8220;&#8216;&#8221; : &#8220;NULL&#8221;;<br />
      break;    case &#8220;defined&#8221;:<br />
      $theValue = ($theValue != &#8220;&#8221;) ? $theDefinedValue : $theNotDefinedValue;<br />
      break;  }  return $theValue;}}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jenifer</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-92251</link>
		<dc:creator>jenifer</dc:creator>
		<pubDate>Fri, 11 Feb 2011 16:18:06 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-92251</guid>
		<description>Nice read. I found your blog on bing and i have your page bookmarked on my favorite read list!
I’m a fan of your blog. Keep up the good work</description>
		<content:encoded><![CDATA[<p>Nice read. I found your blog on bing and i have your page bookmarked on my favorite read list!<br />
I’m a fan of your blog. Keep up the good work</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Plr Store</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-77476</link>
		<dc:creator>Plr Store</dc:creator>
		<pubDate>Thu, 16 Dec 2010 14:25:14 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-77476</guid>
		<description>Thanks, Charlie, your script is superb, I also added a stripslases and strip_tags in there for good measure</description>
		<content:encoded><![CDATA[<p>Thanks, Charlie, your script is superb, I also added a stripslases and strip_tags in there for good measure</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wilfred Sikel</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-38024</link>
		<dc:creator>Wilfred Sikel</dc:creator>
		<pubDate>Wed, 14 Apr 2010 08:01:13 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-38024</guid>
		<description>Superb Post. Niftier then the simillar post I checked 2 days ago on Wordpress. Maintain the good work.</description>
		<content:encoded><![CDATA[<p>Superb Post. Niftier then the simillar post I checked 2 days ago on WordPress. Maintain the good work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-32552</link>
		<dc:creator>Matt</dc:creator>
		<pubDate>Sun, 14 Feb 2010 07:01:53 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-32552</guid>
		<description>This is nice, however you&#039;re missing one important aspect... GET and POST arrays can have arrays as values, so this will fail to clean them (and may infact throw a PHP error as well).

Recursive functions come in handy here...
function __stripslashes($var)
{
	$var = is_array($var) ? array_map(&#039;__stripslashes&#039;, $var) : stripslashes($var);

	return $var;
}
function __htmlspecialchars($var, $style)
{
	$var = is_array($var) ? array_map(&#039;__htmlspecialchars&#039;, $var, array_fill(0, count($var), $style)) : htmlspecialchars($var, $style);

	return $var;
}
$_GET = __stripslashes($_GET);
$_GET = __htmlspecialchars($_GET, ENT_QUOTES);</description>
		<content:encoded><![CDATA[<p>This is nice, however you&#8217;re missing one important aspect&#8230; GET and POST arrays can have arrays as values, so this will fail to clean them (and may infact throw a PHP error as well).</p>
<p>Recursive functions come in handy here&#8230;<br />
function __stripslashes($var)<br />
{<br />
	$var = is_array($var) ? array_map(&#8216;__stripslashes&#8217;, $var) : stripslashes($var);</p>
<p>	return $var;<br />
}<br />
function __htmlspecialchars($var, $style)<br />
{<br />
	$var = is_array($var) ? array_map(&#8216;__htmlspecialchars&#8217;, $var, array_fill(0, count($var), $style)) : htmlspecialchars($var, $style);</p>
<p>	return $var;<br />
}<br />
$_GET = __stripslashes($_GET);<br />
$_GET = __htmlspecialchars($_GET, ENT_QUOTES);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dave</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-30663</link>
		<dc:creator>dave</dc:creator>
		<pubDate>Sat, 02 Jan 2010 21:32:32 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-30663</guid>
		<description>Jolly Joker.  it does work.  Thank you Charles.</description>
		<content:encoded><![CDATA[<p>Jolly Joker.  it does work.  Thank you Charles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jolly joker</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-30356</link>
		<dc:creator>jolly joker</dc:creator>
		<pubDate>Sat, 26 Dec 2009 02:52:41 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-30356</guid>
		<description>charlies solution is known to work, it never fails.</description>
		<content:encoded><![CDATA[<p>charlies solution is known to work, it never fails.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charlie</title>
		<link>http://ditio.net/2008/06/29/clean-input-variable-php/comment-page-1/#comment-26113</link>
		<dc:creator>Charlie</dc:creator>
		<pubDate>Thu, 17 Sep 2009 16:39:02 +0000</pubDate>
		<guid isPermaLink="false">http://ditio.net/?p=26#comment-26113</guid>
		<description>edit: the function is missing a &quot;return $data;&quot; line at the end.</description>
		<content:encoded><![CDATA[<p>edit: the function is missing a &#8220;return $data;&#8221; line at the end.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

