<?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>Moggylaceous &#187; Internet</title>
	<atom:link href="http://moggy.laceous.com/category/internet/feed/" rel="self" type="application/rss+xml" />
	<link>http://moggy.laceous.com</link>
	<description>If nothing we do matters, then all that matters is what we do.</description>
	<lastBuildDate>Tue, 22 Jun 2010 02:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Custom Request Methods And PHP</title>
		<link>http://moggy.laceous.com/2008/08/01/custom-request-methods-and-php/</link>
		<comments>http://moggy.laceous.com/2008/08/01/custom-request-methods-and-php/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 14:41:29 +0000</pubDate>
		<dc:creator>moggy</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://moggy.laceous.com/?p=59</guid>
		<description><![CDATA[By default, PHP (along with web browsers and HTML in general) uses two of the eight defined HTTP request methods. The eight methods are: HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS, and CONNECT. GET is the most widely used, followed by POST. Whenever you type a URL into your browser&#8217;s address bar, select a bookmark, [...]]]></description>
			<content:encoded><![CDATA[<p>By default, <a href="http://en.wikipedia.org/wiki/PHP">PHP</a> (along with <a href="http://en.wikipedia.org/wiki/Web_browser">web browsers</a> and <a href="http://en.wikipedia.org/wiki/HTML">HTML</a> in general) uses two of the eight defined HTTP <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods">request methods</a>.  The eight methods are: HEAD, GET, POST, PUT, DELETE, TRACE, OPTIONS, and CONNECT.  <strong>GET</strong> is the most widely used, followed by <strong>POST</strong>.  Whenever you type a <a href="http://en.wikipedia.org/wiki/Uniform_Resource_Locator">URL</a> into your browser&#8217;s address bar, select a bookmark, or click a link you&#8217;re doing a GET.  When you submit a form on a web page the method will be either GET or POST.</p>
<p><a href="http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_example:_the_World_Wide_Web">RESTfully</a> designed web-services use POST, GET, PUT, and DELETE.</p>
<p>Behind the scenes a basic GET request looks like this:</p>
<pre>
GET /path/to/file.php HTTP/1.1
Host: www.domain.com
</pre>
<p>If you were to type this URL into your browser it would look like http://www.domain.com/path/to/file.php</p>
<p>Similarly, a POST to the same URL would look like this:</p>
<pre>
POST /path/to/file.php HTTP/1.1
Host: www.domain.com
</pre>
<p>(Note: For the purpose of these examples I&#8217;m leaving out other request headers such as User-Agent, Keep-Alive, Connection, etc. that you might normally see when using a web browser)</p>
<p>When you do a GET or POST you can also pass in variables.  Consider a web page with a form that asks you to submit your first and last name.</p>
<p>The GET request might look like this:</p>
<pre>
GET /path/to/file.php?FirstName=John&#038;LastName=Doe HTTP/1.1
Host: www.domain.com
</pre>
<p>As you can see, the variables are attached to the URL.  In your browser this would look like http://www.domain.com/path/to/file.php?FirstName=John&#038;LastName=Doe.  These type of request variables are sometimes known as GET variables.</p>
<p>The POST version of this request would look like this:</p>
<pre>
POST /path/to/file.php HTTP/1.1
Host: www.domain.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

FirstName=John&#038;LastName=Doe
</pre>
<p>As you can see, the variables aren&#8217;t appended to the URL in a POST, but instead submitted following the request headers.  Variables submitted in this way are sometimes known as POST variables.  In this example I also set the Content-Length and Content-Type headers indicating that this POST was submitting name/value pairs.  You can also submit other data via POST such as uploading a file (and you would want to set the Content-Type appropriately).</p>
<p>PHP has two <a href="http://php.net/manual/en/language.variables.superglobals.php">superglobals</a> called <strong>$_GET</strong> and <strong>$_POST</strong>.  These arrays are automatically populated by PHP based on the request.</p>
<p>It should be noted that it&#8217;s technically possible to submit a POST request with both GET and POST-type variables.</p>
<pre>
POST /path/to/file.php?variable1=value1&#038;variable2=value2 HTTP/1.1
Host: www.domain.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

FirstName=John&#038;LastName=Doe
</pre>
<p>In this case, $_GET would be populated with:</p>
<pre>
Array
(
    [variable1] => value1
    [variable2] => value2
)
</pre>
<p>And $_POST would be populated with:</p>
<pre>
Array
(
    [FirstName] => John
    [LastName] => Doe
)
</pre>
<p><span style="text-decoration: underline;"><strong>Using A Custom Request Method</strong></span><br />
What happens if you want to create a RESTful web-service and submit a PUT request to a PHP page?  What if you want to make up your own request method; how do you handle that with PHP?</p>
<p>Consider the following request:</p>
<pre>
MYMETHOD /path/to/file.php HTTP/1.1
Host: www.domain.com
Content-Length: 27

FirstName=John&#038;LastName=Doe
</pre>
<p>In this example I&#8217;m submitting POST-style variables with my own made-up request method called <strong>MYMETHOD</strong>.  I could just as easily substitute <strong>MYMETHOD</strong> with <strong>PUT</strong>.  You can easily submit this type of request with a <a href="http://en.wikipedia.org/wiki/Mozilla_Firefox">Firefox</a> extension called <a href="http://www.xucia.com/#RestTest">RestTest</a>.  You can verify what is actually being sent with another extension called <a href="http://livehttpheaders.mozdev.org/">Live HTTP Headers</a>.</p>
<p>(Note: Some servers may be locked down to only allow certain request methods. If you make a request with a method that&#8217;s not allowed then the server will respond back with a 405 Method Not Allowed error.  A 405 response will include a response header letting you know which methods are allowed.)</p>
<p>If you don&#8217;t want to rely on the convenience of $_GET and $_POST then you can always parse out the raw data yourself.</p>
<ul>
<li><strong>$_SERVER['QUERY_STRING']</strong> is the raw version of <strong>$_GET</strong></li>
<li><strong>php://input</strong> is the raw version of <strong>$_POST</strong></li>
</ul>
<p>You should never have to parse out the query string manually, but $_POST is only populated if the request method is POST and an appropriate Content-Type is set.</p>
<p>To get our raw MYMETHOD data we could do something like this:</p>
<pre>
if ($_SERVER['REQUEST_METHOD'] == 'MYMETHOD')
{
  $raw_data = file_get_contents('php://input');
}
</pre>
<p>We could then parse out the raw data into a $_MYMETHOD variable (to mimic $_GET and $_POST).</p>
<pre>
$array = explode('&#038;', $raw_data);
foreach($array as $item)
{
  list($key, $value) = explode('=', $item);
  $_MYMETHOD[$key] = $value;
}
</pre>
<p><span style="text-decoration: underline;"><strong>Putting It All Together</strong></span><br />
Besides using a tool like RestTest, we can also create a web page that submits a MYMETHOD request.  We&#8217;ll do this by using some JavaScript and making an <a href="http://en.wikipedia.org/wiki/Ajax_(programming)">Ajax</a> request with the <a href="http://en.wikipedia.org/wiki/XMLHttpRequest">XMLHttpRequest</a> object.</p>
<pre>
&lt;?php
if ($_SERVER['REQUEST_METHOD'] == 'MYMETHOD' &#038;&#038; strpos($_SERVER['CONTENT_TYPE'], 'application/x-javascript') !== FALSE):
  $raw_data = file_get_contents('php://input');
  $array = explode('&#038;', $raw_data);
  foreach($array as $item)
  {
    list($key, $value) = explode('=', $item);
    $_MYMETHOD[$key] = $value;
  }
  print_r($_MYMETHOD);

else:
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
/**
 * Bridge XMLHTTP to XMLHttpRequest in pre-7.0 Internet Explorers
 */
if( typeof XMLHttpRequest == "undefined" )
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP") }     catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP") }  catch(e) {}
    throw new Error( "This browser does not support XMLHttpRequest or XMLHTTP." )
  };

function ajax(url, vars) {
  var request = new XMLHttpRequest();
  request.open("MYMETHOD", url, true);
  request.setRequestHeader("Content-Type", "application/x-javascript;");

  request.onreadystatechange = function() {
    if (request.readyState == 4 &#038;&#038; request.status == 200) {
      if (request.responseText) {
        alert(request.responseText);
      }
    }
  };

  request.send(vars);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;input type="button" value="click for ajax" onclick="javascript:ajax('&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;', 'FirstName=John&amp;amp;LastName=Doe');" /&gt;

&lt;/body&gt;
&lt;/html&gt;
&lt;?php endif; ?&gt;
</pre>
<p>If you copy this into a PHP file (i.e. mymethod.php), upload it to a PHP-enabled web server, and navigate to it with your web browser you&#8217;ll see a button that says <strong>click for ajax</strong>.  If you click the button, an asynchronous MYMETHOD request will be sent, the server will parse out the sent name/value pairs and send the processed data back to the browser.  The user will then be presented with an alert popup containing the following text:</p>
<pre>
Array
(
    [FirstName] => John
    [LastName] => Doe
)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://moggy.laceous.com/2008/08/01/custom-request-methods-and-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Unprotect A Folder In Apache</title>
		<link>http://moggy.laceous.com/2008/02/09/how-to-unprotect-a-folder-in-apache/</link>
		<comments>http://moggy.laceous.com/2008/02/09/how-to-unprotect-a-folder-in-apache/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 08:45:39 +0000</pubDate>
		<dc:creator>moggy</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://moggy.laceous.com/2008/02/09/how-to-unprotect-a-folder-in-apache/</guid>
		<description><![CDATA[The Apache HTTP web-server makes it easy to protect a folder (and its sub-folders) with a username/password prompt. Simply create a .htaccess file in the specified folder, and add something like this: AuthName "Folder Protection" AuthUserFile /path/to/.htpasswd AuthGroupFile /dev/null AuthType Basic Require valid-user For help with generating a valid .htpasswd file, you can use a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Apache_HTTP_Server">Apache HTTP web-server</a> makes it easy to protect a folder (and its sub-folders) with a username/password prompt.  Simply create a <a href="http://en.wikipedia.org/wiki/.htaccess">.htaccess</a> file in the specified folder, and add something like this:</p>
<pre>
AuthName "Folder Protection"
AuthUserFile /path/to/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
Require valid-user</pre>
<p>For help with generating a valid .htpasswd file, you can use a tool like Dynamic Drive&#8217;s <a href="http://tools.dynamicdrive.com/password/">.htaccess Password Generator</a>.  There are <a href="http://www.google.com/search?q=apache+password+protect">plenty</a> of tutorials online that explain this process.</p>
<p>On the other hand, it&#8217;s much harder to find information about unprotecting a sub-folder of a folder that is protected in the manner specified above.  Some posts I read implied that it wasn&#8217;t possible at all.  I finally found the answer <a href="http://toel.se/~toel/content/knowledge.php?act=show&amp;id=11">here</a> and <a href="http://www.hybrid6.com/webgeek/2007/02/htaccess-reference.php#Password_unprotection">here</a>.  Basically, in the sub-folder you&#8217;re trying to unprotect, you simply need to create a new .htaccess file and add the following:</p>
<pre>
Satisfy Any</pre>
]]></content:encoded>
			<wfw:commentRss>http://moggy.laceous.com/2008/02/09/how-to-unprotect-a-folder-in-apache/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced) (user agent is rejected)

Served from: moggy.laceous.com @ 2010-09-07 14:48:23 -->