Php header sample code (recommended)

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
/*** Function: PHP header () examples (PHP)
** Desc: Some examples on how to use the header () function of PHPYou find a detailed tutorial at expertsrt.com (English) or at ffm.junetz.de (German ). these is also a good help about caching at web-caching.com.
** Example: see below. <br/> <B> Tip: </B> You can use these sites to check your headers: <a href = "http://web-sniffer.net/"> web-sniffer.net </a>, <a href = "http://www.delorie.com/web/headers.html"> delorie.com </a> or <a href = "http://www.forret.com/projects/analyze/"> www.forret.com </a>.
** Author: Jonas John
*/

// Fix 404 pages:
Header ('HTTP/1.1 200 OK ');

// Set 404 header:
Header ('HTTP/1.1 404 Not Found ');

// Set Moved Permanently header (good for redrictions)
// Use with location header
Header ('HTTP/1.1 301 Moved Permanently ');

// Redirect to a new location:
Header ('location: http://www.example.org /');

// Redrict with delay:
Header ('refresh: 10; url = http://www.example.org /');
Print 'you will be redirected in 10 seconds ';

// You cocould also use the HTML syntax: // <meta http-equiv = "refresh" content = "10; http://www.example.org //>

// Override X-Powered-By: PHP:
Header ('x-Powered-By: PHP/4.4.0 ');
Header ('x-Powered-By: Brain/0.6b ');

// Content language (en = English)
Header ('content-language: en ');

// Last modified (good for caching)
$ Time = time ()-60; // or filemtime ($ fn), etc
Header ('Last-Modified: '. gmdate ('d, d m y h: I: s', $ time). 'gmt ');

// Header for telling the browser that the content
// Did not get changed
Header ('HTTP/1.1 304 Not modified ');

// Set content length (good for caching ):
Header ('content-Length: 1234 ');

// Headers for an download:
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = "example.zip "');
Header ('content-Transfer-Encoding: binary ');

// Load the file to send: readfile('example.zip ');
// Disable caching of the current document:
Header ('cache-Control: no-Cache, no-store, max-age = 0, must-revalidate ');
Header ('expires: Mon, 26 Jul 1997 05:00:00 gmt ');
// Date in the pastheader ('pragma: no-cache ');
// Set content type:
Header ('content-Type: text/html; charset = iso-8859-1 ');
Header ('content-Type: text/html; charset = UTF-8 ');
Header ('content-Type: text/plain ');

// Plain text file
Header ('content-Type: image/jpeg ');

// JPG picture
Header ('content-Type: application/zip ');

// ZIP file
Header ('content-Type: application/pdf ');

// PDF file
Header ('content-Type: audio/mpeg ');

// Audio MPEG (MP3 ,...) File
Header ('content-Type: application/x-shockwave-flash ');

// Flash animation // show sign in box
Header ('HTTP/1.1 401 unauthorized ');
Header ('www-Authenticate: Basic realm = "Top Secret "');
Print 'text that will be displayed if the user hits cancel or ';
Print 'enters wrong login data ';
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.