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新手上路(十一)
Oct 09 PHP
网友原创的PHP模板类代码
Sep 07 PHP
php安全开发 添加随机字符串验证,防止伪造跨站请求
Feb 14 PHP
PHP实现HTML页面静态化的方法
Nov 04 PHP
Laravel框架实现发送短信验证功能代码
Jun 06 PHP
Yii2增删改查之查询 where参数详细介绍
Aug 08 PHP
PHP文件上传处理案例分析
Oct 15 PHP
PHP用正则匹配form表单中所有元素的类型和属性值实例代码
Feb 28 PHP
PHP用户注册邮件激活账户的实现代码
May 31 PHP
PHP定义字符串的四种方式详解
Feb 06 PHP
PHP中的Iterator迭代对象属性详解
Apr 12 PHP
PHP 判断字符串是中文还是英文, 或者是中英混合
Mar 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 操作符与控制结构
2012/03/07 PHP
php+mysql查询实现无限下级分类树输出示例
2016/10/03 PHP
纯JavaScript实现的完美渐变弹出层效果代码
2010/04/02 Javascript
可以将word转成html的js代码
2010/04/11 Javascript
javascript小组件 原生table排序表格脚本(兼容ie firefox opera chrome)
2012/07/25 Javascript
如何让页面加载完成后执行js
2013/06/26 Javascript
移动端点击图片放大特效PhotoSwipe.js插件实现
2016/08/25 Javascript
jQuery下拉菜单的实现代码
2016/11/03 Javascript
javascript设计模式之策略模式学习笔记
2017/02/15 Javascript
VUE前端cookie简单操作
2017/10/17 Javascript
详解js模板引擎art template数组渲染的方法
2018/10/09 Javascript
angular学习之动态创建表单的方法
2018/12/07 Javascript
微信小程序五子棋游戏AI实现方法【附demo源码下载】
2019/02/20 Javascript
layUI的验证码功能及校验实例
2019/10/25 Javascript
ant design实现圈选功能
2019/12/17 Javascript
JS如何寻找数组中心索引过程解析
2020/06/01 Javascript
Element Tooltip 文字提示的使用示例
2020/07/26 Javascript
使用python编写批量卸载手机中安装的android应用脚本
2014/07/21 Python
Python使用Paramiko模块编写脚本进行远程服务器操作
2016/05/05 Python
python利用正则表达式搜索单词示例代码
2017/09/24 Python
Go/Python/Erlang编程语言对比分析及示例代码
2018/04/23 Python
react+django清除浏览器缓存的几种方法小结
2019/07/17 Python
Django 允许局域网中的机器访问你的主机操作
2020/05/13 Python
css3实现可滑动跳转的分页插件示例
2014/05/08 HTML / CSS
美的官方商城:Midea
2016/09/14 全球购物
Monnier Freres中文官网:法国领先的奢侈品配饰在线零售商
2017/11/01 全球购物
Ibatis中如何提高SQL Map的性能
2013/05/11 面试题
销售人员自我评价怎么写
2013/09/19 职场文书
水污染治理工程专业求职信
2014/06/14 职场文书
护理学专业求职信
2014/06/29 职场文书
农村党支部书记四风问题个人对照检查材料
2014/09/21 职场文书
2014年业务工作总结
2014/11/17 职场文书
2014保险公司内勤工作总结
2014/12/16 职场文书
材料采购员岗位职责
2015/04/03 职场文书
《这片土地是神圣的》教学反思
2016/02/16 职场文书
MySQL count(*)统计总数问题汇总
2022/09/23 MySQL