• PHP header() 函数


    [

    PHP header() 函数

    完整的 PHP HTTP 参考手册

    定义和用法

    header() 函数向客户端发送原始的 HTTP 报头。
    认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓冲来解决这个问题):

    <html>

    <?php

    // This results in an error.

    // The output above is before the header() call

    header('Location: http://www.example.com/');

    ?>

    语法

    header(string,replace,http_response_code)

    参数 描述
    string 必需。规定要发送的报头字符串。
    replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。默认是 TRUE(替换)。FALSE(允许相同类型的多个报头)。
    http_response_code 可选。把 HTTP 响应代码强制为指定的值。(PHP 4.3 以及更高版本可用)

    提示和注释

    注释:从 PHP 4.4 之后,该函数防止一次发送多个报头。这是对头部注入攻击的保护措施。

    实例 1

    禁用页面缓存:

    <?php

    // Date in the past

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    header("Cache-Control: no-cache");

    header("Pragma: no-cache");

    ?>

    <html>

    <body>

    ...

    ...

    注释:用户可能会设置一些选项来更改浏览器的默认缓存设置。通过发送上面的报头,您可以覆盖任何这些设置,强制浏览器不进行缓存!

    实例 2

    提示用户保存一个生成的 PDF 文件(Content-Disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框):

    <?php

    header("Content-type:application/pdf");

    // It will be called downloaded.pdf

    header("Content-Disposition:attachment;filename='downloaded.pdf'");

    // The PDF source is in original.pdf

    readfile("original.pdf");

    ?>

    <html>

    <body>

    ...

    ...

    注释:微软 IE 5.5 存在一个阻止以上机制的 bug。通过升级为 Service Pack 2 或更高的版本,可以解决该 bug。

    完整的 PHP HTTP 参考手册

    ]
    转载请保留页面地址:https://www.breakyizhan.com/php/28697.html
  • 相关阅读:
    UVA 12545 Bits Equalizer
    UVA 1610 Party Games
    UVA 1149 Bin Packing
    UVA 1607 Gates
    UVA 12627 Erratic Expansion
    UVA10562-Undraw the Trees(递归)
    UVA10129-Play on Words(欧拉路径)
    UVA816-Abbott's Revenge(搜索进阶)
    UVA1103-Ancient Messages(脑洞+dfs)
    UVA839-Not so Mobile
  • 原文地址:https://www.cnblogs.com/breakyizhan/p/13252755.html
Copyright © 2020-2023  润新知