PHP 通过设置P3P头来实现跨域访问COOKIE

简介: 我的是CentOS的系统(Linux 内核) 编辑HOST  vi /etc/hosts 加入 127.0.0.1        www.a.com 127.0.0.1        www.

我的是CentOS的系统(Linux 内核)

编辑HOST 

vi /etc/hosts

加入

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"
); 
?>

然后:创建 a_getcookie.php 文件,内容如下:

<?php 
var_dump
($_COOKIE
); 
?>

最后:创建 b_setcookie.php 文件,内容如下:

<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

 

 

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

目录
相关文章
|
前端开发
解决前端ajax跨域请求不携带cookie信息JSESSIONID的问题
解决前端ajax跨域请求不携带cookie信息JSESSIONID的问题
|
4月前
|
安全 API PHP
PHP中实现CORS跨域资源共享的方法
通过这种方式,你可以在PHP应用中灵活地实现CORS,以支持跨域Web应用的需求。
324 15
|
存储 缓存 数据处理
php学习笔记-php会话控制,cookie,session的使用,cookie自动登录和session 图书上传信息添加和修改例子-day07
本文介绍了PHP会话控制及Web常用的预定义变量,包括`$_REQUEST`、`$_SERVER`、`$_COOKIE`和`$_SESSION`的用法和示例。涵盖了cookie的创建、使用、删除以及session的工作原理和使用,并通过图书上传的例子演示了session在实际应用中的使用。
php学习笔记-php会话控制,cookie,session的使用,cookie自动登录和session 图书上传信息添加和修改例子-day07
axios允许跨域cookie
axios允许跨域cookie
150 4
|
Ubuntu PHP Apache
蓝易云 - 如何在Ubuntu 22.04上安装PHP8.1并设置本地开发环境
以上就是在Ubuntu 22.04上安装PHP 8.1并设置本地开发环境的步骤。
753 2
|
存储 缓存 安全
【PHP开发专栏】PHP Cookie与Session管理
【4月更文挑战第30天】本文介绍了PHP中的Cookie和Session管理。Cookie是服务器发送至客户端的数据,用于维持会话状态,可使用`setcookie()`设置和`$_COOKIE`访问。Session数据存于服务器,更安全且能存储更多数据,通过`session_start()`启动,`$_SESSION`数组操作。根据需求选择Cookie(跨会话共享)或Session(单会话存储)。实战中常组合使用,如Cookie记住登录状态,Session处理购物车。理解两者原理和应用场景能提升Web开发技能。
229 2
|
存储 安全 关系型数据库
安全开发-PHP应用&留言板功能&超全局变量&数据库操作&第三方插件引用&后台模块&Session&Cookie&Token&身份验证&唯一性
安全开发-PHP应用&留言板功能&超全局变量&数据库操作&第三方插件引用&后台模块&Session&Cookie&Token&身份验证&唯一性
296 0
|
PHP
php curl获取cookie
php curl获取cookie
180 0
|
5月前
|
关系型数据库 MySQL PHP
PHP和Mysql前后端交互效果实现
本文介绍了使用PHP连接MySQL数据库的基本函数及其实现案例。内容涵盖数据库连接、选择数据库、执行查询、获取结果等常用操作,并通过用户登录和修改密码的功能实例,展示了PHP与MySQL的交互过程及代码实现。
392 0
PHP和Mysql前后端交互效果实现
|
10月前
|
关系型数据库 MySQL Linux
查看Linux、Apache、MySQL、PHP版本的技巧
以上就是查看Linux、Apache、MySQL、PHP版本信息的方法。希望这些信息能帮助你更好地理解和使用你的LAMP技术栈。
506 17