php惯用的header:301,302, Redirects, 404,及文档类型
2014-08-05 11:46
288 查看
PHP is not limited to outputting html. PHP can output images, pdf, javascript files, etc. Browsers determine
what type of content is by analyzing the headers sent. In this tutorial I will present you with some examples of how to send headers. To send PHP header use the function header(). You must call this function before any output. Use the function headers_sent()
to check whether the headers have been sent and output started.
301 moved permanently (redirect):
302 moved temporarily(redirect):
404 Page Not Found:
Service not avaliable:
CSS:
Javascript header:
Images:
For JPEG(jpg):
For PNG:
For BMP:
PDF (output pdf with php):
Cache (force browsers not to cache files):
Download dialog:
Authentication (force the browser to pop up a Username/Password input window) - only available when PHP is running as an Apache module:
php header编码
header("Content-type: text/html; charset=utf-8");
what type of content is by analyzing the headers sent. In this tutorial I will present you with some examples of how to send headers. To send PHP header use the function header(). You must call this function before any output. Use the function headers_sent()
to check whether the headers have been sent and output started.
301 moved permanently (redirect):
<?php header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.example.com'); die(); ?>
302 moved temporarily(redirect):
<?php header('Location: http://www.example.com'); die(); ?>
404 Page Not Found:
<?php header('HTTP/1.1 404 Not Found'); ?>
Service not avaliable:
<?php header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 60'); ?>
CSS:
<?php header('Content-Type: text/css'); ?>
Javascript header:
<?php header('Content-Type: application/javascript'); ?>
Images:
For JPEG(jpg):
<?php header('Content-Type: image/jpeg'); ?>
For PNG:
<?php header('Content-Type: image/png'); ?>
For BMP:
<?php header('Content-Type: image/bmp'); ?>
PDF (output pdf with php):
<?php header('Content-Type: application/pdf'); echo file_get_contents('filename.pdf'); ?>
Cache (force browsers not to cache files):
<?php header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0'); header ('Pragma: no-cache'); ?>
Download dialog:
<?php header('Content-Disposition: attachment; filename=' . urlencode($f)); header('Content-Type: application/force-download'); header('Content-Type: application/octet-stream'); header('Content-Type: application/download'); header('Content-Description: File Transfer'); header('Content-Length: ' . filesize($f)); echo file_get_contents($f); ?>
Authentication (force the browser to pop up a Username/Password input window) - only available when PHP is running as an Apache module:
<?php if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="The Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'If cancel is pressed this text shows'; die(); } else { //always escape your data// $user='user'; $pass='pass'; if($_SERVER['PHP_AUTH_USER']==$user && $_SERVER['PHP_AUTH_PW']==$pass){ echo 'Authorized'; } } ?>
php header编码
header("Content-type: text/html; charset=utf-8");
相关文章推荐
- php常用的header:301,302, Redirects, 404
- PHP header()函数使用详细(301、404等错误设置)
- php header Content-type:所有文件类型 文档类型及服务器响应类别
- PHP header()函数使用详细(301、404等错误设置)
- PHP header 请求返回状态值设置(301,404)
- php用header函数实现301跳转代码实例
- nginx error_page 404 用 php header 无法跳转
- php header 头输出 不同文档
- php header Content-Type类型小结
- php 获取301 302的真实地址
- PHP 获取网页301|302真实地址
- php header 301
- 说说URL重定向的301和302类型
- 状态码200,301,302,404,500
- 200、301、302、304、404等HTTP状态码
- <?php Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://g.cn/"
- php http 301 302 重定向 跳转
- php header Content-Type类型小结
- php中下载相关header文件类型总结
- IIS日志代码分析,常见的200,404,301,302分别代表什么意思