Home > Backend Development > PHP Tutorial > PHP simulates the method of response class in asp

PHP simulates the method of response class in asp

墨辰丷
Release: 2023-03-30 14:56:01
Original
1686 people have browsed it

This article mainly introduces the method of simulating the response class in ASP in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

People who are used to asp or asp.net development will often use the response class. This class is used to process the client's response and can implement functions such as jump and output. There is no such class in PHP , but it is indeed possible to simulate this class through functions.

/* 
* 类用途: 实现类似于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; 
  } 
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

Detailed explanation of the definition and usage of PHP paging class

php implements generating random strings Method

php file upload class sharing_php instance

The above is the detailed content of PHP simulates the method of response class in asp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template