p3p

网上看了别人介绍的一片文章,说使用P3P能够完成跨域COOKIE操做,感受很COOL,不过没有提供源代码,我胡乱写了一下,你们看看。php

实际工做中,相似这样的要求不少,好比说,咱们有两个域名,咱们想实如今一个域名登陆后,能自动完成另外一个域名的登陆,也就是PASSPORT的功能。html

我只写一个大概,为了测试的方便,先编辑hosts文件,加入测试域名(C:\WINDOWS\system32\drivers\etc\hosts)node

127.0.0.1        www.a.com
127.0.0.1        www.b.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"); 
?>
cookie

而后:建立 a_getcookie.php 文件,内容以下:session

<?php 
var_dump($_COOKIE
); 
?>
dom

最后:建立 b_setcookie.php 文件,内容以下:ide

<script src="http://www.a.com/a_setcookie.php?id=www.b.com"></script>测试

----------------------------

三个文件建立完毕后,咱们经过浏览器依次访问:

http://www.b.com/b_setcookie.php
http://www.a.com/a_getcookie.php

咱们会发现,在访问b.com域的时候,咱们并无在a.com域设置上cookie值。

而后咱们修改一下a_setcookie.php文件,去掉注释符号,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值。

末了补充一句,彷佛只有IE对跨域访问COOKIE限制比较严格,上述代码在FIREFOX下测试,即便不发送P3P头信息,也能成功。

==========================================

经过Fiddler能够方便的知道上面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.


Validate at: http://www.w3.org/P3P/validator.html
Learn more at: http://www.fiddlertool.com/redir/?id=p3pinfo



文章说的主要是跨域设置COOKIE的状况,若是是跨域读取COOKIE的状况,只要保证在对应设置COOKIE的时候设置P3P便可,不然在读取的事情IE会屏蔽跨域COOKIE。在Internet Explorer Cookie Internals (FAQ)一文里对这个状况有描述。

参考文档:http://www.w3.org/P3P/

相关文章
相关标签/搜索