PHP模拟asp中response类实现方法
作者:Minho 时间:2024-06-05 09:39:32
本文实例讲述了PHP模拟asp中response类的方法。分享给大家供大家参考。具体如下:
习惯了asp或是asp.net开发的人, 他们会经常用到response类,这个类用于处理客户端的响应,可以实现跳转,输出等功能. 在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程序设计有所帮助。
标签:PHP,模拟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django项目在pycharm新建的步骤方法
2021-01-19 11:46:32
对Python3中列表乘以某一个数的示例详解
2023-05-05 03:10:40
一台linux主机启动多个MySQL数据库的方法
2024-01-14 00:46:00
![](https://img.aspxhome.com/file/2023/1/99501_0s.jpg)
PHP解决高并发问题(opcache)
2023-11-07 23:17:49
![](https://img.aspxhome.com/file/2023/3/62033_0s.png)
Python通过PIL获取图片主要颜色并和颜色库进行对比的方法
2022-06-05 13:51:35
微信小程序开发自定义tabBar实战案例(定制消息99+小红心)
2024-05-22 10:32:10
![](https://img.aspxhome.com/file/2023/7/123447_0s.gif)
JS定义函数的几种常用方法小结
2024-04-16 09:26:30
python 实现按对象传值
2023-05-26 14:20:18
![](https://img.aspxhome.com/file/2023/7/101637_0s.jpg)
9种python web 程序的部署方式小结
2021-04-14 10:39:44
通过javascript进行UTF-8编码的实现方法
2024-04-16 10:39:49
![](https://img.aspxhome.com/file/2023/8/136568_0s.jpg)
Flash的Fallback Content等
2010-04-01 12:18:00
SQL Server 2019下载与安装教程(自定义安装)
2024-01-25 07:15:47
![](https://img.aspxhome.com/file/2023/3/94433_0s.jpg)
debug case: onsubmit=return false依然能提交
2009-01-18 13:07:00
Python使用urlretrieve实现直接远程下载图片的示例代码
2022-11-10 16:54:33
![](https://img.aspxhome.com/file/2023/1/110421_0s.jpg)
mysql命令提示行连接乱码的解决
2024-01-14 11:05:20
javascript动画之模拟拖拽效果篇
2024-04-16 09:14:22
![](https://img.aspxhome.com/file/2023/1/136661_0s.gif)
详解Python相关文件常见的后缀名
2021-05-21 23:47:53
![](https://img.aspxhome.com/file/2023/8/101648_0s.png)
禁止拷贝网页内容的js代码
2024-04-25 13:07:50
Python爬虫获取op.gg英雄联盟英雄对位胜率的源码
2024-01-02 13:03:52
![](https://img.aspxhome.com/file/2023/3/83933_0s.png)
使用python opencv对目录下图片进行去重的方法
2023-07-06 20:04:13