Here's a summary of everything above. For a complete understanding, read all of that, but the basics are here.
This tutorial is designed to show you how to create a single PHP page (and .htaccess file) that will make an entire directory (or site) dynamically served by PHP.
This is like normal mod_rewrite methods, except that the control is given entirely to PHP.
First, create a .htaccess file with the following information:
Code:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} !^/test/index.php$
RewriteCond %{REQUEST_URI} !^/test/index.php/
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
Note that "test" is the name of the directory (as in yoursite.com/test) where you want this to work. If you want it to apply to the whole site, use RewriteBase /
and remove the subdirectory from the URIs as well.
Now create a file called "index.php" in that directory:
PHP Code:
<?php
if (!isset($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; }
if (strpos($_SERVER['REQUEST_URI'],'/test/index.php')===0) {
if (strpos($_SERVER['PATH_INFO'],'/index.php')!==0) {
$_SERVER['PATH_INFO'] = '/index.php'.$_SERVER['PATH_INFO'];
}
}
//now $_SERVER['PATH_INFO'] will work as expected.
$_SERVER['PATH_INFO'] will contain the original request uri and query string (like /folder/file.ext?var=value) and you can do whatever you'd like with it.
As a very simple example of what this can do, you can use the following:
PHP Code:
$file = $_SERVER['PATH_INFO']; //get what was sent
$file = substr($file,0,strpos($file,'?')); //remove any query string ["get" variables]
if (strpos($file,'../')===0) { exit('Big Security Threat!'); } //don't allow higher level directories!!
include($file); //include the file
That will serve files normally*. In other words, now this effectively does nothing: just as if you typed anything into the URL without all of this setup.
So that's just an example of how you'd be able to create a "php fileserver".
The example above is not helpful (since it doesn't do anything new), but it's just a clear example of how you'd approach using this method.
(*Note: this will work fine for any sort of text file. It will NOT work for images, audio, video, or other files that require specific headers to be sent. At least I don't think it will, but it might depend on the server.)
For some more practical examples, consider the following:
Replace normal "get" variables with pretty URLs
Request URI: /home
Use PHP to translate that to: $_GET['page'] = 'home';
Now, you can use that just like:
Fake Request URI: index.php?page=home
How: 1. strip the initial slash; 2. make sure it doesn't contain any extra characters (ignore everything after a slash or after a question mark); 3. the variable should be clean now; 4. Just use normal practices for safety (for example, don't allow ../ at the beginning of it).
Hide file extensions:
Request URI: /page
Use PHP to translate that to 'page.php'
Now, it is like:
Fake Request URI: /page.php
How: 1. use normal security precautions and allow only a single word (valid filename-- AZaz09_ [and more if you want]); 2. include($that.'.php');
There are many possibilities with this, and I've been using it a lot recently to create dynamic websites. You can do anything you want with it and you're only limited by your knowledge of PHP and working with strings.
The one weakness of this is that if you want to serve images or other files that require special headers you will need to work this out in PHP (it's possible), or you might want to use a real mod_rewrite method that wouldn't have this problem (since PHP must use include() to serve the pages).
Bookmarks