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安装全攻略:APACHE
Oct 09 PHP
用php和MySql来与ODBC数据连接
Oct 09 PHP
php自动给网址加上链接的方法
Jun 02 PHP
以文件形式缓存php变量的方法
Jun 26 PHP
smarty中常用方法实例总结
Aug 07 PHP
php图片水印添加、压缩、剪切的封装类实现
Apr 18 PHP
基于php实现随机合并数组并排序(原排序)
Nov 26 PHP
CodeIgniter自定义控制器MY_Controller用法分析
Jan 20 PHP
在Mac OS上搭建PHP的Yii框架及相关测试环境
Feb 14 PHP
thinkphp3.2实现在线留言提交验证码功能
Jul 19 PHP
php如何计算两坐标点之间的距离
Dec 29 PHP
PHP常量及变量区别原理详解
Aug 14 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编写大型网站问题集
2007/03/06 PHP
php数组函数序列之in_array() 查找数组值是否存在
2011/10/29 PHP
jquery 笔记 事件
2011/11/02 Javascript
js如何判断用户是否是用微信浏览器
2014/06/05 Javascript
用jquery实现动画跳到顶部和底部(这个比较简单)
2014/09/01 Javascript
node.js中的forEach()是同步还是异步呢
2015/01/29 Javascript
jQuery实现的超链接提示效果示例【附demo源码下载】
2016/09/09 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
利用Vue.js实现求职在线之职位查询功能
2017/07/03 Javascript
vue实现导航栏效果(选中状态刷新不消失)
2017/12/13 Javascript
详解基于DllPlugin和DllReferencePlugin的webpack构建优化
2018/06/28 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
微信小程序实现音频文件播放进度的实例代码
2020/03/02 Javascript
微信小程序学习总结(三)条件、模板、文件引用实例分析
2020/06/04 Javascript
分享8个JavaScript库可更好地处理本地存储
2020/10/12 Javascript
使用Python编写提取日志中的中文的脚本的方法
2015/04/30 Python
利用Python生成文件md5校验值函数的方法
2017/01/10 Python
git使用.gitignore设置不生效或不起作用问题的解决方法
2017/06/01 Python
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
关于python2 csv写入空白行的问题
2018/06/22 Python
python pandas 对时间序列文件处理的实例
2018/06/22 Python
PyQt QCombobox设置行高的方法
2019/06/20 Python
numpy实现神经网络反向传播算法的步骤
2019/12/24 Python
Python sqlite3查询操作过程解析
2020/02/20 Python
python3中编码获取网页的实例方法
2020/11/16 Python
Django数据统计功能count()的使用
2020/11/30 Python
Reebok俄罗斯官方网上商店:购买锐步运动服装和鞋子
2016/09/26 全球购物
兰蔻美国官网:Lancome美国
2017/04/25 全球购物
优秀实习自我鉴定
2013/12/04 职场文书
财务经理的岗位职责
2013/12/17 职场文书
工厂门卫岗位职责范本
2014/04/04 职场文书
十佳文明家庭事迹
2014/05/25 职场文书
教师国庆节演讲稿范文2014
2014/09/21 职场文书
党支部培养考察意见
2015/06/02 职场文书
java多态注意项小结
2021/10/16 Java/Android
Java 超详细讲解IO操作字节流与字符流
2022/03/25 Java/Android