_php Techniques for Response class implementation in PHP simulation ASP

Source: Internet
Author: User

This article illustrates the method of response class in PHP simulation ASP. Share to everyone for your reference. Specifically as follows:

Accustomed to the ASP or ASP.net developers, they will often use the response class, this class is used to handle the client's response, can achieve jump, output and other functions. There is no such class in PHP, but you can actually simulate this class by using a function.

* * Class use: To achieve similar to the Response function of ASP/FINAL class Response {private $headers = array (); 
  Private $output; 
  Private $level = 0; 
  Public Function AddHeader ($key, $value) {$this->headers[$key] = $value; Public Function Removeheader ($key) {if (Isset ($this->headers[$key))) {unset ($this->headers[$key]) 
    ; 
    The public Function redirect ($url) {header (' Location: '. $url); 
  Exit 
    The Public Function setoutput ($output, $level = 0) {$this->output = $output; 
  $this->level = $level; Private Function Compress ($data, $level = 0) {if (Isset ($_server[' http_accept_encoding ')) && Strpos ($_ 
    server[' http_accept_encoding '], ' gzip ')!== FALSE) {$encoding = ' gzip '; if (Isset ($_server[' http_accept_encoding ')) && (Strpos ($_server[' http_accept_encoding '], ' x-gzip ')!== FAL 
    SE)) {$encoding = ' x-gzip '; 
    } if (!isset ($encoding)) {return $data; } IF (!extension_loaded (' zlib ') | | | ini_get (' zlib.output_compression ')) {return $data; 
    } if (Headers_sent ()) {return $data; 
    } if (Connection_status ()) {return $data; 
    } $this->addheader (' content-encoding ', $encoding); 
  Return Gzencode ($data, (int) $level); The Public function output () {if ($this->level) {$ouput = $this->compress ($this->output, $this 
    ; level); 
    else {$ouput = $this->output; } if (!headers_sent ()) {foreach ($this->headers as $key => $value) {header ($key. ': ' . 
      $value); 
  } Echo $ouput;

 } 
}

I hope this article will help you with your PHP programming.

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.