icy2003 /
php
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Class Header |
||
| 4 | * |
||
| 5 | * @link https://www.icy2003.com/ |
||
| 6 | * @author icy2003 <[email protected]> |
||
| 7 | * @copyright Copyright (c) 2017, icy2003 |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace icy2003\php\ihelpers; |
||
| 11 | |||
| 12 | use icy2003\php\I; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * 对 header 函数的一些封装 |
||
| 16 | */ |
||
| 17 | class Header |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * 发送原生 HTTP 头 |
||
| 22 | * |
||
| 23 | * - 兼容 Workerman 的 header 头 |
||
| 24 | * |
||
| 25 | * @param string $string 头字符串 |
||
| 26 | * @param boolean $replace 是否用后面的头替换前面相同类型的头,默认是 |
||
| 27 | * @param integer $httpResponseCode 强制指定HTTP响应的值,默认不强制 |
||
| 28 | * |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public static function send($string, $replace = true, $httpResponseCode = null) |
||
| 32 | { |
||
| 33 | if (method_exists('\Workerman\Protocols\Http', 'header')) { |
||
| 34 | call_user_func_array('\Workerman\Protocols\Http::header', [$string, $replace, $httpResponseCode]); |
||
| 35 | } else { |
||
| 36 | header($string, $replace, $httpResponseCode); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 正常访问 |
||
| 42 | * |
||
| 43 | * HTTP 返回 200 |
||
| 44 | * |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | public static function ok($info = 'OK') |
||
| 48 | { |
||
| 49 | self::send('HTTP/1.1 200 ' . $info); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 永久跳转 |
||
| 54 | * |
||
| 55 | * HTTP 返回 301 |
||
| 56 | * |
||
| 57 | * @param string $url 永久跳转的地址 |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public static function redirectPermanently($url, $info = 'Moved Permanently') |
||
| 62 | { |
||
| 63 | self::send('HTTP/1.1 301 ' . $info); |
||
| 64 | self::send('Location: ' . $url); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * 跳转到一个新的地址 |
||
| 69 | * |
||
| 70 | * HTTP 返回 302 |
||
| 71 | * |
||
| 72 | * @param string|null $url 新地址,如果不给这个值,表示刷新当前页面 |
||
| 73 | * @param integer $time 延迟时间,单位秒 |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public static function redirect($url = null, $time = 0, $info = 'Found') |
||
| 78 | { |
||
| 79 | null === $url && $url = ''; |
||
| 80 | if ($time < 0) { |
||
| 81 | throw new \Exception('time 参数不能小于 0 '); |
||
| 82 | } else { |
||
| 83 | self::send('HTTP/1.1 302 ' . $info); |
||
| 84 | self::send('Refresh: ' . $time . '; ' . $url); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 告诉浏览器文档内容没有发生改变 |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public static function notModified($info = 'Not Modified') |
||
| 94 | { |
||
| 95 | self::send('HTTP/1.1 304 ' . $info); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 禁止访问 |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public static function forbidden($info = 'Forbidden') |
||
| 104 | { |
||
| 105 | self::send('HTTP/1.1 403 ' . $info); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * 页面不存在 |
||
| 110 | * |
||
| 111 | * HTTP 返回 404 |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public static function notFound($info = 'Not Found') |
||
| 116 | { |
||
| 117 | self::send('HTTP/1.1 404 ' . $info); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 服务器错误 |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public static function serverError($info = 'Internal Server Error') |
||
| 126 | { |
||
| 127 | self::send('HTTP/1.1 500 ' . $info); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * 设置网页编码为 UTF-8 |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public static function utf8() |
||
| 136 | { |
||
| 137 | self::send('Content-Type: text/html; charset=utf-8'); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * 内容类型为 JSON |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public static function json() |
||
| 146 | { |
||
| 147 | self::send('Content-Type: application/json'); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * 内容类型为 XML |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public static function xml() |
||
| 156 | { |
||
| 157 | self::send('Content-Type: text/xml'); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * 设置可跨域的域名 |
||
| 162 | * |
||
| 163 | * @param array|string $urls 跨域域名列表,格式例如:http://127.0.0.1:8080 |
||
| 164 | * |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | public static function allowOrigin($urls = '*') |
||
| 168 | { |
||
| 169 | if ('*' === $urls) { |
||
| 170 | self::send('Access-Control-Allow-Origin:*'); |
||
| 171 | } else { |
||
| 172 | $origin = I::get($_SERVER, 'HTTP_ORIGIN'); |
||
| 173 | $urls = Strings::toArray($urls); |
||
| 174 | if (is_array($urls) && in_array($origin, $urls)) { |
||
| 175 | self::send('Access-Control-Allow-Origin:' . $origin); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | self::send('Access-Control-Max-Age:86400'); |
||
| 179 | self::send('Access-Control-Allow-Credentials:true'); |
||
| 180 | self::send('Access-Control-Allow-Methods:*'); |
||
| 181 | self::send('Access-Control-Allow-Headers:Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, Authorization, X-E4M-With'); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * 修改 X-Powered-By信息 |
||
| 186 | * |
||
| 187 | * @param string $string |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public static function xPoweredBy($string = 'icy2003') |
||
| 192 | { |
||
| 193 | self::send('X-Powered-By: ' . $string); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * 禁用缓存 |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public static function noCache() |
||
| 202 | { |
||
| 203 | self::send('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); |
||
| 204 | self::send('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 设置 X-Frame-Options 选项 |
||
| 209 | * |
||
| 210 | * - 部分浏览器可能不支持,@link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/X-Frame-Options |
||
| 211 | * |
||
| 212 | * @param boolean|string $asFrame 取值如下: |
||
| 213 | * - false:X-Frame-Options: deny 表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许 |
||
| 214 | * - true:X-Frame-Options: sameorigin 表示该页面可以在相同域名页面的 frame 中展示 |
||
| 215 | * - 字符串:X-Frame-Options: allow-from https://example.com/ 表示该页面可以在指定来源的 frame 中展示 |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public static function frame($asFrame = true) |
||
| 220 | { |
||
| 221 | if (true === $asFrame) { |
||
| 222 | self::send('X-Frame-Options: sameorigin'); |
||
| 223 | } elseif (false === $asFrame) { |
||
| 224 | self::send('X-Frame-Options: deny'); |
||
| 225 | } else { |
||
| 226 | $asFrame = (string) $asFrame; |
||
| 227 | self::send('X-Frame-Options: allow-from ' . $asFrame); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * 是否禁用 mine 嗅探 |
||
| 233 | * |
||
| 234 | * - 部分浏览器不支持,@link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options |
||
| 235 | * - 下面两种情况的请求将被阻止: |
||
| 236 | * 1. 请求类型是"style" 但是 MIME 类型不是 "text/css", |
||
| 237 | * 2. 请求类型是"script" 但是 MIME 类型不是 |
||
| 238 | * |
||
| 239 | * @param boolean $disabled 默认禁用 |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | public static function mimeSniffing($disabled = true) |
||
| 244 | { |
||
| 245 | if (true === $disabled) { |
||
| 246 | self::send('X-Content-Type-Options: nosniff'); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 |