<?php | |
// php build in server vhost | |
// php -S 0.0.0.0:80 router.php | |
// on Windows, router.php must be absolute path, bug of php v5.4.26 | |
$map = array( | |
'host.name' => '/file/path', | |
); | |
$f = __DIR__.'/router_config.php'; | |
if (file_exists($f)) { | |
$map = require $f; | |
} | |
$host = $_SERVER['HTTP_HOST']; | |
if (isset($map[$host])) { | |
error_log("$host $_SERVER[REQUEST_URI]"); | |
$root = $map[$host]; | |
$arr = explode('?', $_SERVER["REQUEST_URI"]); | |
$path = $arr[0]; | |
$f = $root.$path; | |
if ($f == "$root/favicon.ico") { | |
if (is_file($f)) { | |
header("Content-Type: image/x-icon"); | |
readfile($f); | |
} else { | |
return false; | |
} | |
} | |
$fhtml = "$root/index.html"; | |
$fphp = "$root/index.php"; | |
if (is_file($f)) { | |
$pathinfo = pathinfo($f); | |
if (isset($pathinfo['extension']) && $pathinfo['extension'] == 'php') { | |
chdir(dirname($f)); | |
include $f; | |
} else { | |
$meme = get_content_type($f); | |
header("Content-Type: $meme"); | |
readfile($f); | |
} | |
} elseif (is_file($fhtml)) { | |
echo file_get_contents($f); | |
} elseif (is_file($fphp)) { | |
chdir($root); | |
include $fphp; | |
} | |
} else { | |
return false; | |
} | |
function get_content_type($f) | |
{ | |
static $map = array( | |
'js' => 'application/javascript', | |
'css' => 'text/css', | |
'png' => 'image/png', | |
'jpg' => 'image/jpeg', | |
'jpeg' => 'image/jpeg', | |
'gif' => 'image/gif', | |
'ico' => 'image/x-icon', | |
); | |
$pathinfo = pathinfo($f); | |
if (isset($pathinfo['extension']) && ($extension = strtolower($pathinfo['extension'])) && isset($map[$extension])) { | |
return $map[$extension]; | |
} | |
return 'text/html'; | |
} |
![]() |
1
vibbow 2014-03-10 18:51:17 +08:00
PHP Build-in server作为一个单线程服务器,做vhost的意义在哪里?
|
![]() |
2
picasso250 OP |
![]() |
3
picasso250 OP 发现 Windows 下的一个bug:router.php 必须得用绝对路径指定。
|