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 相关文章推荐
基于mysql的论坛(6)
Oct 09 PHP
php插入中文到sqlserver 2008里出现乱码的解决办法分享
Jul 19 PHP
php中通过数组进行高效随机抽取指定条记录的算法
Sep 09 PHP
PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁
Jun 09 PHP
修改ThinkPHP缓存为Memcache的方法
Jun 25 PHP
php数组随机排序实现方法
Jun 13 PHP
如何通过Linux命令行使用和运行PHP脚本
Jul 29 PHP
使用PHP如何实现高效安全的ftp服务器(一)
Dec 20 PHP
PHP批量去除BOM头内容信息代码
Mar 11 PHP
Yii2中设置与获取别名的函数(setAlias和getAlias)用法分析
Jul 25 PHP
php如何比较两个浮点数是否相等详解
Feb 12 PHP
PHP常量define和const的区别详解
May 18 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检测网页是否被百度收录的函数代码
2013/10/09 PHP
PHP获取一年有几周以及每周开始日期和结束日期
2015/08/06 PHP
windows7配置Nginx+php+mysql的详细教程
2016/09/04 PHP
javascript 当前日期加(天、周、月、年)
2009/08/09 Javascript
javascript &amp;&amp;和||运算法的另类使用技巧
2009/11/28 Javascript
javascript 表格内容排序 简单操作示例代码
2014/01/03 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
js中的json对象详细介绍
2014/10/29 Javascript
JS 打印功能代码可实现打印预览、打印设置等
2014/10/31 Javascript
用js读、写、删除Cookie代码续篇
2014/12/03 Javascript
实例讲解javascript注册事件处理函数
2016/01/09 Javascript
javascript HTML+CSS实现经典橙色导航菜单
2016/02/16 Javascript
jQuery实现的放大镜效果示例
2016/09/13 Javascript
jquery动态创建div与input的实例代码
2016/10/12 Javascript
Bootstrap基本组件学习笔记之导航(10)
2016/12/07 Javascript
利用JavaScript在网页实现八数码启发式A*算法动画效果
2017/04/16 Javascript
深入理解Angular.JS中的Scope继承
2017/06/04 Javascript
在 Angular 中使用Chart.js 和 ng2-charts的示例代码
2017/08/17 Javascript
微信小程序wx.request实现后台数据交互功能分析
2017/11/25 Javascript
Vue Echarts实现可视化世界地图代码实例
2019/05/07 Javascript
JS实现的简单tab切换功能完整示例
2019/06/20 Javascript
vue+element表格导出为Excel文件
2019/09/26 Javascript
[43:18]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.22
2019/09/05 DOTA
python fabric实现远程操作和部署示例
2014/03/25 Python
python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
2014/04/25 Python
Python反射用法实例简析
2017/12/22 Python
关于Django显示时间你应该知道的一些问题
2017/12/25 Python
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
2018/01/23 Python
利用python在excel里面直接使用sql函数的方法
2019/02/08 Python
使用Python3+PyQT5+Pyserial 实现简单的串口工具方法
2019/02/13 Python
pytorch三层全连接层实现手写字母识别方式
2020/01/14 Python
Python学习笔记之装饰器
2020/08/06 Python
英国受欢迎的运动鞋和街头服装商店:Footasylum
2018/06/12 全球购物
写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度
2015/11/18 面试题
《姥姥的剪纸》教学反思
2014/02/25 职场文书
工程建设实施方案
2014/03/14 职场文书