用header函数设置信息的几种

本文详细介绍了PHP中如何通过响应头进行各种操作,包括设置编码、重定向、文档语言、文件下载及缓存控制等。
/定义编码
header( 'Content-Type:text/html;charset=utf-8 ');

// ok
header('HTTP/1.1 200 OK');

//设置一个404头:
header('HTTP/1.1 404 Not Found');

//设置地址被永久的重定向
header('HTTP/1.1 301 Moved Permanently');

//转到一个新地址
header('Location: http://www.example.org/');

//文件延迟转向:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';

//当然,也可以使用html语法实现
// <meta http-equiv="refresh" content="10;http://www.example.org/ />

// override X-Powered-By: PHP:
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

//文档语言
header('Content-language: en');

//告诉浏览器最后一次修改时间
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

//告诉浏览器文档内容没有发生改变
header('HTTP/1.1 304 Not Modified');

//设置内容长度
header('Content-Length: 1234');

//设置为一个下载类型
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// 对当前文档禁用缓存
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

//设置内容类型:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); //纯文本格式
header('Content-Type: image/jpeg'); //JPG***
header('Content-Type: application/zip'); // ZIP文件
header('Content-Type: application/pdf'); // PDF文件
header('Content-Type: audio/mpeg'); // 音频文件
header('Content-Type: application/x-shockw**e-flash'); //Flash动画

//显示登陆对话框
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
在不同的开发场景中,API公共函数的使用方法各有不同。 在Uniapp里,可在`common`文件夹下新建公共方法文件,例如`public.js`,在其中定义公共函数。以下是一个字符串省略函数的示例: ```javascript export default { /* * 字符串过程省略的函数 * str 字符串 * num 允许不被省略的长度 */ string_part_omit(str, num) { if (str.length > num) { str = str.substring(0, num) + "..."; } return str; } } ``` 使用时引入该文件即可调用相应函数[^1]。 在Vue开发里,可在`common.js`文件中定义公共函数,如`toast`和`myRequest`: ```javascript function toast(title = '', icon = 'none') { uni.showToast({ title: title, icon: icon }); } function myRequest(url = "", data = {}, method = 'POST') { return new Promise((res, rej) => { uni.request({ url: this.$apiUrl + url, method: method, header: { "Xshop-Token": uni.getStorageSync("zhsgToken") }, data: data, success(data) { res(data) }, fail() { rej() } }) }) } export { toast, myRequest } ``` 使用时按需引入这两个函数进行调用[^3]。 在Laravel中,可通过编写自定义公共函数。在`Helper.php`文件中可以定义静态和非静态公共函数,例如: ```php <?php use app\Helper; /** * Created by PhpStorm. * User: zwx * Date: 2019/3/14 * Time: 15:10 * 自定义的静态公共函数 */ class Helper { public static function hello() { // 自己的逻辑代码 } } /** * Created by PhpStorm. * User: zwx * Date: 2019/3/14 * Time: 15:10 * 自定义非静态公共函数 */ class Helper { public function helper() { // 自己的逻辑 } } ``` 在需要使用这些函数的地方,根据函数的静态与否进行相应调用[^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值