php的header()函数浅析

    xiaoxiao2021-12-14  20

    格式:

    <?php

    header();//为了防止出错header函数请紧跟<?php之后写。

    ?>

    注意事项有以下几点.Location和”:”之间不能有空格,否则会出现错误;在用header前不能有任何的输出(注释:这点大家都知道的,如果header之前有任何的输出,包括空白,就会出现header already sent by xxx的错误);header 后面的东西还会执行的;

    1.重定向

    header("Location: http://blog.csdn.net");

    exit; //在每个重定向之后都必须加上“exit”,避免发生错误后,继续执行。

    2.禁止浏览器缓存

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    header("Cache-Control: no-cache");

    header("Pragma: no-cache");

    3.禁止页面在IE中缓存

    header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );

    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );

    header( 'Cache-Control: no-store, no-cache, must-revalidate' );

    header( 'Cache-Control: post-check=0, pre-check=0', false );

    header( 'Pragma: no-cache' ); //兼容http1.0和https

    4.实现文件下载

    header('Content-Type: application/octet-stream');//设置内容类型

    header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开

    header('Content-Transfer-Encoding: binary');//设置传输方式

    header('Content-Length: '.filesize('example.zip'));//设置内容长度

    readfile('example.zip');//读取需要下载的文件

    5. 向浏览器发送Status标头

    header(”Status: 404 Not Found”);

    6ok

    header(‘HTTP/1.1 200 OK’);

    7设置一个404头

    header(‘HTTP/1.1 404 Not Found’);

    8设置地址被永久的重定向

    header(‘HTTP/1.1 301 Moved Permanently’);

    header("Location: http://blog.csdn.net");

    转载请注明原文地址: https://ju.6miu.com/read-971750.html

    最新回复(0)