php 常见 header 用法说明

 

 最常见的php header编码

header("Content-type: text/html; charset=utf-8"); 

 

301 重定向 (redirect): 

<?php 

header('HTTP/1.1 301 Moved Permanently');

header('Location: http://tqybw.net');

die();

?> 

 

302 跳转(redirect): 

<?php 

header('Location: http://tqybw.net');

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

?> 

 

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

}

}

?>

 

文件下载 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);

?> 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值