PHP Reverse proxy class Code, PHP reverse proxy _php Tutorial

Source: Internet
Author: User

PHP Reverse proxy class Code, PHP reverse Proxy


Changed from PHP Reverse Proxy PRP, modified some errors in the original, support the file upload and upload file type recognition, support the designated IP, Adaptive SAE environment.

How to use

<?php$proxy=new phpreverseproxy (); $proxy->port= "8080"; $proxy->host= "www.jb51.net";//$proxy->ip= " 1.1.1.1 "; $proxy->forward_path="; $proxy->connect (); $proxy->output ();? >

Source

<?php//source code:http://www.xiumu.org/technology/php-reverse-proxy-class.shtmlclass PhpReverseProxy{Public $ Publicbaseurl; Public $outsideHeaders; Public $XRequestedWith; Public $sendPost; Public $port, $host, $ip, $content, $forward _path, $content _type, $user _agent, $XFF, $request _method, $IMS, $cacheTime, $ Cookies, $authorization; Private $http _code, $lastModified, $version, $resultHeader; Const CHUNKSIZE = 10000;  function __construct () {$this->version= "PHP Reverse Proxy (PRP) 1.0";  $this->port= "8080";  $this->host= "127.0.0.1";  $this->ip= "";  $this->content= "";  $this->forward_path= "";  $this->path= "";  $this->content_type= "";  $this->user_agent= "";  $this->http_code= "";  $this->xff= "";  $this->request_method= "GET";  $this->ims=false;  $this->cachetime=72000; $this->lastmodified=gmdate ("D, D M Y h:i:s", Time ()-72000). "  GMT ";  $this->cookie= "";  $this->xrequestedwith = ""; $this->authorization = "";} function TranslateUrl ($serverName{$this->path= $this->forward_path.$_server[' Request_uri ');  if (is_sae) return $this->translateserver ($serverName). $this->path;  if ($_server[' query_string ']== ") return $this->translateserver ($serverName). $this->path; else return $this->translateserver ($serverName). $this->path. "?". $_server[' query_string ']; } function Translateserver ($serverName) {$s = empty ($_server["HTTPS"])? ": ($_server[" HTTPS "] = =" on ")?  "s": "";  $protocol = $this->left (strtolower ($_server["Server_protocol"]), "/"). $s; if ($this->port== "") return $protocol. ":/  /". $serverName; else return $protocol. ":/ /". $serverName.": ". $this->port;} function left ($s 1, $s 2) {return substr ($s 1, 0, Strpos ($s 1, $s 2)), function Preconnect () {$this->user_agent=$_serve  r[' Http_user_agent '];  $this->request_method=$_server[' Request_method '];  $tempCookie = "";  foreach ($_cookie as $i = = $value) {$tempCookie = $tempCookie. "$i =$_cookie[$i];"; $this->cookie= $tempCOokie;  if (Empty ($_server[' http_x_forwarded_for ')) {$this->xff=$_server[' remote_addr '];  } else {$this->xff=$_server[' http_x_forwarded_for ']. ",". $_server[' REMOTE_ADDR '];   }} function connect () {if (Empty ($_server[' http_if_modified_since ')) {$this->preconnect ();   $ch =curl_init ();     if ($this->request_method== "POST") {curl_setopt ($ch, curlopt_post,1);    $postData = Array ();    $filePost = false;    $uploadPath = ' uploads/';     if (is_sae) $uploadPath = Sae_tmp_path; if (count ($_files) >0) {if (!is_writable ($uploadPath)) {die (' cannot upload to the specified directory, ple      ASE CHMOD it to 777. ');        } foreach ($_files as $key = = $fileArray) {copy ($fileArray ["Tmp_name"], $uploadPath. $fileArray ["name"]); $proxyLocation = "@". $uploadPath. $fileArray ["Name"]. "; Type=".        $fileArray ["type"];        $postData = Array ($key = = $proxyLocation);      $filePost = true; }} foreach ($_post as $key = $Value) {if (!is_array ($value)) {$postData [$key] = $value;      } else{$postData [$key] = serialize ($value);      }} if (! $filePost) {//$postData = Http_build_query ($postData);      $postString = "";      $firstLoop = true; foreach ($postData as $key = = $value) {$parameterItem = UrlEncode ($key). "      = ". UrlEncode ($value);      if ($firstLoop) {$postString. = $parameterItem;      } else{$postString. = "&". $parameterItem;       } $firstLoop = false;    } $postData = $postString;     }//echo Print_r ($postData);    curl_setopt ($ch, curlopt_verbose, 0);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0 (compatible;)");    $this->sendpost = $postData;    Var_dump (file_exists (Str_replace (' @ ', ' ', $postData [' Imgfile '])); exit;    curl_setopt ($ch, Curlopt_postfields, $postData);    curl_setopt ($ch, Curlopt_postfields,file_get_contents ($proxyLocation)); Curl_setopT ($ch, curlopt_postfields,file_get_contents ("Php://input")); }//gets rid of mulitple?   In URL $translateURL = $this->translateurl (($this->ip)? $this->ip: $this->host); if (Substr_count ($translateURL, "?")     >1) {$firstPos = Strpos ($translateURL, "?", 0);     $secondPos = Strpos ($translateURL, "?", $firstPos + 1);   $translateURL = substr ($translateURL, 0, $secondPos);    } curl_setopt ($ch, Curlopt_url, $translateURL); $proxyHeaders = Array ("X-forwarded-for:". $this->xff, "user-agent:". $this->user_agent, "Host:". $this-    >host);     if (strlen ($this->xrequestedwith) >1) {$proxyHeaders [] = "X-requested-with:". $this->xrequestedwith;   echo Print_r ($proxyHeaders);    } curl_setopt ($ch, Curlopt_httpheader, $proxyHeaders);   if ($this->cookie!= "") {curl_setopt ($ch, Curlopt_cookie, $this->cookie);    } curl_setopt ($ch, Curlopt_followlocation,false);    curl_setopt ($ch, curlopt_autoreferer,true); curl_setopt ($ch, CurloPt_header,true);    curl_setopt ($ch, curlopt_returntransfer,1);   $output =curl_exec ($ch);   $info = Curl_getinfo ($ch);   Curl_close ($ch);  $this->postconnect ($info, $output);   }else {$this->lastmodified=$_server[' http_if_modified_since ');  $this->ims=true;  }} function Postconnect ($info, $output) {$this->content_type= $info ["Content_Type"];  $this->http_code= $info [' Http_code '];  Var_dump ($info); exit;  if (!empty ($info [' last_modified '])) {$this->lastmodified= $info [' last_modified '];  } $this->resultheader=substr ($output, 0, $info [' header_size ']);   $content = substr ($output, $info [' header_size ']);  if ($this->http_code== ') {$this->content= $content; }elseif (($this->http_code== ' 302 ' | | $this->http_code== ' 301 ') && isset ($info [' Redirect_url '])) {$   Redirect_url = Str_replace ($this->host,$_server[' http_host '), $info [' Redirect_url ']); if (is_sae) $redirect _url = Str_replace (' http://fetchurl.sae.sina.com.cn/', ', $info [' Redirect_url ']);   Header ("Location: $redirect _url");  Exit   }elseif ($this->http_code== ' 404 ') {header ("http/1.1 404 Not Found");  Exit ("http/1.1 404 Not Found");   }elseif ($this->http_code== ') {header (' http/1.1 Internal Server Error ');  Exit ("http/1.1 Internal Server Error"); }else{exit ("http/1.1". $this->http_code. "  Internal Server Error ");  }} function output () {$currentTimeString =gmdate ("D, D M Y h:i:s", Time ());   $expiredTime =gmdate ("D, D M Y h:i:s", (Time () + $this->cachetime));  $doOriginalHeaders = true;     if ($doOriginalHeaders) {if ($this->ims) {header ("http/1.1 304 not Modified");     Header ("date:wed, $currentTimeString GMT");     Header ("Last-modified: $this->lastmodified");    Header ("Server: $this->version");     }else{header ("http/1.1 OK");     Header ("date:wed, $currentTimeString GMT");     Header ("Content-type:". $this->content_type);     Header ("Last-modified: $this->lastmodified"); Header ("Cache-control:max-age= $this->cachetime ");     Header ("Expires: $expiredTime GMT");     Header ("Server: $this->version");     Preg_match ("/set-cookie:[^\n]*/i", $this->resultheader, $result);     foreach ($result as $i = + $value) {header ($result [$i]);     } preg_match ("/content-encoding:[^\n]*/i", $this->resultheader, $result);     foreach ($result as $i = + $value) {//header ($result [$i]);     } preg_match ("/transfer-encoding:[^\n]*/i", $this->resultheader, $result);     foreach ($result as $i = + $value) {//header ($result [$i]);     } echo ($this->content);     /* IF (STRISTR ($this->content, "error")) {echo print_r ($this->sendpost); } */}} else{$headerString = $this->resultheader;//string $headerArray = explode ("\ n", $headerString    );    foreach ($headerArray as $privHeader) {header ($privHeader);   } if (Stristr ($headerString, "transfer-encoding:chunked")) {flush ();   Ob_flush ();   $i = 0; $maxLen = strlen ($this->content);    while ($i < $maxLen) {$endChar = $i + self::chunksize;     if ($endChar >= $maxLen) {$endChar = $maxLen-1;     } $chunk = substr ($this->content, $i, $endChar);     $this->dump_chunk ($chunk);     Flush ();     Ob_flush ();   $i = $i + $endChar;    }} else{Echo ($this->content);    }//echo "header:". Print_r ($headerArray);  Header ($this->resultheader);   }} function Dump_chunk ($chunk) {echo sprintf ("%x\r\n", strlen ($chunk));   Echo $chunk; echo "\ r \ n";   } function Getoutsideheaders () {$headers = array (); foreach ($_server as $name = = $value) {if (substr ($name, 0, 5) = = ' Http_ ') {$name = Str_replace (', '-', Ucword     S (Strtolower (Str_replace (' _ ', ' ', substr ($name, 5))));   $headers [$name] = $value;   }elseif ($name = = "Content_Type") {$headers ["content-type"] = $value;   }elseif ($name = = "Content_length") {$headers ["content-length"] = $value; }elseif (Stristr ($name, "X-requested-with")) {$headers ["X-requested-with"] = $value;  $this->xrequestedwith = $value;    }}//echo Print_r ($headers);   $this->outsideheaders = $headers; return $headers; }}?>

How does such a PHP agent write

Edit your own domain redirection code, such as create a new domain Access address home directory to create a new index.php file, then the file inside the code to get the domain name, and then use the judgment statement, if the domain name "www.a.com/sd.htm" Let It go to a directory on the server. or in the. htaccess settings, this setting is available in many frameworks, but some servers do not support this file or do not support some of the contents of the file, so that the site may not be used, specifically, how to try more.

PHP calls another class of methods, the following code can not be executed, why

a.php file
# Here we first introduce a storage file containing Class B.
Include ' b.php '
Class a{
public static function A1 () {
private static function A2 () {
Self:: $b = B::b ();
}
}
}
b.php file
Class b{
public static B () {
return ^^ ^^ ^;
}
}

http://www.bkjia.com/PHPjc/864478.html www.bkjia.com true http://www.bkjia.com/PHPjc/864478.html techarticle PHP Reverse proxy class Code, PHP reverse proxy modified from PHP Reverse proxy PRP, modified some errors in the original, support the file upload and upload file type recognition, support the designated IP, since ...

  • 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.