5 * Router script for the built-in PHP web server.
7 * The built-in web server should only be used for development and testing as it
8 * has a number of limitations that makes running Drupal on it highly insecure
9 * and somewhat limited.
12 * - The server is single-threaded, any requests made during the execution of
13 * the main request will hang until the main request has been completed.
14 * - The web server does not enforce any of the settings in .htaccess in
15 * particular a remote user will be able to download files that normally would
16 * be protected from direct access such as .module files.
18 * The router script is needed to work around a bug in PHP, see
19 * https://bugs.php.net/bug.php?id=61286.
22 * php -S localhost:8888 .ht.router.php
24 * @see http://php.net/manual/en/features.commandline.webserver.php
27 $url = parse_url($_SERVER['REQUEST_URI']);
28 if (file_exists('.' . $url['path'])) {
29 // Serve the requested resource as-is.
33 // Work around the PHP bug.
35 $script = 'index.php';
36 if (strpos($path, '.php') !== FALSE) {
37 // Work backwards through the path to check if a script exists. Otherwise
38 // fallback to index.php.
40 $path = dirname($path);
41 if (preg_match('/\.php$/', $path) && is_file('.' . $path)) {
42 // Discovered that the path contains an existing PHP file. Use that as the
44 $script = ltrim($path, '/');
47 } while ($path !== '/' && $path !== '.');
50 // Update $_SERVER variables to point to the correct index-file.
51 $index_file_absolute = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $script;
52 $index_file_relative = DIRECTORY_SEPARATOR . $script;
54 // SCRIPT_FILENAME will point to the router script itself, it should point to
55 // the full path of index.php.
56 $_SERVER['SCRIPT_FILENAME'] = $index_file_absolute;
58 // SCRIPT_NAME and PHP_SELF will either point to index.php or contain the full
59 // virtual path being requested depending on the URL being requested. They
60 // should always point to index.php relative to document root.
61 $_SERVER['SCRIPT_NAME'] = $index_file_relative;
62 $_SERVER['PHP_SELF'] = $index_file_relative;
64 // Require the script and let core take over.
65 require $_SERVER['SCRIPT_FILENAME'];