<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-9154511742305251253</id><updated>2012-02-16T15:41:45.568+05:00</updated><category term='injection'/><category term='sql'/><category term='application'/><category term='php'/><category term='variables'/><category term='security'/><category term='prevent'/><category term='session'/><title type='text'>Sarfraz Ahmed's Articles And More...</title><subtitle type='html'>Here you will find programming articles, general articles and more...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sarfraz-ahmed.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9154511742305251253/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sarfraz-ahmed.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sarfraz Ahmed</name><uri>http://www.blogger.com/profile/08244350417517418689</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://bp0.blogger.com/__q68a_Hxs3I/SI7XxjumFTI/AAAAAAAAAAU/RIPaWo8K2xY/S220/Me.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9154511742305251253.post-7674009472709179582</id><published>2008-07-30T10:41:00.003+06:00</published><updated>2008-07-30T11:06:51.707+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sql'/><category scheme='http://www.blogger.com/atom/ns#' term='prevent'/><category scheme='http://www.blogger.com/atom/ns#' term='injection'/><category scheme='http://www.blogger.com/atom/ns#' term='security'/><title type='text'>Preventing SQL Injection</title><content type='html'>&lt;p class="MsoNormal"&gt;&lt;b style=""&gt;What is SQL Injection?&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p class="MsoNormal"&gt;Many sites use databases as a back-end to store their data, using queries to insert and select data from it. However, many people are unaware that such sites are often vulnerable to a form of attack called SQL injection.&lt;br /&gt;&lt;br /&gt;SQL injection is when malformed user input is used directly and deliberately in an SQL query, in a way that allows the attacker to manipulate the query. This means that an attacker could delete portions of your database, make himself an admin account etc, the possibilities are endless.&lt;br /&gt;&lt;br /&gt;One of the most common vulnerabilities is when logging in to a site. Take this example:&lt;br /&gt;&lt;br /&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;$username = $_POST['username'];&lt;br /&gt;$password = $_POST['password'];&lt;br /&gt;&lt;br /&gt;$result = mysql_query("&lt;br /&gt;SELECT *&lt;br /&gt;FROM&lt;br /&gt;   site_users&lt;br /&gt;WHERE&lt;br /&gt;   username = '$username'&lt;br /&gt;   AND&lt;br /&gt;   password = '$password'&lt;br /&gt;");&lt;br /&gt;&lt;br /&gt;if ( mysql_num_rows($result) &gt; 0 )&lt;br /&gt;   // logged in&lt;/span&gt;&lt;span style=";font-family:&amp;quot;;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This is vulnerably to a pretty obvious SQL injection; can you work out how an attacker could modify the query to allow himself to be logged in regardless of whether or not he has the right password?&lt;br /&gt;&lt;br /&gt;If the attacker enters a valid username in the username field: "&lt;b&gt;&lt;i&gt;rob&lt;/i&gt;&lt;/b&gt;" and the following in the password field: &lt;b&gt;&lt;i&gt;' OR 1=1 '&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The resulting query will look like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;SELECT *&lt;br /&gt;FROM&lt;br /&gt;   site_users&lt;br /&gt;WHERE&lt;br /&gt;   username = 'rob'&lt;br /&gt;   AND&lt;br /&gt;   password = '' OR 1=1&lt;/span&gt;&lt;span style=";font-family:&amp;quot;;" &gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;It will therefore select all users where:&lt;br /&gt;&lt;br /&gt;•    the username is "rob"&lt;br /&gt;•    either the "password" field is empty, or 1 is equal to 1&lt;br /&gt;&lt;br /&gt;Since the last criteria will always be true—when is 1 ever not equal to 1?—, the user will be able to log in as rob without knowing rob's password !!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;How can we prevent SQL injection attacks?&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;As with XSS attacks, you must never trust user input. The best way of cleaning user input is using PHP's built in &lt;b&gt;&lt;i&gt;mysql_real_escape_string()&lt;/i&gt;&lt;/b&gt; function; this will escape characters such as ', " and others, making them useless in "breaking out" of a quoted string as in the above example. If you're using a number in your query, then you should use &lt;b&gt;&lt;i&gt;intval()&lt;/i&gt;&lt;/b&gt; on the inputted number to ensure it is numeric.&lt;br /&gt;&lt;br /&gt;I have also made the following function that can be used to discard any characters that can be used to manipulate the SQL queries. So, you can use this functions just as well to validate your SQL queries:&lt;/p&gt;    &lt;p class="MsoNormal"&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;function is_valid($input)&lt;br /&gt;{&lt;br /&gt;   $input = strtolower($input);&lt;br /&gt;&lt;br /&gt;   if (str_word_count($input) &gt; 1)&lt;br /&gt;   {&lt;br /&gt;       $loop = true;&lt;br /&gt;       $input = explode(" ",$input);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   $bad_strings = array("'","--","select","union","insert","update","like","delete","1=1","or");&lt;br /&gt;&lt;br /&gt;   if ($loop == true)&lt;br /&gt;   {&lt;br /&gt;       foreach($input as $value)&lt;br /&gt;       {&lt;br /&gt;           if (in_array($value,$bad_strings))&lt;br /&gt;           {&lt;br /&gt;             return false;&lt;br /&gt;           }&lt;br /&gt;           else&lt;br /&gt;           {&lt;br /&gt;             return true;&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;   else&lt;br /&gt;   {&lt;br /&gt;       if (in_array($input,$bad_strings))&lt;br /&gt;       {&lt;br /&gt;         return false;&lt;br /&gt;       }&lt;br /&gt;       else&lt;br /&gt;       {&lt;br /&gt;         return true;&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;span style=";font-family:&amp;quot;;" &gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p class="MsoNormal"&gt;&lt;b style=""&gt;Function Usage&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p class="MsoNormal"&gt;This is how you can use the above function:&lt;/p&gt;    &lt;p class="MsoNormal"&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;if (is_valid($_REQUEST["username"]) == true &amp;amp;&amp;amp; is_valid($_REQUEST["pass"]) == true)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;&lt;span style=""&gt;     &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;&lt;span style=""&gt;          &lt;/span&gt;//login now&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="background: silver none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;font-family:&amp;quot;;" &gt;&lt;span style=""&gt;     &lt;/span&gt;}&lt;/span&gt;&lt;span style=";font-family:&amp;quot;;" &gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p class="MsoNormal"&gt;However, don’t depend on this function alone, you must still use the &lt;b&gt;&lt;i&gt;mysql_real_escape_string()&lt;/i&gt;&lt;/b&gt; function in your SQL query. You can also modify this function or you can get a new idea to work out of this function.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9154511742305251253-7674009472709179582?l=sarfraz-ahmed.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sarfraz-ahmed.blogspot.com/feeds/7674009472709179582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9154511742305251253&amp;postID=7674009472709179582' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9154511742305251253/posts/default/7674009472709179582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9154511742305251253/posts/default/7674009472709179582'/><link rel='alternate' type='text/html' href='http://sarfraz-ahmed.blogspot.com/2008/07/preventing-sql-injection.html' title='Preventing SQL Injection'/><author><name>Sarfraz Ahmed</name><uri>http://www.blogger.com/profile/08244350417517418689</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://bp0.blogger.com/__q68a_Hxs3I/SI7XxjumFTI/AAAAAAAAAAU/RIPaWo8K2xY/S220/Me.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9154511742305251253.post-137628706566324121</id><published>2008-07-29T14:45:00.000+06:00</published><updated>2008-07-29T15:13:38.739+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='application'/><category scheme='http://www.blogger.com/atom/ns#' term='session'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='variables'/><title type='text'>Application Variables in PHP</title><content type='html'>&lt;p&gt; If you have been doing development using ASP, you must be familiar with the Application variables. Application variables in ASP work very similar to the &lt;span class="code"&gt;$_SESSION&lt;/span&gt; variable in PHP. However, unlike the &lt;span class="code"&gt;$_SESSION&lt;/span&gt; variable, application variables are not specific to  an individual user; they persist across every user of every page on the web  site. &lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" class="header2"&gt;Benefits&lt;/span&gt;  &lt;p&gt;The immediate benefits of application variables may not be apparent, but  esperienced ASP coders understand their power. Take this one-liner, for example:   &lt;table bgcolor="#e0e0e0" border="0" cellpadding="4" cellspacing="0" width="100%"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;pre&gt;//You are visitor number&lt;br /&gt; echo $_APP["visitor_count"]++;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt; &lt;p&gt;In fact, application variables have many other uses as well than what has been depicted in this article.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" class="header2"&gt;Solution&lt;/span&gt;  &lt;p&gt;I looked on the web and read numerous other work-arounds for application  variables in PHP, but found none that I liked. Every one required too much  overhead code, or else multiple lines of code to actually use the variables.  Here is my solution: &lt;/p&gt; &lt;p&gt;app.php  &lt;table bgcolor="#e0e0e0" border="0" cellpadding="4" cellspacing="0" width="100%"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;pre&gt;&lt;br /&gt;&lt;br /&gt;define("APP_DATA_FILE",&lt;br /&gt;   "/tmp/application.data");&lt;br /&gt;&lt;br /&gt;function application_start ()&lt;br /&gt;{&lt;br /&gt;   global $_APP;&lt;br /&gt;&lt;br /&gt;   // if data file exists, load application&lt;br /&gt;   //   variables&lt;br /&gt;   if (file_exists(APP_DATA_FILE))&lt;br /&gt;   {&lt;br /&gt;       // read data file&lt;br /&gt;       $file = fopen(APP_DATA_FILE, "r");&lt;br /&gt;       if ($file)&lt;br /&gt;       {&lt;br /&gt;           $data = fread($file,&lt;br /&gt;               filesize(APP_DATA_FILE));&lt;br /&gt;           fclose($file);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       // build application variables from&lt;br /&gt;       //   data file&lt;br /&gt;       $_APP = unserialize($data);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function application_end ()&lt;br /&gt;{&lt;br /&gt;   global $_APP;&lt;br /&gt;&lt;br /&gt;   // write application data to file&lt;br /&gt;   $data = serialize($_APP);&lt;br /&gt;   $file = fopen(APP_DATA_FILE, "w");&lt;br /&gt;   if ($file)&lt;br /&gt;   {&lt;br /&gt;       fwrite($file, $data);&lt;br /&gt;       fclose($file);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" class="header2"&gt;Usage&lt;/span&gt;  &lt;p&gt;The usage of the &lt;span class="code"&gt;$_APP&lt;/span&gt; variable is very similar to  &lt;span class="code"&gt;$_SESSION&lt;/span&gt;. Before using it on a page, you must include  &lt;span class="code"&gt;&lt;nobr&gt;app.php&lt;/nobr&gt;&lt;/span&gt; and call &lt;span class="code"&gt;&lt;nobr&gt;application_start()&lt;/nobr&gt;&lt;/span&gt;. When you are finished, you  must call &lt;span class="code"&gt;&lt;nobr&gt;application_end()&lt;/nobr&gt;&lt;/span&gt;. &lt;/p&gt; &lt;p&gt;&lt;span style="font-style: italic;"&gt;Sample PHP file using application variables:&lt;/span&gt;  &lt;table bgcolor="#e0e0e0" border="0" cellpadding="4" cellspacing="0" width="100%"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;pre&gt;&lt;br /&gt;include("app.php");&lt;br /&gt;application_start();&lt;br /&gt;&lt;br /&gt;//You are visitor number&lt;br /&gt; echo $_APP["visitor_count"]++;&lt;br /&gt;&lt;br /&gt;application_end();&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;" class="header2"&gt;Optimizations and  Other Notes&lt;/span&gt;  &lt;p&gt;Numerous improvements can be made to this code, but they are not included  here for simplicity.  &lt;/p&gt;&lt;ul&gt;&lt;li&gt;For added security, permissions on the application data file should not  allow any user, except for the web server, to read or write to it. Otherwise, if  application variables contained sensitive data, such as passwords or credit card  information, other users on the system could read this data.  &lt;/li&gt;&lt;li&gt;To keep from writing to disk unnecessarily, only call &lt;span class="code"&gt;&lt;nobr&gt;application_end()&lt;/nobr&gt;&lt;/span&gt; if data in the &lt;span class="code"&gt;$_APP&lt;/span&gt; variable has changed.  &lt;/li&gt;&lt;li&gt;Another alternative is to let the &lt;span class="code"&gt;&lt;nobr&gt;application_end()&lt;/nobr&gt;&lt;/span&gt; function determine whether the  &lt;span class="code"&gt;$_APP&lt;/span&gt; variable has been changed, and only write it to  disk if necessary. Simply make a copy of the &lt;span class="code"&gt;$_APP&lt;/span&gt;  variable in &lt;span class="code"&gt;&lt;nobr&gt;application_start()&lt;/nobr&gt;&lt;/span&gt;, then  compare &lt;span class="code"&gt;$_APP&lt;/span&gt; to the original in &lt;span class="code"&gt;&lt;nobr&gt;application_end()&lt;/nobr&gt;&lt;/span&gt;.  &lt;/li&gt;&lt;li&gt;Finally, don't forget that unlike &lt;span class="code"&gt;$_SESSION&lt;/span&gt;, the  &lt;span class="code"&gt;$_APP&lt;/span&gt; variable is only available in the global scope.  When using &lt;span class="code"&gt;$_APP&lt;/span&gt; from inside a function, be sure to  include the statement &lt;span class="code"&gt;global $_APP;&lt;/span&gt; at the top of the  function.&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9154511742305251253-137628706566324121?l=sarfraz-ahmed.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sarfraz-ahmed.blogspot.com/feeds/137628706566324121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=9154511742305251253&amp;postID=137628706566324121' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9154511742305251253/posts/default/137628706566324121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9154511742305251253/posts/default/137628706566324121'/><link rel='alternate' type='text/html' href='http://sarfraz-ahmed.blogspot.com/2008/07/application-variables-in-php.html' title='Application Variables in PHP'/><author><name>Sarfraz Ahmed</name><uri>http://www.blogger.com/profile/08244350417517418689</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='23' height='32' src='http://bp0.blogger.com/__q68a_Hxs3I/SI7XxjumFTI/AAAAAAAAAAU/RIPaWo8K2xY/S220/Me.jpg'/></author><thr:total>0</thr:total></entry></feed>
