[php] Примеры HTTP заголовка header()

Самые частые примеры HTTP заголовка страниц, аякс-ответов, отдача файлов и т.д.

header("Content-Type: text/html; charset=utf-8");  
header('Content-type: application/json');
header('Content-type: application/json;charset=utf-8');
header('Location: http://www.example.com/');
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 
//mb_internal_encoding("UTF-8");
### EXсel ###
header('Pragma: public');
header('Cache-control: private');
header('Accept-Ranges: bytes');
header('Content-Length: '.strlen($content));
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename=excel_dump.xls');
### CSV ###
header('Pragma: public');
header('Cache-control: private');
header('Accept-Ranges: bytes');
header('Content-Length: '.strlen($content));
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=data.csv');
### XML, CommerceML ###
header('Pragma: public');
header('Cache-control: private');
header('Accept-Ranges: bytes');
header("Content-Type: application/xml; charset=windows-1251");
header("Content-Length: ".$str);
header("Content-Disposition: attachment; filename=orders.xml");
# Пример 1 - Диалог загрузки
// Будем передавать PDF
header('Content-Type: application/pdf');
// Который будет называться downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
или
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
// Исходный PDF файл original.pdf
readfile('original.pdf');
# Пример 2
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
    header("Content-type: text/javascript");
    header("Content-Disposition: inline; filename=\"download.js\"");
    header("Content-Length: ".filesize("script.js"));
} else {
    header("Content-type: application/force-download");
    header("Content-Disposition: attachment; filename=\"download.js\"");
    header("Content-Length: ".filesize("script.js"));
}
header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
}
include("script.js");
# Пример 3
header( "refresh:5;url=wherever.php" ); 
echo 'You\'ll be redirected in about 5 secs. If not, click <a href="wherever.php">here</a>.'; 
# Пример 4 for large files (100+ MBs), 
header("Content-Disposition: attachment; filename=" . urlencode($file));    
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($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r"); 
 while (!feof($fp))
 {
     echo fread($fp, 65536); 
     flush(); // this is essential for large downloads
}  
fclose($fp); 
Имя *
Логин (мин. 3 символа)
E-mail *
*— обязательные для заполнения поля
Логин или e-mail
TUNING-SOFT.RU Разработка умных веб-сервисов