文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>修改HTTP header

修改HTTP header

时间:2008-07-31  来源:liuxingyuyuni


下面这些header指令常常会用到,记得刚工作的时候写的一个网络磁盘就用到下载文件的header。header还可以用来控制客户端浏览器和代理服务器的缓存,这些以前有做过笔记,心情好的时候整理一下发上来


//用这个header指令来解决URL重写产生的404 header
  • header('HTTP/1.1 200 OK');

    • // 页面没找到
    • header('HTTP/1.1 404 Not Found');

    • // 访问受限
    • header('HTTP/1.1 403 Forbidden');

    • // The page moved permanently should be used for
    • // all redrictions, because search engines know
    • // what's going on and can easily update their urls.
    • //页面被永久删除,可以告诉搜索引擎更新它们的urls
    • //PS:第一次碰到这个指令,不知道能不能用来把自己的站点从搜索引擎中删除呢?
    • header('HTTP/1.1 301 Moved Permanently');

    • // 服务器错误
    • header('HTTP/1.1 500 Internal Server Error');

    • // 重定向到一个新的位置
    • header('Location: http://www.example.org/');

    • // 延迟一段时间后重定向
    • header('Refresh: 10; url=http://www.example.org/');
    • print 'You will be redirected in 10 seconds';

    • // 也可以使用HTML语法来实现延迟
    • // http://www.example.org/ />

    • // 覆盖 X-Powered-By value
    • header('X-Powered-By: PHP/4.4.0');
    • header('X-Powered-By: Brain/0.6b');

    • // 内容语言 (en = English)
    • header('Content-language: en');

    • // 最后修改时间(在缓存的时候可以用到)
    • $time = time() - 60; // or filemtime($fn), etc
    • header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

    • // 告诉浏览器要获取的内容还没有更新
    • header('HTTP/1.1 304 Not Modified');

    • // 设置内容的长度 (缓存的时候可以用到):
    • header('Content-Length: 1234');

    • // 用来下载文件:
    • header('Content-Type: application/octet-stream');
    • header('Content-Disposition: attachment; filename="example.zip"');
    • header('Content-Transfer-Encoding: binary');
    • // 加载要下载的文件:
    • readfile('example.zip');

    • // 禁止缓存当前文档:
    • header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    • header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    • header('Pragma: no-cache');

    • // 设置内容类型:
    • header('Content-Type: text/html; charset=iso-8859-1');
    • header('Content-Type: text/html; charset=utf-8');
    • header('Content-Type: text/plain'); // plain text file
    • header('Content-Type: image/jpeg'); // JPG picture
    • header('Content-Type: application/zip'); // ZIP file
    • header('Content-Type: application/pdf'); // PDF file
    • header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
    • header('Content-Type: application/x-shockwave-flash'); // Flash animation

    • // 显示登录对话框,可以用来进行HTTP认证
    • header('HTTP/1.1 401 Unauthorized');
    • header('WWW-Authenticate: Basic realm="Top Secret"');
    • print 'Text that will be displayed if the user hits cancel or ';
    • print 'enters wrong login data';


  • 相关阅读 更多 +
    排行榜 更多 +
    辰域智控app

    辰域智控app

    系统工具 下载
    网医联盟app

    网医联盟app

    运动健身 下载
    汇丰汇选App

    汇丰汇选App

    金融理财 下载