Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of PHP's built-in WEB server

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the use of PHP's built-in WEB server". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the use of PHP's built-in WEB server".

PHP's built-in WEB server

In many cases, we need to simply run a small demo to verify whether some code or wheels are available and can be run, but it is very troublesome to match nginx or apache. In fact, PHP CLI already provides a simple test server, and we can run it directly for some simple testing work.

Directly start a built-in server php-S localhost:8081

By directly using the-S command option and then specifying the address and port number, we can run a simple WEB server built into PHP. By default, this address looks for the index.php or index.html file in the current directory. When we enter the specified file in the browser, we access the specified file. If none of them are found, a 404 error will be returned normally.

The console outputs the access to the current server, as shown in the following figure:

There is not much difference between this built-in server and the application server built with servers such as nginx. Content such as $_ SERVER can be obtained normally, and other files can be loaded normally using functions such as include. In other words, there is no problem for this built-in WEB server to run some frameworks. It can fully meet our testing requirements. It should be noted, however, that this built-in WEB server cannot be used in a production environment. After all, its function is still too simple, not a high-standard server application with production equipment.

Specify the running directory of the built-in server

We can also run the php code for the specified directory in any directory, just add a-t option to indicate the root of the server to run.

Php-S localhost:8081-t dev-blog/php/202004/source

This allows us to run a test environment server with the dev-blog/php/202004/source directory as the root directory.

Built-in WEB server .php using the routing script php-S localhost:8081 PHP

If we specify a PHP file directly to the current server, then opening the link directly will access the contents of the file instead of looking for files such as index.php. Even if we continue to add other paths or other filenames to the URL background, it will still open this file, that is, we have started a single-file entry application server program. Just like the index.php files of various frameworks, for example, we use this file to do a simple routing distribution test:

$routePages = [

'/ testRoute2.php'

'/ route/testRoute1.php'

]

If (in_array ($_ SERVER ['REQUEST_URI'], $routePages)) {

Include _ _ DIR__. $_ SERVER ['REQUEST_URI']

} else {

Print_r ($_ SERVER)

}

/ / route/testRoute1.php

Echo "Hello Route1!"

/ / testRoute2.php

Echo "Hello Route2!"

The two test files simply output a paragraph of text to distinguish between the two files being loaded. The above code means that when we access the two defined routing paths, the corresponding files are loaded, and accessing the other paths prints the $_ SERVER information of the current server.

Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/PHP%E7%9A%84%E5%86%85%E7%BD%AEWEB%E6%9C%8D%E5%8A%A1%E5%99%A8.php

Reference document: https://www.php.net/manual/zh/features.commandline.webserver.php

At this point, I believe you have a deeper understanding of "the use of PHP's built-in WEB server". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report