Hướng dẫn dùng deafult pfp trong PHP
mjt at jpeto dot net ¶ 13 years ago I strongly recommend, that you use header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); instead of header("HTTP/1.1 404 Not Found"); I had big troubles with an Apache/2.0.59 (Unix) answering in HTTP/1.0 while I (accidentially) added a "HTTP/1.1 200 Ok" - Header. Most of the pages were displayed correct, but on some of them apache added weird content to it: A 4-digits HexCode on top of the page (before any output of my php script), seems to be some kind of checksum, because it changes from page to page and browser to browser. (same code for same page and browser) "0" at the bottom of the page (after the complete output of my php script) It took me quite a while to find out about the wrong protocol in the HTTP-header. Marcel G ¶ 12 years ago Several times this one is asked on the net but an answer could not be found in the docs on php.net ... If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent. So, either you have to use the HTML meta refresh thingy or you use the following: header(
"refresh:5;url=wherever.php" ); Dylan at WeDefy dot com ¶ 14 years ago A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header(). //
301 Moved Permanently mandor at mandor dot net ¶ 16 years ago When using PHP to output an image, it won't be cached by the client so if you don't want them to download the image each time they reload the page, you will need to emulate part of the HTTP protocol. Here's how: // Test image. tim at sharpwebdevelopment dot com ¶ 4 years ago The header call can be misleading to novice php users. php at ober-mail dot de ¶ 2 years ago Since PHP 5.4, the function `http_response_code()` can be used to set the response code instead of using the `header()` function, which requires to also set the correct protocol version (which can lead to problems, as seen in other comments). bebertjean at yahoo dot fr ¶ 13 years ago If using the 'header' function for the downloading of files, especially if you're passing the filename as a variable, remember to surround the filename with double quotes, otherwise you'll have problems in Firefox as soon as there's a space in the filename. So instead of typing: header("Content-Disposition: attachment; filename=" . basename($filename)); header("Content-Disposition: attachment; filename=\""
. basename($filename) . "\""); See the page called "Filenames_with_spaces_are_truncated_upon_download" at yjf_victor ¶ 6 years ago According to the RFC 6226 (https://tools.ietf.org/html/rfc6266), the only way to send Content-Disposition Header with encoding is: Content-Disposition: attachment; for backward compatibility, what should be sent is: Content-Disposition: attachment; As a result, we should use $filename = '中文文件名.exe'; // a filename in Chinese
characters$contentDispositionField = 'Content-Disposition: attachment; ' David ¶ 4 years ago It seems the note saying the URI must be absolute is obsolete. Found on https://en.wikipedia.org/wiki/HTTP_location «An obsolete version of the HTTP 1.1 specifications (IETF RFC 2616) required a complete absolute URI for redirection.[2] The IETF HTTP working group found that the most popular web browsers tolerate the passing of a relative URL[3] and, consequently, the updated HTTP 1.1 specifications (IETF RFC 7231) relaxed the original constraint, allowing the use of relative URLs in Location headers.» ben at indietorrent dot org ¶ 10 years ago Be aware that sending binary files to the user-agent (browser) over an encrypted connection (SSL/TLS) will fail in IE (Internet Explorer) versions 5, 6, 7, and 8 if any of the following headers is included: Cache-control:no-store See: http://support.microsoft.com/kb/323308 Workaround: do not send those headers. Also, be aware that IE versions 5, 6, 7, and 8 double-compress already-compressed files and do not reverse the process correctly, so ZIP files and similar are corrupted on download. Workaround: disable compression (beyond text/html) for these particular versions of IE, e.g., using Apache's "BrowserMatch" directive. The following example disables compression in all versions of IE: BrowserMatch ".*MSIE.*" gzip-only-text/html nospam at nospam dot com ¶ 6 years ago //
Response codes behaviors when using // In other words, use 301 if permanent, 302 if temporary, and 303 if a results page from a submitted form. shutout2730 at yahoo dot com ¶ 14 years ago It is important to note that headers are actually sent when the first byte is output to the browser. If you are replacing headers in your scripts, this means that the placement of echo/print statements and output buffers may actually impact which headers are sent. In the case of redirects, if you forget to terminate your script after sending the header, adding a buffer or sending a character may change which page your users are sent to. This redirects to 2.html since the second header replaces the first. header("location: 1.html"); header("location: 1.html"); ob_start(); razvan_bc at yahoo dot com ¶ 4 years ago /* This will give an error. Note the output header ('Location: http://www.example.com/');die();?> 'CAUSE all die function does is to stop the script ,there is no other place for interpretation and the scope you choose to break the action of your script is quickly DONE!!! there are many situations with others examples and the right choose for small parts of your scrips that make differences when you write your php framework at well! Thanks Rasmus Lerdorf and his team to wrap off parts of unusual php functionality ,php 7 roolez!!!!! sk89q ¶ 14 years ago You can use HTTP's etags and last modified dates to ensure that you're not sending the browser data it already has cached. $last_modified_time =
filemtime($file); if (@ strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { header("HTTP/1.1 304 Not Modified"); exit; } ?> dev at omikrosys dot com ¶ 12 years ago Just to inform you all, do not get confused between Content-Transfer-Encoding and Content-Encoding Content-Transfer-Encoding specifies the encoding used to transfer the data within the HTTP protocol, like raw binary or base64. (binary is more compact than base64. base64 having 33% overhead). Content-Encoding is used to apply things like gzip compression to the content/data. jp at webgraphe dot com ¶ 18 years ago A call to session_write_close() before the statement header("Location: URL"); We encountered a situation where the script accessed by the redirection wasn't loading the session correctly because the precedent script hadn't the time to update it (we used a database handler). JP. chris at ocproducts dot com ¶ 5 years ago Note that 'session_start' may overwrite your custom cache
headers. session_cache_limiter(''); ...after you set your custom cache headers. It will tell the PHP session code to not do any cache header changes of its own. mzheng[no-spam-thx] at ariba dot com ¶ 13 years ago For large files (100+ MBs), I found that it is essential to flush the file content ASAP, otherwise the download dialog doesn't show until a long time or never. header("Content-Disposition: attachment; filename=" . urlencode($file)); Vinay Kotekar ¶ 7 years ago Saving php file in ANSI no isuess but when saving the file in UTF-8 format for various reasons remember to save the file without any BOM ( byte-order mark) support. Warning: Cannot modify header information - headers already sent by (output started at C:\www\info.php:1) in C:\www\info.php on line 1 Cody G. ¶ 12 years ago After lots of research and testing, I'd like to share my findings about my problems with Internet Explorer and file downloads. Take a look at this code, which replicates the normal download of a Javascript:
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) { I start out by checking for IE, then if not IE, I set Content-type (case-sensitive) to JS and set Content-Disposition (every header is case-sensitive from now on) to inline, because most browsers outside of IE like to display JS inline. (User may change settings). The Content-Length header is required by some browsers to activate download box. Then, if it is IE, the "application/force-download" Content-type is sometimes required to show the download box. Use this if you don't want your PDF to display in the browser (in IE). I use it here to make sure the box opens. Anyway, I set the Content-Disposition to attachment because I already know that the box will appear. Then I have the Content-Length again. Now, here's my big point. I have the Cache-Control and Pragma headers sent only if not IE. THESE HEADERS WILL PREVENT DOWNLOAD ON IE!!! Only use the Expires header, after all, it will require the file to be downloaded again the next time. This is not a bug! IE stores downloads in the Temporary Internet Files folder until the download is complete. I know this because once I downloaded a huge file to My Documents, but the Download Dialog box put it in the Temp folder and moved it at the end. Just think about it. If IE requires the file to be downloaded to the Temp folder, setting the Cache-Control and Pragma headers will cause an error! I hope this saves someone some time! scott at lucentminds dot com ¶ 12 years ago If you want to remove a header and keep it from being sent as part of the header response, just provide nothing as the header value after the header name. For example... PHP, by default, always returns the following header: "Content-Type: text/html" Which your entire header response will look like HTTP/1.1 200 OK If you call the header name with no value like so... header ( 'Content-Type:' );?>Your headers now look like this: HTTP/1.1 200
OK Anonymous ¶ 13 years ago I just want to add, becuase I see here lots of wrong formated headers. 1. All used headers have first letters uppercase, so you MUST follow this. For example: Location, not location 2. Then there MUST be colon and space, like good: header("Content-Type: text/plain"); 3. Location header MUST be absolute uri with scheme, domain, port, path, etc. good: header("Location: http://www.example.com/something.php?a=1"); 4. Relative URIs are NOT allowed wrong: Location: /something.php?a=1 It will make proxy server and http clients happier. Angelica Perduta ¶ 2 years ago I made a script that generates an optimized image for use on web pages using a 404 script to resize and reduce original images, but on some servers it was generating the image but then not using it due to some kind of cache somewhere of the 404 status. I managed to get it to work with the following and although I don't quite understand it, I hope my posting here does help others with similar issues: header_remove(); Refugnic ¶ 12 years ago My files are in a compressed state (bz2). When the user clicks the link, I want them to get the uncompressed version of the file. After decompressing the file, I ran into the problem, that the download dialog would always pop up, even when I told the dialog to 'Always perform this operation with this file type'. As I found out, the problem was in the header directive 'Content-Disposition', namely the 'attachment' directive. If you want your browser to simulate a plain link to a file, either change 'attachment' to 'inline' or omit it alltogether and you'll be fine. This took me a while to figure out and I hope it will help someone else out there, who runs into the same problem. bMindful at fleetingiamge dot org ¶ 19 years ago If you haven't used, HTTP Response 204 can be very convenient. 204 tells the server to immediately termiante this request. This is helpful if you want a javascript (or similar) client-side function to execute a server-side function without refreshing or changing the current webpage. Great for updating database, setting global variables, etc. header("status: 204"); (or the other call) nobileelpirata at hotmail dot com ¶ 15 years ago This is the Headers to force a browser to use fresh content (no caching) in HTTP/1.0 and HTTP/1.1: header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' ); er dot ellison dot nyc at gmail dot com ¶ 6 years ago DO NOT PUT
space between location and the colon that comes after that , // INSTEAD USE THIS -> jamie ¶ 13 years ago The encoding of a file is discovered by the Content-Type, either in the HTML meta tag or as part of the HTTP header. Thus, the server and browser does not need - nor expect - a Unicode file to begin with a BOM mark. BOMs can confuse *nix systems too. More info at http://unicode.org/faq/utf_bom.html#bom1 On another note: Safari can display CMYK images (at least the OS X version, because it uses the services of QuickTime) hamza dot eljaouhari dot etudes at gmail dot com ¶ 4 years ago // Beware that adding a space between the keyword "Location" and the colon causes an Internal Sever Error //This line causes the error // While It must be written without the space ASchmidt at Anamera dot net ¶ 4 years ago Setting the "Location: " header has another undocumented side-effect! It will also disregard any expressly set "Content-Type: " and forces: "Content-Type: text/html; charset=UTF-8" The HTTP RFCs don't call for such a drastic action. They simply state that a redirect content SHOULD include a link to the destination page (in which case ANY HTML compatible content type would do). But PHP even overrides a perfectly standards-compliant cedric at gn dot apc dot org ¶ 11 years ago Setting a Location header "returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set". If you are sending a response to a POST request, you might want to look at RFC 2616 sections 10.3.3 and 10.3.4. It is suggested that if you want the browser to immediately GET the resource in the Location header in this circumstance, you should use a 303 status code not the 302 (with the same link as hypertext in the body for very old browsers). This may have (rare) consequences as mentioned in bug 42969. David Spector ¶ 1 year ago Please note that there is no error checking for the header command, either in PHP, browsers, or Web Developer Tools. If you use something like "header('text/javascript');" to set the MIME type for PHP response text (such as for echoed or Included data), you will get an undiagnosed failure. The proper MIME-setting function is "header('Content-type: text/javascript');". |