首頁 > 後端開發 > php教程 > php常用的header:301,302, Redirects, 404, Javascript, Download, Authentication,编码等设置小结

php常用的header:301,302, Redirects, 404, Javascript, Download, Authentication,编码等设置小结

WBOY
發布: 2016-06-20 13:04:51
原創
1029 人瀏覽過

301永久跳转

<?php 
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');
die();
?> 
登入後複製


302临时重定向跳转

<?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
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板