PHP header 函数的用法及其注意事项

void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) : Send a raw HTTP header

下面有一些使用header的几种用法:
1、使用header函数进行跳转页面;
  header('Location:'.$url);
  其中$url就是将要跳转的url了。
  这种用法的注意事项有以下几点:

  • Location和":"之间不能有空格,否则会出现错误(注释:我刚测试了,在我本地环境下,没有跳转页面,但是也没有报错,不清楚什么原因);
  • 在用header前不能有任何的输出(注释:这点大家都知道的,如果header之前有任何的输出,包括空白,就会出现header already sent by xxx的错误);
  • header 后面的东西还会执行的;

2、使用header声明content-type
  header('content-type:text/html;charset=utf-8');
  这个没有什么好说的;
3、使用header返回response 状态码
  header(sprintf('%s %d %s', $http_version, $status_code, $description));
  样式就是这样的;
  例如:header('HTTP/1.1 404 Not Found');
4、使用header在某个时间后执行跳转
  header("Refresh: {$delay}; url={$url}");
  其中$delay就是推迟跳转的时间,$url为需要跳转的url
  例如:header('Refresh: 10; url=http://www.example.org/'); 意思为10s后跳转到http://www.eexample.org这个网站
5、使用header控制浏览器缓存
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
  header("Cache-Control: no-cache, must-revalidate");
  header("Pragma: no-cache");
6、执行http验证
  header('HTTP/1.1 401 Unauthorized');
  header('WWW-Authenticate: Basic realm="Top Secret"');
7、使用header进行下载操作
  header('Content-Type: application/octet-stream');//设置内容类型
  header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开
  header('Content-Transfer-Encoding: binary');//设置传输方式
  header('Content-Length: '.filesize('example.zip'));//设置内容长度
  // load the file to send:
  readfile('example.zip');//读取需要下载的文件

PHP中的header()函数的更多相关文章

  1. PHP中的header()函数作用

    PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...

  2. PHP中小小的header函数

    不废话,直接说功能 1.重定向,语法: header("location:http://www.lemon-x.ga"); file_put_contents("./te ...

  3. PHP中header函数的用法及其注意重点是什么呢

    1.使用header函数进行跳转页面: header('Location:'.$url); 其中$url就是将要跳转的url了. 这种用法的注意事项有以下几点: •Location和":&q ...

  4. PHP header函数设置http报文头(设置头部域)

    PHP HTTP 简介: HTTP 函数允许您在其他输出被发送之前,对由 Web 服务器发送到浏览器的信息进行操作. PHP 5 HTTP 函数:header()     向客户端发送原始的 HTTP ...

  5. PHP header函数使用大全

    PHP header函数大全 header('Content-Type: text/html; charset=utf-8'); header('Location: http://52php.cnbl ...

  6. PHP中ob系列函数整理

    ob,输出缓冲区,是output buffering的简称,而不是output cache.ob用对了,是能对速度有一定的帮助,但是盲目的加上ob函数,只会增加CPU额外的负担. 下面我说说ob的基本 ...

  7. PHP header()函数

    对header函数,我用得最多的就是跳转页面和设置字符集,其他的功能用得比较少. 一.设置字符集 其实我们用的最多的在在html代码当中的<meta>标签里面设置字符集.格式如下: < ...

  8. PHP header() 函数详细说明(301、404等错误设置)

    原文来自:http://www.veryhuo.com/a/view/41466.html 如果您刚刚开始学习PHP,可能有许多函数需要研究,今天我们就来学习一下PHP Header()的使用方法,更 ...

  9. PHP header函数大全

    PHP header函数大全 header('Content-Type: text/html; charset=utf-8'); header('Location: http://www.php-no ...

随机推荐

  1. mac port选择使用的python的版本

    To list: port select --list python To show: port select --show python To select: sudo port select -- ...

  2. Service Fabric 群集在Service Replica过多的情况下报错问题

    首先 Service Fabric 群集是正常的,部署一些服务过后也能正常运行,但一旦部署的服务过多后,且每个服务不止一个Partition,就有可能让群集状态为Error,但其实服务还是在正常运行的 ...

  3. linux通配符与基础正则

    1.特殊符号: ' '     单引号          单引号的内容     写什么就是什么,不会被当成特殊字符.       单引号实例:      [root@oldboyedu-sh01 ~] ...

  4. 「美团 CodeM 初赛 Round A」最长树链

    题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...

  5. boostrap selectpicker 用法

    1..html中先引用 <link href="{{ url_for('static', filename='css/bootstrap-select.css') }}" r ...

  6. RedHat6安装git

    通过yum安装git : 一. 先配置yum: 把redhat系统镜像加载到电脑光驱中(无光驱可用u盘),然后把该镜像配置到环境变量中 文件名不限 在此新建的RHEL_6文件中添加如下内容 其中bas ...

  7. idea 面板介绍

    一.面板说明 IDEA面板的全貌如下图 二.菜单栏 下面会简单介绍下一些常用的部分菜单使用,如有疑问或补充欢迎留言. (1).File文件 1. New:新建一个工程 可以新建project,导入已存 ...

  8. SEVERE: One or more listeners failed to start.

    Full details will be found in the appropriate container log file 错误日志在/home/dela/.IntelliJIdea2017.1 ...

  9. 13、OpenCV Python canny边缘提取

    __author__ = "WSX" import cv2 as cv import numpy as np def lapalian_demo(image): #拉普拉斯算子 # ...

  10. 2019.2.25考试T3, 离线+线段树

    \(\color{#0066ff}{题解}\) #include<bits/stdc++.h> #define LL long long LL in() { char ch; LL x = ...