Built-in web server

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

This web server is designed for developmental purposes only, and should notbe used in production. 

URI requests are served from the current working directory wherePHP was started, unless the -t option is used to specify anexplicit document root. 

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

If a PHP file is given on the command line when the web server isstarted it is treated as a "router" script for the web server.The script is run at the start of each HTTP request. If thisscript returns FALSE, then the requested resource is returnedas-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 will show: 

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011

Listening on localhost:8000

Document root is /home/me/public_html

Press Ctrl-C to quit

After URI requests for http://localhost:8000/ andhttp://localhost:8000/myscript.html the terminal will showsomething similar to: 

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011

Listening on localhost:8000

Document root is /home/me/public_html

Press Ctrl-C to quit.

[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read

[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read

[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read

[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read

[Thu Jul 21 10:48:52 2011] ::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 will show: 

PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011

Listening on localhost:8000

Document root is /home/me/public_html/foo

Press Ctrl-C to quit

Example #3 Using a Router Script

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

<?php

// router.php

if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"]))

    return false;    // serve the requested resource as-is.

else { 

    echo "<p>Welcome to PHP</p>";

}

?>  

$ php -S localhost:8000 router.php

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

PHP 5.4.0 Development Server started at Thu Jul 21 10:53:19 2011

Listening on localhost:8000

Document root is /home/me/public_html

Press Ctrl-C to quit.

[Thu Jul 21 10:53:45 2011] ::1:55801 GET /mylogo.jpg - Request read

[Thu Jul 21 10:53:52 2011] ::1:55803 GET /abc.html - Request read

[Thu Jul 21 10:53:52 2011] ::1:55804 GET /favicon.ico - Request read

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

php编程基础教程.pptx|php编程培训,php,编程,基础,教程,pptx
php编程基础教程.pptx

历史上的今天:04月29日

热门专题

昆明综合高中|昆明综合高中
昆明综合高中
小程序开发|微信小程序,小程序开发,小程序,小程序制作,微信小程序开发,小程序公司,小程序开发公司,分销,三级分销系统,分销系统
小程序开发
综合高中|云南综合高中,昆明综合高中,综合高中能考本一吗,综合高中和普通高中的区别,综合高中是什么意思,综合高中能参加全国统一高考吗,综合高中可以考哪些大学,综合高中的学籍是什么
综合高中
安徽开放大学|安徽开放大学报名,安徽开放大学报考,安徽开放大学,什么是安徽开放大学,安徽开放大学学历,安徽开放大学学费,安徽开放大学报名条件,安徽开放大学报名时间,安徽开放大学学历,安徽开放大学专业
安徽开放大学
易捷尔单招|易捷尔单招,易捷尔单招培训,易捷尔单招报名,易捷尔单招考试,易捷尔单招培训学校,易捷尔单招分数
易捷尔单招
云南巨榕教育投资集团有限公司|云南巨榕教育投资集团有限公司,巨榕教育集团,巨榕教育
云南巨榕教育投资集团有限公司
APP开发|app开发_app开发公司_app软件开发_专业app开发_云南app开发公司_app定制_原生app开发定制
APP开发
国家开放大学|国家开放大学报名,国家开放大学报考,国家开放大学,什么是国家开放大学,国家开放大学学历,国家开放大学学费,国家开放大学报名条件,国家开放大学报名时间,国家开放大学学历,国家开放大学专业
国家开放大学

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部