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字符串函数系列之nl2br(),在字符串中的每个新行 (\n) 之前插入 HTML 换行符br
Nov 10 PHP
基于php split()函数的用法详解
Jun 05 PHP
destoon找回管理员密码的方法
Jun 21 PHP
PHP+FastCGI+Nginx配置PHP运行环境
Aug 07 PHP
php发送与接收流文件的方法
Feb 11 PHP
jquery+php实现导出datatables插件数据到excel的方法
Jul 06 PHP
PHP数据库表操作的封装类及用法实例详解
Jul 12 PHP
PHP实现QQ快速登录的方法
Sep 28 PHP
php 输出json及显示json中的中文汉字详解及实例
Nov 09 PHP
laravel 5.3中自定义加密服务的方案详解
May 09 PHP
PHP设计模式之观察者模式定义与用法示例
Aug 04 PHP
php二维数组按某个键值排序的实例讲解
Feb 15 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
音乐朗读剧《MARS RED》2021年TV动画化决定!
2020/03/06 日漫
1 Tube Radio
2021/03/02 无线电
一段php加密解密的代码
2007/07/16 PHP
使用PHP实现微信摇一摇周边红包
2016/01/04 PHP
原生php实现excel文件读写的方法分析
2018/04/25 PHP
禁止js文件缓存的代码
2010/04/09 Javascript
JS打开新窗口的2种方式
2013/04/18 Javascript
Javascript中的异步编程规范Promises/A详细介绍
2014/06/06 Javascript
了不起的node.js读书笔记之例程分析
2014/12/22 Javascript
javascript实现根据3原色制作颜色选择器的方法
2015/07/17 Javascript
javascript自定义in_array()函数实现方法
2015/08/03 Javascript
js点击文本框弹出可选择的checkbox复选框
2016/02/03 Javascript
jQuery中常用动画效果函数(日常整理)
2016/09/17 Javascript
通过扫描二维码打开app的实现代码
2016/11/10 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
Vuejs入门教程之Vue生命周期,数据,手动挂载,指令,过滤器
2017/04/19 Javascript
纯原生js实现贪吃蛇游戏
2020/04/16 Javascript
js判断数组是否包含某个字符串变量的实例
2017/11/24 Javascript
Angular整合zTree的示例代码
2018/01/24 Javascript
JavaScript获取移动设备型号的实现代码(JS获取手机型号和系统)
2018/03/10 Javascript
JS隐藏号码中间4位代码实例
2019/04/09 Javascript
2020京东618叠蛋糕js脚本(亲测好用)
2020/06/02 Javascript
[57:22]完美世界DOTA2联赛PWL S2 FTD vs PXG 第二场 11.27
2020/12/01 DOTA
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
Python工程师面试题 与Python Web相关
2016/01/14 Python
Python 遍历列表里面序号和值的方法(三种)
2017/02/17 Python
Python上下文管理器和with块详解
2017/09/09 Python
动态规划之矩阵连乘问题Python实现方法
2017/11/27 Python
mac使用python识别图形验证码功能
2020/01/10 Python
pytorch使用tensorboardX进行loss可视化实例
2020/02/24 Python
Python 使用office365邮箱的示例
2020/10/29 Python
html5指南-6.如何创建离线web应用程序实现离线访问
2013/01/07 HTML / CSS
KIKO MILANO俄罗斯官网:意大利领先的化妆品和护肤品品牌
2021/01/09 全球购物
毕业生如何写自我鉴定
2014/03/15 职场文书
新员工试用期工作总结2015
2015/05/28 职场文书
学校食堂管理制度
2015/08/04 职场文书