欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php header示例代码(推荐)

程序员文章站 2023-04-08 08:07:32
复制代码 代码如下:

复制代码 代码如下:

<?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/><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 could 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';
?>