《PHP學(xué)習(xí):PHP模擬asp中response類實(shí)現(xiàn)方法》要點(diǎn):
本文介紹了PHP學(xué)習(xí):PHP模擬asp中response類實(shí)現(xiàn)方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP編程本文實(shí)例講述了PHP模擬asp中response類的辦法.分享給大家供大家參考.具體如下:
PHP編程習(xí)慣了asp或是asp.net開(kāi)發(fā)的人, 他們會(huì)經(jīng)常用到response類,這個(gè)類用于處理客戶端的響應(yīng),可以實(shí)現(xiàn)跳轉(zhuǎn),輸出等功能. 在php中沒(méi)有這個(gè)類,但是確實(shí)可以通過(guò)函數(shù)來(lái)模擬這個(gè)類.
PHP編程
/*
* 類用途: 實(shí)現(xiàn)類似于asp中的response功能
*/
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]);
}
}
public function redirect($url) {
header('Location: ' . $url);
exit;
}
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') !== FALSE)) {
$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);
}
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;
}
}
PHP編程希望本文所述對(duì)大家的php程序設(shè)計(jì)有所贊助.
歡迎參與《PHP學(xué)習(xí):PHP模擬asp中response類實(shí)現(xiàn)方法》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/8922.html