This article mainly introduces PHP implementation of Dynamic Web server methods, interested in the friend's reference, I hope to be helpful to everyone.
If you are a Web server, you need to know about how the Web server works. Start with a static text server to access the 1.html of the Web server as an example
1. The client sends an HTTP request to the server, if the server listens on the port number is 9002, then the native Self test access to the address is http://localhost:9002/1.html .
2. The server listens on port 9002, then after receiving the request request, it can obtain the location of the URI resource in the Web directory from the HTTP head header that needs to be accessed in the request.
3. The server reads the resource files that need to be accessed, and then populates the entities in the HTTP returned to the client.
As follows:
Php<?phpclass Web_config {//Monitor port number const PORT = 9003;//project root directory const WEB_ROOT = "/users/zhoumengkang/documents/html"; }class server {private $ip; private $port; public function __construct ($IP, $port) {$this->ip = $ip; $this->port = $port; $this->await (); } Private function await () {$sock = Socket_create (Af_inet, Sock_stream, sol_tcp), if ($sock < 0) {echo "Error:". Socket_strerror (Socket_last_error ()). "\ n"; } $ret = Socket_bind ($sock, $this->ip, $this->port); if (! $ret) {echo "BIND FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; Exit } echo "ok\n"; $ret = Socket_listen ($sock); if ($ret < 0) {echo "LISTEN FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; } do {$new _sock = null, try {$new _sock = socket_accept ($sock);} catch (Exception $e) {echo $e->getmessage (); echo " ACCEPT FAILED: ". Socket_strerror (Socket_last_error ()). "\ n"; try {$request _string = Socket_read ($new _sock, 1024x768), $response = $this->output ($request _string);Socket_write ($new _sock, $response); Socket_close ($new _sock); } catch (Exception $e) {echo $e->getmessage (); echo "READ FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; }} while (TRUE); }/** * @param $request _string * @return String */Private Function output ($request _string) {//static get/1.html http/1.1: . $request _array = Explode ("", $request _string); if (count ($request _array) < 2) {return $this->not_found ();} $uri = $request _array[1]; $filename = Web_config::web_root. $uri; echo "Request:" $filename. " \ n "; The processing of the static file if (File_exists ($filename)) {return $this->add_header (file_get_contents ($filename));} else {return $this ->not_found (); }}/** * 404 returns * @return String */Private Function Not_found () {$content = "
The code has been uploaded to GitHub https://github.com/zhoumengkang/php/tree/master/php-webserver/static
As stated in the code above, a static Web server is started as long as the file is executed at the terminal.
For me to access the 1.jpg files in my web directory
The simple static Web server has been completed, the following question is how to let it support the output of dynamic content. Is it necessary to return the resulting results to the client after executing a program inside the Web server? But so the code of the Web server is coupled with the business code, how to solve a Web server, can be used in various business scenarios?
The advent of CGI solves this problem. So what is CGI? The following words are copied:
CGI is an interface standard between an external application (CGI program) and a Web server, and is a discipline for passing information between CGI programs and Web servers. The CGI specification allows the Web server to execute external programs and send their output to a Web browser, CGI turning a simple set of static hypermedia documents from the Web into a complete new interactive media.
So faint, for example, the global variables of the PHP we are using $_SERVER['QUERY_STRING'] are the WEB servers that pass over the CGI protocol. For example in Nginx, perhaps you remember such a fastcgi configuration
The code is as follows:
Fastcgi_param query_string $query _string;
Yes, Nginx passed its global variables $query_string to the FASTCGI_PARAM environment variable.
We also use CGI as a QUERY_STRING bridge to pass information from the URI of the client request to the CGI program. The putenv QUERY_STRING environment variable in which the request is deposited.
We agreed that the resources accessed in the WEB server are .cgi suffixes, which means dynamic access, which is a bit like configuring location in Nginx to look for a PHP scripting program. is a rule that checks whether a CGI program should be requested. In order to distinguish it from the WEB server, I wrote a CGI program to query the user's information in C and query the user data according to the user ID.
Approximate access logic, such as
If you want to run the demo, do the following:
1. Changes config.php in the project root directoryWEB_ROOT
2. Compile cgi-demo\user.c and compile the command gcc -o user.cgi user.c , then user.cgi put the file under the project root directory you configured
3. Execute at the terminal php start.php so that the Web server starts the
4. By http://localhost:9003/user.cgi?id=1 you can see the following effect
In fact, just on the basis of a static server to do some CGI judgment is the request of forwarding processing, three files on GitHub to merge the code into a file for everyone to watch
Php<?phpclass Web_config {//Monitor port number const PORT = 9003;//project root directory const WEB_ROOT = "/users/zhoumengkang/documents/html"; The system supports CGI programs with the file extension const Cgi_extension = "CGI";} Class Server {private $ip; private $port; public function __construct ($IP, $port) {$this->ip = $ip; $this->port = $port; $this->await (); } Private function await () {$sock = Socket_create (Af_inet, Sock_stream, sol_tcp), if ($sock < 0) {echo "Error:". Socket_strerror (Socket_last_error ()). "\ n"; } $ret = Socket_bind ($sock, $this->ip, $this->port); if (! $ret) {echo "BIND FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; Exit } echo "ok\n"; $ret = Socket_listen ($sock); if ($ret < 0) {echo "LISTEN FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; } do {$new _sock = null, try {$new _sock = socket_accept ($sock);} catch (Exception $e) {echo $e->getmessage (); echo " ACCEPT FAILED: ". Socket_strerror (Socket_last_error ()). "\ n"; try {$request _string = Socket_read ($new _sock, 1024); $response = $this->output ($request _string); Socket_write ($new _sock, $response); Socket_close ($new _sock); } catch (Exception $e) {echo $e->getmessage (); echo "READ FAILED:". Socket_strerror (Socket_last_error ()). "\ n"; }} while (TRUE); }/** * @param $request _string * @return String */Private Function output ($request _string) {//static get/1.html http/1.1: . Dynamic get/user.cgi?id=1 http/1.1 ... $request _array = Explode ("", $request _string); if (count ($request _array) < 2) {return "";} $uri = $request _array[1]; echo "Request:". Web_config::web_root. $uri. " \ n "; $query _string = null; if ($uri = = "/favicon.ico") {return "";} if (Strpos ($uri, "?")) {$URIARR = explode ("?", $uri); $uri = $URIARR [0]; $query _string = Isset ($uriArr [1])? $URIARR [1]: null;} $filename = Web_config::web_root. $uri; if ($this->cgi_check ($uri)) {$this->set_env ($query _string); $handle = Popen (web_config::web_root. $uri, "R"); $ Read = Stream_get_contents ($handle); Pclose ($handle); Return $this->add_header ($read); }//The processing of the static file if (File_exists ($filename)) {return $this->add_header (file_get_contents ($filename));} else {return $thi S->not_found (); }}/** * Set environment variables for CGI programs using * @param $query _string * @return BOOL */Private Function set_env ($query _string) {if ($query _st Ring = = null) {return false;} if (Strpos ($query _string, "=")) {putenv ("query_string=". $query _string);}} /** * Determines whether the requested URI is a legitimate CGI resource * @param $uri * @return BOOL */Private Function Cgi_check ($uri) {$info = PathInfo ($uri); $e XTension = Isset ($info ["extension"])? $info ["extension"]: null; if ($extension && in_array ($extension, Explode (",", Web_config::cgi_extension))) {return true;} return false; }/** * 404 returns * @return String */Private Function Not_found () {$content = "
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
The function of using curl to forge IP in PHP
PHP using the Magickwand module to manipulate the image to add a watermark method
Smarty definition and usage skills of custom resources