php header方法 修改传输头

本文介绍了如何使用PHP操作HTTP响应头来实现文本编码设置、页面重定向、延迟跳转、禁用缓存及文件下载等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改文本编码

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. header("Content-Type: text/html;charset=utf-8");  


重定向

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // header("Location:a.php");  
  2. header("Location:http://www.126.com");  


多少秒后跳转

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. header("Refresh:5;url='http://www.126.com'");  


缓存

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //不启用缓存 多个配置 适配不同的浏览器  
  2. header("Expires:-1");  
  3. header("Cache-Control:no-cache");  
  4. header("Pragma:no-cache");  


文件下载

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. //在此页面中不能调用echo等方法在页面显示内容,否则下载的内容就是错误的  
  3. $str = "我的.jpg";  
  4. //中文的文件名 php文件系统不认,须转换  
  5. $str=iconv('UTF-8','GB2312',$str);  
  6. if(!file_exists($str)){  
  7.     echo "找不到文件:".$str;  
  8.     return;  
  9. }  
  10. $fp = fopen($str"r");  
  11. $file_size = filesize($str);  
  12. while (! feof($fp)) {  
  13.     $read = fread($fp, 1024);  
  14.     echo $read;  
  15. }  
  16. fclose($fp);  
  17. // 说明返回的是文件  
  18. header("Content-Type:application/octet-stream");  
  19. // 按字节大小返回  
  20. header("Accept-Range:bytes");  
  21. // 文件有多大  
  22. header("Content-Length:" . $file_size);  
  23. // 文件名 弹出框的名称  
  24. header("Content-Disposition:attachment; filename=" . $str);  
  25.   
  26. ?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值