PHP如何利用P3P实现跨域


Posted in PHP onAugust 24, 2013

有别于JS跨域、IFRAME跨域等的常用处理办法,还可以利用P3P来实现跨域。

P3P是什么
P3P(Platform for Privacy Preferences)是W3C公布的一项隐私保护推荐标准,以为用户提供隐私保护。

P3P标准的构想是:Web 站点的隐私策略应该告之访问者该站点所收集的信息类型、信息将提供给哪些人、信息将被保留多少时间及其使用信息的方式,如站点应做诸如 “本网站将监测您所访问的页面以提高站点的使用率”或“本网站将尽可能为您提供更合适的广告”等申明。访问支持P3P网站的用户有权查看站点隐私报告,然 后决定是否接受cookie 或是否使用该网站。

如何利用P3P实现跨域
在开发中,我们碰到的跨域主要还是纠结在IE,页面中的IFRAME或者FRAME或者JS跨域的时候,IE有安全策略限制页面不带cookie,但是如果我们加上P3P,就没有这策略的限制。这也是P3P来突破跨域的可行前提。

以下为摘录的例子:
http://www.a.com/a_setcookie.php 文件内容:
<?php setcookie("test", $_GET['id'], time()+3600, "/", ".a.com"); ?>
http://www.a.com/a_getcookie.php 文件内容:
<?php var_dump($_COOKIE); ?>
http://www.b.com/b_setcookie.php 文件内容:
<script src="http://www.a.com/a_setcookie.php?id=www.b.com"></script>
通过浏览器访问:

1?> http://www.b.com/b_setcookie.php
2?> http://www.a.com/a_getcookie.php

访问1 b.com域后,我们并没有在2 a.com域发现设置上cookie值。
将http://www.a.com/a_setcookie.php文件内容改为如下:
<?php  
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');   
setcookie("test", $_GET['id'], time()+3600, "/", ".a.com");  
?>

再次访问:
http://www.b.com/b_setcookie.php
http://www.a.com/a_getcookie.php
在访问b.com域后,设置了a.com域的cookie值。
从上面例子可以看出通过发送P3P头信息而实现的跨域。(在Firefox不发送P3P也能跨域成功)

PHP使用P3P协议

header( 'P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"' );

JS使用P3P协议
xmlhttp.setRequestHeader( "P3P" , 'CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"' );

P3P的头部参数解释
引用:
P3P Header is present:
CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Compact Policy token is present. A trailing 'o' means opt-out, a trailing 'i' means opt-in.
CURa
Information is used to complete the activity for which it was provided.
ADMa
Information may be used for the technical support of the Web site and its computer system.
DEVa
Information may be used to enhance, evaluate, or otherwise review the site, service, product, or market.
PSAo
Information may be used to create or build a record of a particular individual or computer that is tied to a pseudonymous identifier, without tying identified data (such as name, address, phone number, or email address) to the record. This profile will be used to determine the habits, interests, or other characteristics of individuals for purpose of research, analysis and reporting, but it will not be used to attempt to identify specific individuals. 
PSDo
Information may be used to create or build a record of a particular individual or computer that is tied to a pseudonymous identifier, without tying identified data (such as name, address, phone number, or email address) to the record. This profile will be used to determine the habits, interests, or other characteristics of individuals to make a decision that directly affects that individual, but it will not be used to attempt to identify specific individuals.
OUR
We share information with ourselves and/or entities acting as our agents or entities for whom we are acting as an agent.
BUS
Info is retained under a service provider's stated business practices. Sites MUST have a retention policy that establishes a destruction time table. The retention policy MUST be included in or linked from the site's human-readable privacy policy.
UNI
Non-financial identifiers, excluding government-issued identifiers, issued for purposes of consistently identifying or recognizing the individual. These include identifiers issued by a Web site or service.
PUR
Information actively generated by the purchase of a product or service, including information about the method of payment.
INT
Data actively generated from or reflecting explicit interactions with a service provider through its site -- such as queries to a search engine, or logs of account activity.
DEM
Data about an individual's characteristics -- such as gender, age, and income.
STA
Mechanisms for maintaining a stateful session with a user or automatically recognizing users who have visited a particular site or accessed particular content previously -- such as HTTP cookies.
PRE
Data about an individual's likes and dislikes -- such as favorite color or musical tastes.
COM
Information about the computer system that the individual is using to access the network -- such as the IP number, domain name, browser type or operating system.
NAV
Data passively generated by browsing the Web site -- such as which pages are visited, and how long users stay on each page.
OTC
Other types of data not captured by the above definitions.
NOI
Web Site does not collected identified data.
DSP
The privacy policy contains DISPUTES elements.
COR
Errors or wrongful actions arising in connection with the privacy policy will be remedied by the service.

PS,这里说的跨域主要是设置cookie的情况,如果是跨域读取cookie,要保证在对应设置cookie的时候设置了P3P,否则在读取的事情IE会屏蔽跨域cookie。
PHP 相关文章推荐
php下使用SimpleXML 处理XML 文件
Feb 27 PHP
PHP中MVC模式的模板引擎开发经验分享
Mar 23 PHP
php实现MD5加密16位(不要默认的32位)
Aug 12 PHP
Drupal7 form表单二次开发要点与实例
Mar 02 PHP
从零开始学YII2框架(三)扩展插件yii2-gird
Aug 20 PHP
php生成短域名函数
Mar 23 PHP
php取得字符串首字母的方法
Mar 25 PHP
8个必备的PHP功能开发
Oct 02 PHP
PHP控制反转(IOC)和依赖注入(DI)
Mar 13 PHP
PHP基础之输出缓冲区基本概念、原理分析
Jun 19 PHP
PHP rmdir()函数的用法总结
Jul 02 PHP
laravel5.0在linux下解决.htaccess无效和去除index.php的问题
Oct 16 PHP
PHP引用符&amp;的用法详细解析
Aug 22 #PHP
新手菜鸟必读:session与cookie的区别
Aug 22 #PHP
PHP mysql与mysqli事务使用说明 分享
Aug 17 #PHP
php中url传递中文字符,特殊危险字符的解决方法
Aug 17 #PHP
测试PHP连接MYSQL成功与否的代码
Aug 16 #PHP
PHP 通过Socket收发十六进制数据的实现代码
Aug 16 #PHP
php读取mysql中文数据出现乱码的解决方法
Aug 16 #PHP
You might like
PHP实现随机生成水印图片功能
2017/03/22 PHP
jquery tools之tooltip
2009/07/25 Javascript
网站如何做到完全不需要jQuery也可以满足简单需求
2013/06/27 Javascript
JS实现的一个简单的Autocomplete自动完成例子
2014/04/16 Javascript
javascript实现相同事件名称,不同命名空间的调用方法
2015/06/26 Javascript
基于jQuery仿淘宝产品图片放大镜代码分享
2020/06/23 Javascript
详解JavaScript数组的操作大全
2015/10/19 Javascript
理解 JavaScript Scoping &amp; Hoisting(二)
2015/11/18 Javascript
7个jQuery最佳实践
2016/01/12 Javascript
使用node+vue.js实现SPA应用
2016/01/28 Javascript
Jquery对新插入的节点 绑定Click事件失效的解决方法
2016/06/02 Javascript
JavaScript限制在客户区可见范围的拖拽(解决scrollLeft和scrollTop的问题)(2)
2017/05/17 Javascript
基于jQuery和CSS3实现APPLE TV海报视差效果
2017/06/16 jQuery
通过vue-cli3构建一个SSR应用程序的方法
2018/09/13 Javascript
JavaScript寄生组合式继承原理与用法分析
2019/01/11 Javascript
node.js处理前端提交的GET请求
2019/08/30 Javascript
Python pass 语句使用示例
2014/03/11 Python
基python实现多线程网页爬虫
2015/09/06 Python
python数据类型_字符串常用操作(详解)
2017/05/30 Python
Python Tkinter模块实现时钟功能应用示例
2018/07/23 Python
Django代码性能优化与Pycharm Profile使用详解
2018/08/26 Python
在python中使用with打开多个文件的方法
2019/01/07 Python
完美解决pycharm导入自己写的py文件爆红问题
2020/02/12 Python
python 还原梯度下降算法实现一维线性回归
2020/10/22 Python
涉外经济法专业毕业生推荐信
2013/11/24 职场文书
建筑工程管理专业自荐信范文
2013/12/28 职场文书
区优秀教师事迹材料
2014/02/10 职场文书
初中三好学生自我鉴定
2014/04/07 职场文书
班级学习计划书
2014/04/27 职场文书
党员群众路线学习心得体会
2014/11/04 职场文书
小学中等生评语
2014/12/29 职场文书
公司开业主持词
2015/07/02 职场文书
学校安全管理制度
2015/08/06 职场文书
婚宴祝酒词大全
2015/08/10 职场文书
浅谈如何写好演讲稿?
2019/06/12 职场文书
CSS 实现Chrome标签栏的技巧
2021/08/04 HTML / CSS