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 相关文章推荐
PHP4中实现动态代理
Oct 09 PHP
PHP 服务器配置(使用Apache及IIS两种方法)
Jun 01 PHP
PHP生成Flash动画的实现代码
Mar 12 PHP
ajax+php打造进度条代码[readyState各状态说明]
Apr 12 PHP
PHP字符串函数系列之nl2br(),在字符串中的每个新行 (\n) 之前插入 HTML 换行符br
Nov 10 PHP
php $_SERVER windows系统与linux系统下的区别说明
Feb 14 PHP
php获取Google机器人访问足迹的方法
Apr 15 PHP
基于PHP+jQuery+MySql实现红蓝(顶踩)投票代码
Aug 25 PHP
各种快递查询--Api接口
Apr 26 PHP
轻松实现php文件上传功能
Feb 17 PHP
laravel框架中间件 except 和 only 的用法示例
Jul 12 PHP
php 实现简单的登录功能示例【基于thinkPHP框架】
Dec 02 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
zf框架的数据库追踪器使用示例
2014/03/13 PHP
PHP+MySQL修改记录的方法
2015/01/21 PHP
php中使用url传递数组的方法
2015/02/11 PHP
PHP邮箱验证示例教程
2016/06/01 PHP
Yii2中使用asset压缩js,css文件的方法
2016/11/24 PHP
JSON 入门指南 想了解json的朋友可以看下
2009/08/26 Javascript
jQuery 第二课 操作包装集元素代码
2010/03/14 Javascript
javascript权威指南 学习笔记之null和undefined
2011/09/25 Javascript
js中的onchange和onpropertychange (onchange无效的解决方法)
2014/03/08 Javascript
微信JSSDK上传图片
2015/08/23 Javascript
jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码
2016/12/05 Javascript
jquery实现数字输入框
2017/02/22 Javascript
JS闭包的几种常见形式实例详解
2017/09/16 Javascript
vue router 源码概览案例分析
2018/10/09 Javascript
express框架中使用jwt实现验证的方法
2019/08/25 Javascript
在vue项目中使用codemirror插件实现代码编辑器功能
2019/08/27 Javascript
Vue.js实现大转盘抽奖总结及实现思路
2019/10/09 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
[00:38]TI珍贵瞬间系列(二):笑
2020/08/26 DOTA
初学Python实用技巧两则
2014/08/29 Python
python搜索指定目录的方法
2015/04/29 Python
Python检测QQ在线状态的方法
2015/05/09 Python
如何实现删除numpy.array中的行或列
2018/05/08 Python
Python常用字符串替换函数strip、replace及sub用法示例
2018/05/21 Python
Python 实现异步调用函数的示例讲解
2018/10/14 Python
python实现批量nii文件转换为png图像
2019/07/18 Python
pytorch索引查找 index_select的例子
2019/08/18 Python
tensorflow 报错unitialized value的解决方法
2020/02/06 Python
切尔西足球俱乐部官方网上商店:Chelsea FC
2019/06/17 全球购物
The Athlete’s Foot新西兰:新西兰最大的运动鞋零售商
2019/12/23 全球购物
自动化专业个人求职信范文
2013/12/30 职场文书
党校个人自我鉴定范文
2014/03/28 职场文书
2015年见习期工作总结
2014/12/12 职场文书
创业项目大全(适合在家创业的项目)
2019/08/15 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers
动画电影《龙珠超 超级英雄》延期上映
2022/03/20 日漫