<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Making Safe Variables' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Making Safe Variables' posted on the 'PHP' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Thu, 09 Feb 2012 08:53:06 -0800</pubDate>
    <lastBuildDate>Thu, 09 Feb 2012 08:53:06 -0800</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Making Safe Variables</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/409738/409738/making-safe-variables/</link>
      <description>Hey all;&lt;br /&gt;
&lt;br /&gt;
Just a small PHP function I came up with today.&lt;br /&gt;
&lt;br /&gt;
I'm writing a rather involved PHP site that deals with a lot of checks against a MySQL database, dumping data to a browser, or dealing with form information.  I'm going in and out of the PHP code to draw the site.  I was getting annoyed with having to write out mysql_real_escape_string($VariableName) or htmlspecialchars($VariableName) whenever I wanted to deal with some data, especially when having several checks against the SQL statement, so I came up with a small function that'll take some data and convert it to a type of output I want.  It makes things a LITTLE more neater in my oppinion if used properly.&lt;br /&gt;
&lt;br /&gt;
You could use this at the beginning of your PHP script to get the $_GET/$_POST data properly formatted for wherever you're going to send the data out to.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
function MakeSafe($Unsafe,$OriginalName="") {
  if ($OriginalName!="") $NewVar[$OriginalName]=$Unsafe;
  $NewVar["MySQL"]=mysql_real_escape_string($Unsafe)
;
  $NewVar["HTML"]=htmlspecialchars($Unsafe);
  $NewVar["URLEncode"]=urlencode($Unsafe);
  $NewVar["URLDecode"]=urldecode($Unsafe);
  return $NewVar;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
When I want to use a variable in multiple places:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
&amp;lt;?
// Generate the variable lists
$OutputVar=MakeSafe($UnsafeVarible);

echo $OutputVar["HTML"];
$Sql="select * from tbl_example where ExField='".$OutputVar["MySQL"]."'";
?&amp;gt;

&amp;lt;a href='&amp;lt;?echo $OutputVar["URLEncode"]?&amp;gt;'&amp;gt;Linky Linky&amp;lt;/a&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
To make better sense of where this would come in useful, lets say we're adding a user to a database with the country they were born in:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
&amp;lt;?
// Assume we pulled this from a database query:
$OriginalUserName="Donnald O'Brian &amp;lt;CAN&amp;gt;";

// No more assumptions
$UserName=MakeSafe($OriginalUserName);
$SqlToAddUser="insert into tbl_users Name='".$UserName["MySQL"]."'";
echo "&amp;lt;a href='viewuser.php?UN=".$UserName["URLEncode"]."'&amp;gt;
".$UserName["HTML"]."&amp;lt;/a&amp;gt;";
?&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Just thought I'd share.  Makes things a LITTLE bit easier.&lt;br /&gt;
&lt;br /&gt;
PS: Not sure if its just the preview or what, but those code blocks don't seem to work right. :/</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/409738/409738/making-safe-variables/</guid>
      <pubDate>Sun, 22 Nov 2009 13:36:19 -0800</pubDate>
      <category>PHP</category>
    </item>
    <item>
      <title>Re: Making Safe Variables</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/409738/409914/re-making-safe-variables/#409914</link>
      <description>: Hey all;&lt;br /&gt;
: &lt;br /&gt;
: Just a small PHP function I came up with today.&lt;br /&gt;
: &lt;br /&gt;
: I'm writing a rather involved PHP site that deals with a lot of &lt;br /&gt;
: checks against a MySQL database, dumping data to a browser, or &lt;br /&gt;
: dealing with form information.  I'm going in and out of the PHP code &lt;br /&gt;
: to draw the site.  I was getting annoyed with having to write out &lt;br /&gt;
: mysql_real_escape_string($VariableName) or &lt;br /&gt;
: htmlspecialchars($VariableName) whenever I wanted to deal with some &lt;br /&gt;
: data, especially when having several checks against the SQL &lt;br /&gt;
: statement, so I came up with a small function that'll take some data &lt;br /&gt;
: and convert it to a type of output I want.  It makes things a LITTLE &lt;br /&gt;
: more neater in my oppinion if used properly.&lt;br /&gt;
: &lt;br /&gt;
: You could use this at the beginning of your PHP script to get the &lt;br /&gt;
: $_GET/$_POST data properly formatted for wherever you're going to &lt;br /&gt;
: send the data out to.&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 
: function MakeSafe($Unsafe,$OriginalName="") {
:   if ($OriginalName!="") $NewVar[$OriginalName]=$Unsafe;
:   $NewVar["MySQL"]=mysql_real_escape_string($Unsafe)
;
:   $NewVar["HTML"]=htmlspecialchars($Unsafe);
:   $NewVar["URLEncode"]=urlencode($Unsafe);
:   $NewVar["URLDecode"]=urldecode($Unsafe);
:   return $NewVar;
: }
: &lt;/pre&gt;: &lt;br /&gt;
: &lt;br /&gt;
: When I want to use a variable in multiple places:&lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 
: &amp;lt;?
: // Generate the variable lists
: $OutputVar=MakeSafe($UnsafeVarible);
: 
: echo $OutputVar["HTML"];
: $Sql="select * from tbl_example where ExField='".$OutputVar["MySQL"]."'";
: ?&amp;gt;
: 
: &amp;lt;a href='&amp;lt;?echo $OutputVar["URLEncode"]?&amp;gt;'&amp;gt;Linky Linky&amp;lt;/a&amp;gt;
: &lt;/pre&gt;: &lt;br /&gt;
: &lt;br /&gt;
: To make better sense of where this would come in useful, lets say &lt;br /&gt;
: we're adding a user to a database with the country they were born in:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 
: &amp;lt;?
: // Assume we pulled this from a database query:
: $OriginalUserName="Donnald O'Brian &amp;lt;CAN&amp;gt;";
: 
: // No more assumptions
: $UserName=MakeSafe($OriginalUserName);
: $SqlToAddUser="insert into tbl_users Name='".$UserName["MySQL"]."'";
: echo "&amp;lt;a href='viewuser.php?UN=".$UserName["URLEncode"]."'&amp;gt;
".$UserName["HTML"]."&amp;lt;/a&amp;gt;";
: ?&amp;gt;
: &lt;/pre&gt;: &lt;br /&gt;
: &lt;br /&gt;
: Just thought I'd share.  Makes things a LITTLE bit easier.&lt;br /&gt;
: &lt;br /&gt;
: PS: Not sure if its just the preview or what, but those code blocks &lt;br /&gt;
: don't seem to work right. :/&lt;br /&gt;
&lt;pre class="sourcecode"&gt;&amp;lt;html&amp;gt;the quick tcho a sdf&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/409738/409914/re-making-safe-variables/#409914</guid>
      <pubDate>Fri, 27 Nov 2009 00:45:48 -0800</pubDate>
      <category>PHP</category>
    </item>
  </channel>
</rss>
