PHP Built-in Web server

Source: Internet
Author: User
Tags response code
Built-in Web server

As of PHP 5.4.0, the CLI SAPI provides a built-in Web server.

This Web server was designed for developmental purposes only, and should not being used in production.

URI requests served from the current working directory where PHP were started, unless the-t option is used to specify An explicit document root.

If a URI request does not specify a file, then either index.php or index.html in the given directory is returned. If neither file exists, then a 404 response code is returned.

If a PHP file is given on the command line then the Web server is started it's treated as a "router" script for the Web s Erver. The script is run at the start of each HTTP request. If This script returns FALSE and then the requested resource is returned as-is. Otherwise The script ' s output is returned to the browser.

Example #1 starting the Web server

$ cd ~/public_html$ php-s localhost:8000

The terminal would show:

PHP 5.4.0 Development Server started at Thu Jul 10:43:28 2011Listening on localhost:8000document root Is/home/me/publi C_htmlpress Ctrl-c to quit

After URI requests for Http://localhost:8000/and http://localhost:8000/myscript.html The terminal would show something sim Ilar to:

PHP 5.4.0 Development Server started at Thu Jul 10:43:28 2011Listening on localhost:8000document root Is/home/me/publi C_htmlpress ctrl-c to quit. [Thu Jul 10:48:48]:: 1:39144 get/favicon.ico-request read[thu Jul 10:48:50]:: 1:39146 GET/-Request R Ead[thu Jul 10:48:50]:: 1:39147 get/favicon.ico-request read[thu Jul 10:48:52]:: 1:39148 get/myscript. Html-request Read[thu Jul 10:48:52]:: 1:39149 get/favicon.ico-request Read

Example #2 starting with a specific document root directory

$ cd ~/public_html$ php-s localhost:8000-t foo/

The terminal would show:

PHP 5.4.0 Development Server started at Thu Jul 10:50:26 2011Listening on localhost:8000document root Is/home/me/publi C_html/foopress Ctrl-c to quit

Example #3 Using a Router Script

Requests for images would display them, but requests for HTML files would display "Welcome to PHP"

router.php
if (Preg_match ('/\.:p ng|jpg|jpeg|gif) $/', $_server[' Request_uri '])
return false; Serve the requested resource as-is.
else {
echo "

Welcome to PHP

";
}
?>

$ php-s localhost:8000 router.php

After several URI requests the terminal would show something similar to:

PHP 5.4.0 Development Server started at Thu Jul 10:53:19 2011Listening on localhost:8000document root Is/home/me/publi C_htmlpress ctrl-c to quit. [Thu Jul 10:53:45]:: 1:55801 get/mylogo.jpg-request read[thu Jul 10:53:52]:: 1:55803 get/abc.html-re Quest Read[thu Jul 10:53:52]:: 1:55804 get/favicon.ico-request Read

Author:php.net

source:http://php.net/manual/en/features.commandline.webserver.php

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.