Skip to content

Instantly share code, notes, and snippets.

@vaites
Created August 15, 2016 23:04
Show Gist options
  • Save vaites/c3ff426025c9dec976769885d82ae612 to your computer and use it in GitHub Desktop.
Save vaites/c3ff426025c9dec976769885d82ae612 to your computer and use it in GitHub Desktop.
h5ai router for PHP built-in server
<?php
/**
* h5ai router for PHP built-in server
*
* @link http://php.net/manual/en/features.commandline.webserver.php
*/
// full path of requested file
$path = dirname(__DIR__) . preg_replace('/\?(.*)/', '', $_SERVER['REQUEST_URI']);
// h5ai is required when user request a directory
if(file_exists($path) && is_dir($path))
{
// directory must not contain index.php or index.html
if(file_exists("$path/index.php") == false && file_exists("$path/index.html") == false)
{
// script name must be "tweaked" to detect path
$_SERVER['SCRIPT_NAME'] = '/_h5ai/public/index.php';
include __DIR__ . "/public/index.php";
exit;
}
}
// CSS is sent with text/html content type, so we need to fix it
if($path == __DIR__ . '/public/css/styles.css')
{
header('Content-Type: text/css');
}
// return resource "as-is"
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment