zoukankan      html  css  js  c++  java
  • PHP Header

    返回response状态码

    header('HTTP/1.1 200 OK'); // ok 正常访问
    header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在
    header('HTTP/1.1 301 Moved Permanently'); //设置地址被永久的重定向 301
    header('HTTP/1.1 304 Not Modified'); //告诉浏览器文档内容没有发生改变

    设置修改 X-Powered-By信息

    header('X-Powered-By: PHP/6.0.0');

     文档语言

    header('Content-language: en');

    内容长度

    header('Content-Length: 1024');

    内容类型

    header('Content-Type: text/html; charset=utf-8'); //网页格式,网页编码
    header('Content-Type: text/plain'); //纯文本格式

    设置浏览器最后一次修改时间

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

    对当前文档禁用缓存

    header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");

    Cache-control介绍

    一个需要验证的登陆对话框

    header('HTTP/1.1 401 Unauthorized'); 
    header('WWW-Authenticate: Basic realm="Top Secret"'); 

    跳转页面

    header('Location:'.$url);  //Location和":"之间无空格
    header('Refresh: 10; url=http://www.baidu.com/');  //10s后跳转

    声明一个下载的文件

    // 下载 zip
    header('Content-Type: application/octet-stream'); //设置内容类型
    header('Content-Disposition: attachment; filename="ITblog.zip"'); //设置MIME用户作为附件
    header('Content-Transfer-Encoding: binary'); //设置传输方式
    readfile('test.zip'); // 打开文件,输出内容
    // 下载 execl
    header('Content-Disposition: attachment; filename=ithhc.xlsx');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Length: '.filesize('./test.xls')); //设置内容长度
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); // 任何人都可访问
    readfile('./test.xls');

    ajax 设置Access-Control-Allow-Origin实现跨域访问

    //在被请求的Response header中加入
    //指定某域名(http://client.runoob.com)跨域访问
    header('Access-Control-Allow-Origin:http://client.runoob.com');
    //指定多个域名(http://client1.runoob.com、http://client2.runoob.com等)跨域访问
    $allow_origin = array(  
        'http://client1.runoob.com',  
        'http://client2.runoob.com'  
    ); 
    header('Access-Control-Allow-Origin:'.$origin);    
    // 指定允许其他域名访问
    header('Access-Control-Allow-Origin:*');
    //设置允许的跨域header,区别普通请求还是ajax请求
    header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');  
    // 响应类型
    header('Access-Control-Allow-Methods:POST');
    // 带 cookie 的跨域访问
    header('Access-Control-Allow-Credentials: true');
    // 响应头设置
    header('Access-Control-Allow-Headers:x-requested-with,content-type,X-CSRF-Token');

    限制iframe网页嵌套

    使用 X-Frame-Options 有三个可选的值:

    DENY:浏览器拒绝当前页面加载任何Frame页面

    SAMEORIGIN:frame页面的地址只能为同源域名下的页面

    ALLOW-FROM:origin为允许frame加载的页面地址

    header('X-Frame-Options:Deny'); // php
    add_header X-Frame-Options SAMEORIGIN // nignx
    Header always append X-Frame-Options SAMEORIGIN // Apache

    开启了XSS保护

    0:禁用XSS保护;

    1:启用XSS保护;

    1; mode=block:启用XSS保护,并在检查到XSS攻击时,停止渲染页面(例如IE8中,检查到攻击时,整个页面会被一个#替换);

    header("X-XSS-Protection: 1"); 

    X-Content-Type-Options

      通常浏览器会根据响应头的Content-Type字段来分辨它们的类型。有些资源的Content-Type是错的或者未定义。这时,某些浏览器会启用MIME-sniffing来猜测该资源的类型,解析内容并执行。利用浏览器的这个特性,攻击者甚至可以让原本应该解析为图片的请求被解析为JavaScript。通过下面这个响应头可以禁用浏览器的类型猜测行为:

    header("X-Content-Type-Options:nosniff");

     

  • 相关阅读:
    ASP.NET 错误
    linux下使用蓝牙设备【转】
    AIDL Android中的远程接口 [转]
    Handler理解
    Hid Report Descriptor
    Android kernel x86 编译方法
    Android Init Language
    DBUS 资源
    Analysing Bluetooth Keyboard Traffic with hcidump
    DBUS基础知识
  • 原文地址:https://www.cnblogs.com/xuey/p/9739459.html
Copyright © 2011-2022 走看看