網上看了別人介紹的一片文章,說使用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)一文裏對這個狀況有描述。