首頁 > php教程 > PHP源码 > 主體

在PHP中模拟asp的response类

PHP中文网
發布: 2016-05-25 17:11:01
原創
947 人瀏覽過

php代码

/*  
* 作者: 雪狐博客  
* 类用途: 实现类似于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.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!