


PHP 54 built-in web server web server ranking web server principle simple web server
PHP is a scripting language, which requires a PHP interpreter to analyze and run PHP files. When using PHP as a CGI to serve web requests, it needs to be embedded into some kind of web server, most commonly integrated into Apache or IIS. This means that before using PHP, you need to install Apache or IIS, and Correctly configure them and PHP integration parameters. Although this configuration has been standardized and the documentation is very rich, we still often encounter problems when installing Apache and PHP integration. Moreover, sometimes we just want to test a simple PHP feature and do not want to install and start the Apache service for this purpose. .
But according to the official documentation, this built-in web server is only for development and testing, and is not recommended for use in production environments. Because this server accepts and processes requests sequentially and cannot handle them concurrently.
This built-in web server is very convenient to use. You only need to execute the following command:
<ol><li><span><span>$ php -S localhost:8000 </span></span></li></ol>
Then you can access it. After starting in this way, the default web service directory is the current directory where the command is executed. If you do not want to use the current directory, you need to use the -t parameter to specify it.
Example #1 Start the Web server
<ol> <li><span><span>$ cd ~/public_html </span></span></li> <li><span>$ php -S localhost:8000 </span></li> </ol>
Terminal output information:
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html </span></li> <li><span>Press Ctrl-C to quit </span></li> </ol>
After requesting the http://localhost:8000/ and http://localhost:8000/myscript.html addresses, the terminal outputs information similar to the following :
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html </span></li> <li><span>Press Ctrl-C to quit. </span></li> <li><span>[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read </span></li> <li><span>[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read </span></li> <li><span>[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read </span></li> <li><span>[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read </span></li> <li><span>[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read </span></li> </ol>
Example #2 Specify the root directory of the document when starting the web server
<ol> <li><span><span>$ cd ~/public_html </span></span></li> <li><span>$ php -S localhost:8000 -t foo/ </span></li> </ol>
Terminal display message:
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html/foo </span></li> <li><span>Press Ctrl-C to quit </span></li> </ol>
If you append a php script file after the startup command line, then this file will be treated as a "router" script. This script will be responsible for all HTTP requests. If this script returns FALSE when executed, the requested resource will be returned normally. If it is not FALSE, the content generated by this script will be displayed in the browser.
Example #3 Using router script
In this example, a request for an image will return the corresponding image, but a request for an HTML file will display "Welcome to PHP":
<ol> <li><span><span><?php </span></span></span></li> <li> <span>// router.php </span><span> </span> </li> <li> <span>if</span><span> (preg_match(</span><span>'/\.(?:png|jpg|jpeg|gif)$/'</span><span>, </span><span>$_SERVER</span><span>[</span><span>"REQUEST_URI"</span><span>])) { </span> </li> <li> <span>return</span><span> false; </span><span>// serve the requested resource as-is. </span><span> </span> </li> <li> <span>} </span><span>else</span><span> { </span> </li> <li> <span>echo</span><span> </span><span>"<p>Welcome to PHP</p>"</span><span>; </span> </li> <li><span>} </span></li> <li><span>?> </span></li> </ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
Example #4 Determine whether it is being used The built-in web server
adjusts the different behaviors of the same PHP router script in the built-in web server and the production server through program judgment:
<ol> <li><span><span><?php </span></span></span></li> <li> <span>// router.php </span><span> </span> </li> <li> <span>if</span><span> (php_sapi_name() == </span><span>'cli-server'</span><span>) { </span> </li> <li> <span>/* route static assets and return false */</span><span> </span> </li> <li><span>} </span></li> <li> <span>/* go on with normal index.php operations */</span><span> </span> </li> <li><span>?> </span></li> </ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
This built-in web server can recognize some standard MIME type resources, and their extensions are : .css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, and .txt. Support for .htm and .svg extensions is only supported after PHP 5.4.4.
Example #5 Handling unsupported file types
If you want the web server to correctly handle unsupported MIME file types, do this:
<ol> <li><span><span><?php </span></span></span></li> <li> <span>// router.php </span><span> </span> </li> <li> <span>$path</span><span> = </span><span>pathinfo</span><span>(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]); </span> </li> <li> <span>if</span><span> (</span><span>$path</span><span>[</span><span>"extension"</span><span>] == </span><span>"ogg"</span><span>) { </span> </li> <li> <span>header(</span><span>"Content-Type: video/ogg"</span><span>); </span> </li> <li> <span>readfile(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]); </span> </li> <li><span>} </span></li> <li> <span>else</span><span> { </span> </li> <li> <span>return</span><span> FALSE; </span> </li> <li><span>} </span></li> <li><span>?> </span></li> </ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
If you want to access the built-in web server remotely, Your startup command needs to be changed to the following:
Example #6 Remote access to this built-in web server
<ol><li><span><span>$ php -S 0.0.0.0:8000 </span></span></li></ol>
So you can remotely access this built-in web server through port 8000
The above has introduced the PHP 54 built-in Web server, including the content of the Web server. I hope it will be helpful to friends who are interested in PHP tutorials.

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
