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.