http://hosting.chinaon.com/hosting/school/php4/function.php-header.htm

解决方案 »

  1.   

    header
    (PHP 3, PHP 4 )header -- Send a raw HTTP header
    Description
    int header ( string string [, bool replace [, int http_reponse_code]])
    header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers. The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type. For example: 
    header('WWW-Authenticate: Negotiate');
    header('WWW-Authenticate: NTLM', FALSE);
     The second optional http_response_code force the HTTP response code to the specified value. (This parameter is available in PHP 4.3.0 and higher.) There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code. 
    <?php
      header("HTTP/1.0 404 Not Found");
    ?>
     
    注: The HTTP status header line will always be the first sent to the client, regardless of the actual header() call beeing the first or not. The status may be overridden by calling header() with a new status line at any time unless the HTTP headers have already been sent. 注: In PHP 3, this only works when PHP is compiled as an Apache module. You can achieve the same effect using the Status header. 
    header("Status: 404 Not Found");
     
    The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless some 3xx status code has already been set. 
    header("Location: http://www.example.com/"); /* Redirect browser */
    exit;                 /* Make sure that code below does 
                             not get executed when we redirect. */
     注: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself: 
    header("Location: http://".$_SERVER['HTTP_HOST']
                          .dirname($_SERVER['PHP_SELF'])
                          ."/".$relative_url);
     PHP scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                                                         // always modified
    header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");                          // HTTP/1.0
     
    注: You may find that your pages aren't cached even if you don't output all of the headers above. There are a number of options that users may be able to set for their browser that change its default caching behavior. By sending the headers above, you should override any settings that may otherwise cause the output of your script to be cached. Additionally, session_cache_limiter() and the session.cache_limiter configuration setting can be used to automatically generate the correct caching-related headers when sessions are being used. 
    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. 
    <?php header ("Content-type: audio/x-pn-realaudio"); ?>
    // Broken, note the blank line above
     
    注: In PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files. 
    If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the Content-Disposition header to supply a recommended filename and force the browser to display the save dialog. 
    <?php
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=downloaded.pdf");/* ... output pdf file ... */
     
    注: There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later. 
    注: If safe mode is enabled the uid of the script is added to the realm part of the WWW-Authenticate header if you set this header (used for HTTP Authentication). See also headers_sent(), setcookie(), and the section on HTTP authentication.  
     
      

  2.   


    --------------------------------------------------------------------------------
     函数:header() 
    --------------------------------------------------------------------------------
     
    HTTP 相关函数库
    header
    送出 HTTP 协议的标识到浏览器语法: int header(string string);返回值: 整数函数类型: 网络系统
     
     
    内容说明 
    标识 (header) 是服务器以 HTTP 协议传 HTML 数据到浏览器前所送出的字符串,在标识与 HTML 文件之间尚需空一行分隔。有关 HTTP 的详细说明,可以参考其它的相关书籍或更详细的 RFC 2068 官方文件(http://www.w3.org/Protocols/rfc2068/rfc2068)。在 PHP 中送回 HTML 数据前,需先传完所有的标识。 注意: 传统的标识一定包含下面三种标识之一,并只能出现一次。Content-Type: xxxx/yyyy
    Location: xxxx:yyyy/zzzz
    Status: nnn xxxxxx
    在新的多型标识规格 (Multipart MIME) 方可以出现二次以上。
     
     
    使用范例 
    范例一: 本例用来重定向使用者到 PHP 的官方网站。
    <?php
    Header("Location: http://www.php.net");  
    exit;
    ?>范例二: 欲让使用者每次都能得到最新的数据,而不是 Proxy 或 cache 中的数据,可以使用下列的标识
    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");范例三: 让使用者的浏览器出现找不到文件的讯息。
    <?php
    header("Status: 404 Not Found");
    ?>范例四: [email protected] (28-Apr-1999) 提供让使用者下载文件的范例。
    header("Content-type: application/x-gzip");
    header("Content-Disposition: attachment; filename=some-file.tar.gz");
    header("Content-Description: PHP3 Generated Data");
    --------------------------------------------------------------------------------