PHP常用header函数

1.实现重定向(状态码302)

<?php
header(”Location: http://www.baidu.com”);
exit;

2.刷新页面

header("refresh:2;url=http://www.baidu.com");

3.页面不存在(404页面)

<?php 
header('HTTP/1.1 404 Not Found'); 
header("status: 404 Not Found"); 

4.永久重定向(状态码301)

<?
Header( "HTTP/1.1 301 Moved Permanently" ) ;
Header( "Location: www.baidu.com" );

5.下载文件

//返回的文件
header("Content-type: application/octet-stream");
//按照字节大小返回
header("Accept-Ranges: bytes");
//返回文件大小
header("Accept-Length: $file_size");
//这里客户端的弹出对话框,对应的文件名
header("Content-Disposition: attachment; filename=".$file_name);

6.文档语言

<?
header('Content-language: en');

7.设置文档类型

header('Content-Type: text/html; charset=utf-8');//utf-8格式html
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-shockwave-flash'); //Flash动画

8.设置文档长度

<?
header('Content-Length: 1024');

9.禁用缓存

<?
header("Cache-Control:no-cache,must-revalidate,no-store");
header("Pragma:no-cache");
header("Expires:-1");
header ( " Last-Modified:" . gmdate ( " D, d M Y H:i:s " ). "GMT " );
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • API定义规范 本规范设计基于如下使用场景: 请求频率不是非常高:如果产品的使用周期内请求频率非常高,建议使用双通...
    有涯逐无涯阅读 2,776评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,155评论 19 139
  • 1. 网络基础TCP/IP HTTP基于TCP/IP协议族,HTTP属于它内部的一个子集。 把互联网相关联的协议集...
    yozosann阅读 3,485评论 0 20
  • 一、概念(载录于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434阅读 8,535评论 6 152
  • 自己看过的电影 觉得不错 推荐1 阿甘正传 http://pan.baidu.com/share/link?sha...
    小学生大学生阅读 122评论 0 1