PHP如何利用P3P实现跨域

有别于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。

(0)

相关推荐

  • 使用JSON实现数据的跨域传输的php代码

    后台profile.php代码: 复制代码 代码如下: <?php $arr = array( 'firstname' => iconv('gb2312', 'utf-8', '非诚'), 'lastname' => iconv('gb2312', 'utf-8', '勿扰'), 'contact' => array( 'email' =>'fcwr@jb51.net', 'website' =>'http://www.jb51.net', ) ); //将一个数组JS

  • PHP中运用jQuery的Ajax跨域调用实现代码

    可以在页面定义一个调用方法,如下: 复制代码 代码如下: function getData(){ $.getJSON("http://123.123.123.123/?callback=?", { "m":"data",// 指定php的文件名字 "act":"getdata",// 指定php文件中的方法 "name":"问题儿童"// 传入的参数 }, funct

  • php jq jquery getJSON跨域提交数据完整版

    前端请求端: 复制代码 代码如下: <script> $(function() { $.getJSON('http://test.com/aa.php?callback=?',{classid:1,num:2},function(json){ alert(json.key); }); }); </script> 被请求端: 复制代码 代码如下: $classid=$_GET['classid']; $num=$_GET['num']; if($classid&&$n

  • ThinkPHP框架实现session跨域问题的解决方法

    ThinkPHP的session跨域问题很多开发者都遇到过! 其实不管是ThinkPHP还是php本身,在解决session跨域问题的时候都需要设置session.cookie_domain. 在ThinkPHP里,需要修改配置文件conf/config.php 在第一行加上: ini_set('session.cookie_domain', ".domain.com");//跨域访问Session 经过总结,针对session跨域这一问题的解决方法主要有以下几种: 第一种情况:如果你

  • 两种简单的跨域方法(jsonp、php)

    Ajax不能跨域,比如您是www.baidu.com,您就不能请求www.163.com的文件.但您可以请求www.baidu.com/1.json.ent.baidu.com/1.json.这是因为安全原因,对于任何后台语言来说.服务器程序来说,所有的XHR类型的请求,如果来自其他的服务器,将不予应答. 一.使用jsonp JSONP是JSON with Padding的略称.它是一个非官方的协议,出处不可考,它允许在服务器端集成Script tags返回至客户端,通过javascript c

  • php跨域调用json的例子

    JSON和XML没什么太大区别,不过JSON有一个更大范围的应用,那就是,跨域的数据调用.由于安全性问题,AJAX不支持跨域调用,这样要调用不同域名下的数据,很麻烦.下面这个例子,足以展示php用json如何进跨域调用了. index.html 复制代码 代码如下: <script type="text/javascript"> function getProfile(str) {      var arr = str;      document.getElementBy

  • 浅析php中jsonp的跨域实例

    我们现在www.test.com这个域名下面有这么个html文件testjsonp.html: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtm

  • php跨域cookie共享使用方法

    A 机器所在的域:a1.main.com,A 有应用 main.phpB 机器所在的域:b1.test.com,B 有应用 test.php 1.在 main.php 里设置 cookie 的时候, cookie 的设置方法如下: 复制代码 代码如下: setcookie( "TestCookie",  "okol",  time() + 3600,  "/", "b1.test.com", 1 ); 这样在 test.php

  • PHP如何实现跨域

    因工作需要,客户端软件的一些界面用HTML+CSS+Javascript实现,这些文件放在客户端本地,以本地文件形式加载,但是有些情况下,需要连接服务器取得一些信息,如果不做任何处理,则请求失败,返回的信息如下: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 这是因为浏览器跨域策略起作用,阻止了

  • PHP防止跨域提交表单

    在写用户注册的时候,一定要主要你的表单是否可以跨域提交.php中解决的方法:1.除了在页面做好表但验证之外,还要在提交的服务段的数据进行验证.验证的主要代码如下: 复制代码 代码如下: $servername=$_SERVER['SERVER_NAME'];//当前运行脚本所在服务器主机的名字.  $sub_from=$_SERVER["HTTP_REFERER"];//链接到当前页面的前一页面的 URL 地址  $sub_len=strlen($servername);//统计服务器

随机推荐