国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP模擬asp中response類實現方法

2024-05-04 23:38:36
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP模擬asp中response類的方法,可實現模擬ASP中response類處理客戶端響應的功能,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP模擬asp中response類的方法。分享給大家供大家參考。具體如下:

習慣了asp或是asp.net開發的人, 他們會經常用到response類,這個類用于處理客戶端的響應,可以實現跳轉,輸出等功能. 在php中沒有這個類,但是確實可以通過函數來模擬這個類.

 

 
  1. /*  
  2. * 類用途: 實現類似于asp中的response功能  
  3. */ 
  4. final class Response {  
  5. private $headers = array();  
  6. private $output;  
  7. private $level = 0;  
  8. public function addHeader($key$value) {  
  9. $this->headers[$key] = $value;  
  10. }  
  11. public function removeHeader($key) {  
  12. if (isset($this->headers[$key])) {  
  13. unset($this->headers[$key]);  
  14. }  
  15. }  
  16. public function redirect($url) {  
  17. header('Location: ' . $url);  
  18. exit;  
  19. }  
  20. public function setOutput($output$level = 0) {  
  21. $this->output = $output;  
  22. $this->level = $level;  
  23. }  
  24. private function compress($data$level = 0) {  
  25. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)) {  
  26. $encoding = 'gzip';  
  27. }  
  28. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE)) {  
  29. $encoding = 'x-gzip';  
  30. }  
  31. if (!isset($encoding)) {  
  32. return $data;  
  33. }  
  34. if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {  
  35. return $data;  
  36. }  
  37. if (headers_sent()) {  
  38. return $data;  
  39. }  
  40. if (connection_status()) {  
  41. return $data;  
  42. }  
  43. $this->addHeader('Content-Encoding'$encoding);  
  44. return gzencode($data, (int)$level);  
  45. }  
  46. public function output() {  
  47. if ($this->level) {  
  48. $ouput = $this->compress($this->output, $this->level);  
  49. else {  
  50. $ouput = $this->output;  
  51. }  
  52. if (!headers_sent()) {  
  53. foreach ($this->headers as $key => $value) {  
  54. header($key . ': ' . $value);  
  55. }  
  56. }  
  57. echo $ouput;  
  58. }  

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交口县| 肃北| 时尚| 舞钢市| 星座| 临猗县| 都匀市| 克东县| 汝州市| 舒城县| 永泰县| 望都县| 韩城市| 永修县| 望江县| 高唐县| 涞源县| 尉氏县| 内乡县| 育儿| 桂东县| 普兰店市| 南投县| 莱州市| 察隅县| 滕州市| 壤塘县| 聂拉木县| 萨迦县| 肃北| 荣昌县| 徐闻县| 平罗县| 镇原县| 仁怀市| 清丰县| 金秀| 张家港市| 千阳县| 谢通门县| 游戏|