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 相关文章推荐
一个很方便的 XML 类!!原创的噢
Oct 09 PHP
Php注入点构造代码
Jun 14 PHP
无法载入 mcrypt 扩展,请检查 PHP 配置终极解决方案
Jul 18 PHP
PHP获取http请求的头信息实现步骤
Dec 16 PHP
基于php中使用excel的简单介绍
Aug 02 PHP
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
Jun 25 PHP
php去除html标记的原生函数详解
Jan 27 PHP
php实现用于验证所有类型的信用卡类
Mar 24 PHP
Linux操作系统安装LAMP环境
Jun 26 PHP
PHP手机号中间四位用星号*代替显示的实例
Jun 02 PHP
PHP常见的序列化与反序列化操作实例分析
Oct 28 PHP
PHP实现递归的三种方法
Jul 04 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把小数转成整数3种方法
2014/06/30 PHP
超级兔子让浮动层消失的前因后果
2007/03/09 Javascript
ext jquery 简单比较
2010/04/07 Javascript
使用隐藏的new来创建对象
2011/03/29 Javascript
浅谈checkbox的一些操作(实战经验)
2013/11/20 Javascript
可恶的ie8提示缺少id未定义
2014/03/20 Javascript
jquery实现不同大小浏览器使用不同的css样式表的方法
2014/04/02 Javascript
javascript中eval解析JSON字符串
2016/02/27 Javascript
jQuery前端开发35个小技巧
2016/05/24 Javascript
javascript实现粘贴qq截图功能(clipboardData)
2016/05/29 Javascript
bootstrap输入框组代码分享
2016/06/07 Javascript
JavaScript的new date等日期函数在safari中遇到的坑
2016/10/24 Javascript
原生js实现图片放大缩小计时器效果
2017/01/20 Javascript
基于vue的换肤功能的示例代码
2017/10/10 Javascript
vue实现todolist基本功能以及数据存储功能实例详解
2019/04/11 Javascript
Vue在 Nuxt.js 中重定向 404 页面的方法
2019/04/23 Javascript
详解使用WebPack搭建React开发环境
2019/08/06 Javascript
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
JS实现贪吃蛇游戏
2019/11/15 Javascript
js实现消灭星星(web简易版)
2020/03/24 Javascript
[01:17:47]TNC vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
使用python获取CPU和内存信息的思路与实现(linux系统)
2014/01/03 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
python3中获取文件当前绝对路径的两种方法
2018/04/26 Python
Django开发中的日志输出的方法
2018/07/02 Python
python使用scapy模块实现ping扫描的过程详解
2021/01/21 Python
技术总监个人的自我评价范文
2013/12/18 职场文书
消防安全汇报材料
2014/02/08 职场文书
行政管理专业求职信
2014/07/06 职场文书
主题党日活动总结
2014/07/08 职场文书
社区创先争优承诺书
2014/08/30 职场文书
盗窃案辩护词
2015/05/21 职场文书
2015年度工程师评职称工作总结
2015/10/14 职场文书
2016年全国爱眼日宣传教育活动总结
2016/04/05 职场文书
小学生作文之《压岁钱的烦恼》
2019/09/27 职场文书
Win11如何查看显卡型号 Win11查看显卡型号的方法
2022/08/14 数码科技