• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-23) 21次浏览 已收录 0个评论

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样式文件:

<?phpheader('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 i~来1源gaodai#ma#com搞*代#码1网搞代gaodaima码s running as an Apache module:

<?phpif (!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"); 

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php常用的header:301,302, Redirects, 404, Javascript, Download, Authentication,编码等设置小结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址