13c07-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 158 行

PHP
158
字号
<?php// A barebones, basic, php webserver// First let's insert some basic configuration, at the top of the file//  where it is easy to edit$port = 8088;$host = '127.0.0.1';$docroot = '/html';// Ensure that our server will never timeoutset_time_limit(0);// Start off by listening to our designated port:if (!($server = stream_socket_server("tcp://{$host}:{$port}",        $err_num, $err_string))) {    exit("ERROR: Failed to Open Server - {$err_num} - {$err_string}\n");}// Now, begin a permanent loopfor (;;) {    // Listen for a new connection and don't timeout:    $client = stream_socket_accept($server, -1);        // If we truly got a client connection, and not an error:    if ($client) {        // Attempt to fork a new process, so that the server can         //  still accept new connections.        $pid = pcntl_fork();                // If an error happened:        if ($pid == -1) {            exit("ERROR: Could not create new process!");        }        // Else, if we are in the child        elseif (!$pid) {            // Read the first line of the request            //  (The only line we are going to bother with)            $command = fgets($client, 2048);                        // Read the rest of the request; however, we are going to ignore            //  it - Given we are being such a simple web server. We need to            //  read all lines presented, until we get a blank line or             //  error, that means the request is done.            while($line = fgets($client, 2048)) {                if (trim($line) === '') { break; }            }                        // Now, divide up the request string into it's parts:            $request = explode(' ', $command);                        // If the Request is not GET tell them we cannot do that.            if ($request[0] != 'GET') {                // Respond that we do not support this                @fwrite($client, "HTTP/0.9 501 Not ImplementedServer: Bare-Basic-PHP-WebServerContent-Type: text/html<html><head><title>501 Not Implemented</title></head><body><h1>501 Not Implemented</h1><p>This is a very basic web server that only implements GET</p></body></html>");                // Close the connection                fclose($client);                // And exit the child                exit();            }            // Attempt to split any GET parameters out of the request:            $parts = explode('?', $request[1]);                                    // If the request ends in '/', assume index.php            if ($parts[0]{strlen($parts[0])-1} == '/') {                $parts[0] .= 'index.php';            }                        // Now, if the file requested cannot be found in document root:            if (!is_file($docroot . $parts[0])) {                // Respond with a 404 error - We can't find the document:                @fwrite($client, "HTTP/0.9 404 Not FoundServer: Bare-Basic-PHP-WebServerContent-Type: text/html<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1><p>We looked, but the file you requested was nowhere to be found!</p></body></html>");                // Close the connection                fclose($client);                // And exit the child                exit();            }            // Finally, we are ALMOST ready            // We just need to decide what filetype to return:            $path = pathinfo($parts[0]);            switch ($path['extension']) {                // For the image types:                case 'gif':                case 'png':                case 'jpg':                    $mime = "image/{$path['extension']}";                    break;                // Text Types:                case 'html':                case 'xml':                case 'css':                    $mime = "text/{$path['extension']}";                    break;                // Javascript:                case 'js':                    $mime = 'application/x-javascript';                    break;                // Special case for PHP files:                case 'php':                    $mime = 'text/html';                    break;                // Give a generic answer for everything else:                default:                    $mime = 'application/octet-stream';            }            // We know the mime type to return, prep the GET variables            if (isset($parts[1])) {                // Parse them into $_GET just like PHP would have                parse_str($parts[1], $_GET);            }                        // Now, INCLUDE the file, no matter what the type.  This is a            //  PHP webserver, so we are going to assume that EVERY file            //  might have some embedded PHP in it.  Capture the output            //  via buffering            ob_start();            include "{$docroot}{$parts[0]}";            $output = ob_get_contents();            ob_end_clean();                        // Ok, time to return everything!            $length = strlen($output);            @fwrite($client,"HTTP/0.9 200 OkServer: Bare-Basic-PHP-WebServerContent-Type: {$mime}Content-Length: {$length}{$output}");            // Since we aren't supporting Keep-Alive, close the connection            fclose($client);            exit();        }    }    // We are in the parent here, just about to restart our loop, make sure    //  that the parent closes the client connection.    @fclose($client);}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?