How to use PHP to implement a dynamic Web server _php instance

Source: Internet
Author: User

If a Web server is real, then you need to know how the Web server works. Start with a static text server to access the Web server 1.html for example

1. The client sends an HTTP request to the server, and if the server listens for a port number of 9002, then the address on the local test access is http://localhost:9002/1.html.

2. The server listens for 9002 ports, and after receiving a request, it is able to obtain the location of the URI resource in the Web directory that needs to be accessed in the request from the HTTP head header.

3. The server reads the resource file that needs to be accessed, and then populates the HTTP entity to return it to the client.

The schematic diagram is as follows:

<?php class Web_config {//Listener 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, 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 ... $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 static files if (File_exists ($filename)) {return $this->add_header ($filename));
    else {return $this->not_found (); }/** * 404 returns * @reTurn String */Private Function Not_found () {$content = " 

As mentioned in the code above, as long as the file is executed at the terminal, a static Web server is started.

The following image gives me a screenshot of the 1.jpg files in my web directory

A simple static Web server has been completed, and the following question is how to make it support the output of dynamic content. Is it only necessary to finish a program inside the Web server and return the results to the client? However, the Web server's code is coupled with the business code, how to solve a Web server, can be used in various business scenarios?

The presence of CGI solves the problem. So what is a CGI? The following passage copies:

CGI is the interface standard between an external application (CGI program) and a Web server, and is a procedure for transferring information between a CGI program and a Web server. The CGI specification allows Web servers to execute external programs and send their output to a Web browser, and CGI turns a simple set of static hypermedia documents on the Web into a complete new interactive medium.

Good halo, for example, we are using the global variable $_server[' query_string ' is the WEB server through the CGI protocol, passed over. For example in Nginx, perhaps you remember such a fastcgi configuration

Fastcgi_param query_string $query _string;

Yes, Nginx has passed its global variable $query_string to the FASTCGI_PARAM environment variable.

Below we also use CGI query_string as a bridge to pass information from the URI requested by the client to the CGI program. The query_string is deposited into the environment variable of the request by putenv.

We agreed that the resource accessed in the Web server is a. CGI suffix means dynamic access, which is somewhat similar to the Nginx configuration location to look for PHP scripts. is a rule that checks whether a CGI program should be requested. To differentiate it from the WEB server, I wrote a CGI program that queried user information in C, and queried user information based on the user ID.

The approximate access logic is shown below

Demo Code Address: https://github.com/zhoumengkang/php/tree/master/php-webserver/dynamic

If you want to run this demo you need to do the following

1. Modify the project root directory in config.php web_root

2. Compile the cgi-demo\user.c, compile the command gcc-o user.cgi user.c, and then put the user.cgi file under your configured project root directory

3. Execute PHP start.php at the terminal so that the Web server starts

4. Through the http://localhost:9003/user.cgi?id=1 can be accessed to see the following effect

In fact, only on the basis of static server to do some CGI judgment is the request of forwarding processing, the github of the three files on the code merged into a file to facilitate everyone to watch

<?php class Web_config {//Listener port number Const Port = 9003;

  Project root directory Const WEB_ROOT = "/users/zhoumengkang/documents/html";

The system-supported CGI program's file name 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);
    }//static file processing if (file_exists ($filename)) {return $this->add_header ($filename));
    else {return $this->not_found (); }/** * Set environment variable to CGI program using * @param $query _string * @return bool/Private Function set_env ($query _str
    ing) {if ($query _string = null) {return false;
    } if (Strpos ($query _string, "=")) {putenv ("query_string=". $query _string);
    }/** * To determine whether the requested URI is a valid CGI resource * @param $uri * @return bool/Private Function Cgi_check ($uri) {
    $info = PathInfo ($uri); $extension = 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 = " 

The above is the implementation of a Dynamic Web server PHP all the process, I hope to help you learn.

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.