ホームページ  >  記事  >  バックエンド開発  >  PHP で一般的に使用されるヘッダー: 301、302、リダイレクト、404、JavaScript、ダウンロード、認証、エンコーディング、その他の設定の概要

PHP で一般的に使用されるヘッダー: 301、302、リダイレクト、404、JavaScript、ダウンロード、認証、エンコーディング、その他の設定の概要

WBOY
WBOYオリジナル
2016-06-20 13:04:51953ブラウズ

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 ページが見つかりません:

<?php 
header('HTTP/1.1 404 Not Found');
?> 



サービスは利用できません:

<?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 ヘッダー スクリプト:

<?php 
header('Content-Type: application/javascript');
?> 



画像画像出力:

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 ファイル (php で PDF を出力):

<?php 
header('Content-Type: application/pdf');
echo file_get_contents('filename.pdf');
?> 



キャッシュ (ブラウザにファイルをキャッシュさせないようにする):

<?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'); 
?> 



ダウンロード ダイアログダウンロード ダイアログ:

<?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);
?> 



認証 (ブラウザに強制的にユーザー名/パスワード入力ウィンドウをポップアップ表示させます) - PHP が Apache モジュールとして実行されている場合にのみ利用可能です:

<?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("Content-type: text/html; charset=utf-8"); 


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。