Bài 21: Hàm Header Trong Php

header() dùng để gửi 1 HTTP.. header thô tới trình săn sóc. HTTP.. headers được diễn đạt chi tiết trong HTTP/1.1
4.4.2 & 5.1.2
(PHP 4, PHP. 5) header — Skết thúc a raw HTTP.. header Description void header ( string $string <, bool $replace= true <, int $http_response_code >> )Remember that header() must be called before anyactual output is sent, either by normal HTML tags, blank lines in afile, or from PHP.. It is a very comtháng error khổng lồ read code withinclude(), or require(), functions, or another tệp tin access function, và have sầu spaces or emptylines that are output before header() is called.The same problem exists when using a single PHP/HTML tệp tin. /* This will give an error. lưu ý the output* above sầu, which is before the header() Gọi */header("Location: http://www.example.com/");?> Parameters string The header string.There are two special-case header calls. The first is a headerthat starts with the string "HTTP/" (case is notsignificant), which will be used lớn figure out the HTTPhường. statuscode lớn skết thúc. For example, if you have sầu configured Apabít touse a PHPhường script khổng lồ handle requests for missing files (usingthe ErrorDocument directive), you may want tomake sure that your script generates the proper status code. header("HTTP/1.0 404 Not Found");?> The second special case is the "Location:" header. Not only doesit skết thúc this header bachồng to lớn the browser, but it also returns aREDIRECT (302) status code to the browser unlesssome 3xx status code has already been set. header("Location: http://www.example.com/"); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */exit;?> replace The optional replace parameter indicateswhether the header should replace a previous similar header, oradd a second header of the same type. By mặc định it will replace,but if you pass in FALSE as the second argument you can forcemultiple headers of the same type.

Bạn đang xem: Bài 21: hàm header trong php

Xem thêm: Tâm Lý Phụ Nữ Khi Yêu ThậT Lòng Có Gì đÁº·C BiệT Dễ NhậN ThấY?

For example: header("WWW-Authenticate: Negotiate");header("WWW-Authenticate: NTLM", false);?> http_response_code Forces the HTTPhường response code to lớn the specified value. Return ValuesNo value is returned. Changelog Version Description
This function now prevents more than one header to lớn be sent at once asa protection against header injection attacks.
4.3.0The http_response_code parameter was added.
4.0.4The replace parameter was added.
Examples Example #1 Download dialog If you want the user khổng lồ be prompted khổng lồ save sầu the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename và force the browser lớn display the save dialog. // We"ll be outputting a PDFheader("Content-type: application/pdf"); // It will be called downloaded.pdfheader("Content-Disposition: attachment; filename="downloaded.pdf""); // The PDF source is in original.pdfreadfile("original.pdf");?> Example #2 Caching directives PHP scripts often generate dynamic nội dung that must not be cached by the client browser or any proxy caches between the VPS và the client browser. Many proxies và clients can be forced to lớn disable caching with: header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past?> Note: You may find that your pages aren"t cached even if you don"toutput all of the headers above. There are a number of optionsthat users may be able to lớn phối for their browser that change itsmặc định caching behavior. By sending the headers above, you shouldoverride any settings that may otherwise cause the output of yourscript to be cached.Additionally, session_cache_limiter() andthe session.cache_limiter configurationsetting can be used to lớn automatically generate the correctcaching-related headers when sessions are being used. Notes Note:As of PHPhường 4, you can use output buffering khổng lồ get around this problem, with the overhead of all of your output khổng lồ the browser being buffered in the VPS until you sover it. You can do this by calling ob_start() và ob_end_flush() in your script, or setting the output_buffering configuration directive sầu on in your php.ini or hệ thống configuration files. Note:The HTTPhường status header line will always be the first sent khổng lồ the client, regardless of the actual header() Gọi being the first or not. The status may be overridden by calling header() with a new status line at any time unless the HTTPhường headers have already been sent. Note: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 Paông xã 2 or later. Note: If safe mode is enabled the uid of the script is added to lớn the realm part of the WWW-Authenticate header if you phối this header (used for HTTPhường. Authentication). Note:HTTP/1.1 requires an absolute URI as argument lớn » Location: including the scheme, hostname và absolute path, but some clients accept relative URIs. You can usually use $_SERVER<"HTTP_HOST">, $_SERVER<"PHP_SELF"> and dirname() lớn make an absolute URI from a relative one yourself: /* Redirect to a different page in the current directory that was requested */$host = $_SERVER<"HTTP_HOST">;$uri = rtrim(dirname($_SERVER<"PHP_SELF">), "/");$extra = "mypage.php";header("Location: http://$host$uri/$extra");exit;?>