PHP header函数分析详解
程序员文章站
2022-12-07 08:20:13
在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助 复制代码...
在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助
<?php
// 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';
?>
复制代码 代码如下:
<?php
// 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';
?>
推荐阅读
-
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
-
详解WordPress中用于更新和获取用户选项数据的PHP函数
-
百度工程师讲PHP函数的实现原理及性能分析(二)
-
百度工程师讲PHP函数的实现原理及性能分析(一)
-
PHP 函数call_user_func和call_user_func_array用法详解
-
PHP中array_keys和array_unique函数源码的分析
-
PHP 源代码分析 Zend HashTable详解第1/3页
-
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
-
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
-
PHP date函数参数详解