PHP simulates the response class implementation method in asp. aspresponse_PHP tutorial

Source: Internet
Author: User
PHP simulates the response class implementation method in asp, aspresponse. PHP simulates the response class implementation method in asp. aspresponse this article describes how PHP simulates the response class in asp. Share it with you for your reference. Specifically: get used to asp or a PHP to simulate the response class implementation method in asp, aspresponse

This example describes how to simulate the response class in PHP asp. Share it with you for your reference. The details are as follows:

People who are used to asp or asp.net development often use the response class, which is used to process client responses and implement jump and output functions. this class does not exist in php, but it can be simulated using functions.

/** Usage: implements the response function similar to 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]) ;}} public function redirect ($ url) {header ('Location :'. $ url); exit;} public function setOutput ($ out Put, $ 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 ;}}

I hope this article will help you with php programming.

Examples in this article describes how PHP simulates the response class in asp. Share it with you for your reference. The specifics are as follows: get used to asp or...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.