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代码飞起来的40条小技巧(提升php效率)
Apr 12 PHP
php 使用post,get的一种简洁方式
Apr 25 PHP
php5 apache 2.2 webservice 创建与配置(java)
Jan 27 PHP
php解析html类库simple_html_dom(详细介绍)
Jul 05 PHP
PHP解码unicode编码的中文字符代码分享
Aug 13 PHP
php实现按天数、星期、月份查询的搜索框
May 02 PHP
利用php获得flv视频长度的实例代码
Oct 26 PHP
基于 Swoole 的微信扫码登录功能实现代码
Jan 15 PHP
bindParam和bindValue的区别以及在Yii2中的使用详解
Mar 12 PHP
PHP获取本周所有日期或者最近七天所有日期的方法
Jun 20 PHP
ThinkPHP 3.2.3实现加减乘除图片验证码
Dec 05 PHP
PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
Sep 09 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集成FCK的函数代码
2008/09/27 PHP
php通过session防url攻击方法
2014/12/10 PHP
PHP的几个常用加密函数
2016/02/03 PHP
JS中引用百度地图并将百度地图的logo和信息去掉
2013/09/29 Javascript
js获取会话框prompt的返回值的方法
2015/01/10 Javascript
JavaScript实现自动生成网页元素功能(按钮、文本等)
2015/11/21 Javascript
javascript随机抽取0-100之间不重复的10个数
2016/02/25 Javascript
jQuery实现 上升、下降、删除、添加一行代码
2017/03/06 Javascript
js指定步长实现单方向匀速运动
2017/07/17 Javascript
jQuery扇形定时器插件pietimer使用方法详解
2017/07/18 jQuery
浅谈Vue网络请求之interceptors实际应用
2018/02/28 Javascript
JavaScript模拟实现自由落体效果
2018/08/28 Javascript
[06:25]DOTA2英雄梦之声_第17期_大地之灵
2014/06/20 DOTA
[01:01:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第一场 3月4日
2021/03/11 DOTA
python选择排序算法的实现代码
2013/11/21 Python
Python中用startswith()函数判断字符串开头的教程
2015/04/07 Python
Python基于回溯法子集树模板实现8皇后问题
2017/09/01 Python
Python多线程中阻塞(join)与锁(Lock)使用误区解析
2018/04/27 Python
python3 flask实现文件上传功能
2020/03/20 Python
Python 绘制酷炫的三维图步骤详解
2019/07/12 Python
基于python使用tibco ems代码实例
2019/12/20 Python
基于python3抓取pinpoint应用信息入库
2020/01/08 Python
使用 Python 处理3万多条数据只要几秒钟
2020/01/19 Python
Pytest mark使用实例及原理解析
2020/02/22 Python
Django中F函数的使用示例代码详解
2020/07/06 Python
Python3爬虫关于识别检验滑动验证码的实例
2020/07/30 Python
关于canvas绘制模糊问题的解决方法
2019/09/24 HTML / CSS
美国体育用品在线:Modell’s Sporting Goods
2018/06/07 全球购物
销售人员中英文自荐信
2013/09/22 职场文书
法律进社区实施方案
2014/03/21 职场文书
学习经验交流会主持词
2014/04/01 职场文书
毕业生简历自我评价范文
2014/04/09 职场文书
机关党建工作汇报材料
2014/08/20 职场文书
同学会感言
2015/07/30 职场文书
社区挂职锻炼个人工作总结
2015/10/23 职场文书
Python matplotlib多个子图绘制整合
2022/04/13 Python