php 跨域、跨子域,跨服务器读取session
1、跨子域和跨服务器解决方式
Session主要分两部分:
一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在
另一个是标志着Session数据的Session Id,Session ID,就是那个 Session 文件的文件名,Session ID 是随机生成的,因此能保证唯一性和随机性,确保 Session 的安全。一般如果没有设置 Session 的生存周期,则 Session ID 存储在内存中,关闭浏览器后该 ID 自动注销,重新请求该页面后,重新注册一个 session ID。如果客户端没有禁用 Cookie,则 Cookie 在启动 Session 会话的时候扮演的是存储 Session ID 和 Session 生存期的角色。
两个不同的域名网站,想用同一个Session,就是牵扯到Session跨域问题!
默认情况下,各个服务器会各自分别对同一个客户端产生 SESSIONID,如对于同一个用户浏览器,A 服务器产生的 SESSION ID 是 11111111111,而B 服务器生成的则是222222。另外,PHP 的 SESSION数据都是分别保存在本服务器的文件系统中。想要共享 SESSION 数据,那就必须实现两个目标:
一个是各个服务器对同一个客户端产生的SESSION ID 必须相同,并且可通过同一个 COOKIE 进行传递,也就是说各个服务器必须可以读取同一个名为 PHPSESSID 的COOKIE;另一个是 SESSION 数据的存储方式/位置必须保证各个服务器都能够访问到。这两个目标简单地说就是多服务器(A、B服务器)共享客户端的 SESSION ID,同时还必须共享服务器端的 SESSION 数据。
第一个目标的实现其实很简单,只需要对 COOKIE 的域(domain)进行特殊地设置即可(setcookie()函数中的第4个参数),默认情况下,COOKIE 的域是当前服务器的域名/IP 地址,而域不同的话,各个服务器所设置的 COOKIE 是不能相互访问的,
1)跨子域
采用这种方式,跨域不行,但同一子域可以,如:aaa.cocoglp.com 和www.cocoglp.com 都属于域 .cocoglp.com是可以的,那么我们就可以设置 COOKIE 的域为 .cocoglp.com,这样 aaa.cocoglp.com、www.cocoglp.com等等都可以访问此COOKIE。这样各个服务器共享同一客户端 SESSION ID 的目的就达到了。
实现如下
-------------------------------------------------------------------------------------------------
这里有三种方式可以实现:
1.只要在php页面的最开始(要在任何输出之前,并且在session_start()之前)的地方进行以下设置
ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.mydomain.com');
ini_set('session.cookie_lifetime', '1800');
2.在php.ini里设置
session.cookie_path = /
session.cookie_domain = .mydomain.com
session.cookie_lifetime = 1800
3.在php页面最开始的地方(条件同1)调用函数
session_set_cookie_params(1800 , '/', '.mydomain.com');
这三种方式都是同样的效果。
这里我用第一种方法设置,分别在www.mydomain.com和sub.mydomain.com两个域名来测试,测试代码如下
sub1.php
<?php
//先访问的页面做设置
ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.mydomain.com');
ini_set('session.cookie_lifetime', '1800');
//
session_set_cookie_params(1800 , '/', '.mydomain.com');
session_start();
$_SESSION['sub1'] = 'sub1';
print_r($_SESSION);
?>
sub2.php
<?php
session_set_cookie_params(1800 , '/', '.mydomain.com');
session_start();
$_SESSION['sub2'] = 'sub2';
print_r($_SESSION);
?>
访问顺序:
(1)www.mydomain.com/sub1.php
页面输出:Array ( [sub1] => sub1 )
(2)sub.mydomain.com/sub2.php
页面输出:Array ( [sub1] => sub1 [sub2] => sub2 )
成功
----------------------------------------------------------------------------------------------------
第二个目标的实现可以使用数据库来保存SESSION 数据,这样各个服务器就可以方便地访问同一个数据源,获取相同的SESSION 数据了;或者是通过文件共享方式,如 NFS 方式(我的其他文章有如何配置nfs)
如果用数据库存储session数据的话,可能会有遗留问题,就是如果网站的访问量很大的话,SESSION 的读写会频繁地对数据库进行操作,可以把这个放在memcache中。存放在数据库里的前面有文章实现了。把数据库和memcache结合的思路,前面有了。如果单独用memcache存放session不太好,最好和数据库结合操作。
2)跨域解决
思路:用iframe解决,但是ff不支持,所以需要前面加上p3p协议。
P3P(Platform for Privacy Preferences Project),简单的说,就是个协议,通过其声明它是好人,允许我收集浏览器用户行为吧... 可现实中,大家都可以说自己是好人,背地里没准儿干啥坏事呢。这就是其分歧所在。[参考] 国内多数网站,都不关注这个 P3P。隐私问题可能没国外(微软的隐私声明)重视吧。
首先想到就是通过JS操作Cookie并让两个不同域的cookie能够相互访问,这样就可达到了上述的效果,具体实现过程大致可分以下两个步骤:
1、在A系统下成功登录后,利用JS动态创建一个隐藏的iframe,通过iframe的src属性将A域下的cookie值作为get参数重定向到B系统下b.jsp页面上;
- var _frm = document.createElement("iframe");
- _frm.style.display="none";
- _frm.src = "http://www.222.com/setcookie.php?mycookie=xxxxx";//此处xxx最好编码
- document.body.appendChild(_frm);
2、在B系统的setcookie.php页面中来获取A系统中所传过来的cookie值,并将所获取到值写入用户的cookie中,当然域是自己的了,这样就简单的实现了cookie跨域的访问; 不过这其中有个问题需要注意,就是在IE浏览器下这样操作不能成功,需要在setocokie.php页面中设置P3P HTTP Header就可以解决了(具体詳細信息可以参考:http://www.w3.org/P3P/),P3P设置代码为:
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
php 跨域、跨子域,跨服务器读取session的更多相关文章
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 【转】JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- JQuery 的跨域方法 可跨任意网站
JS的跨域问题,很多人在网上找其解决方法,教其用IFRAME去解决的文章很多,真有那么复杂吗?其实很简单的,如果你用JQUERY,一个GETJSON方法就搞定了,而且是一行代码搞定. 下面开始贴出方法 ...
- Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法
同源是指同样的协议.域名.port,三者都同样才属于同域.不符合上述定义的请求,则称为跨域. 相信每一个开发者都曾遇到过跨域请求的情况,尽管情况不一样,但问题的本质都能够归为浏览器出于安全考虑下的同源 ...
- JSONP跨域和CORS跨域的区别
跨域: 由于浏览器中的javascript的同源策略,同源策略会阻止一个域的JavaScript脚本和另一个域的内容进行交互. 同源:协议,域名,端口,三者有一个不同即为跨域. 解决跨域有以下多种方法 ...
- session跨域和ajax跨域名
后台跨域和ajax跨域名: 后台跨域: www.baidu.com 主域名(一级域名一般以www开头) news.baidu.com 二级域名 (a.test.com和b.test.com有相 ...
- Api之Cors跨域以及其他跨域方式
Web Api之Cors跨域以及其他跨域方式(三) 我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...
- SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析
最近在研究SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1.jquery ajax跨 ...
- 跨域问题解决方式(HttpClient安全跨域 & jsonp跨域)
1 错误场景 今天要把项目部署到外网的时候,出现了这种问题, 我把两个项目放到自己本机的tomcat下, 进行代码调试, 执行 都没有问题的, 一旦把我须要调用接口的项目B放到其它的server上, ...
随机推荐
- 【JavaScript Demo】回到顶部功能实现
随着网站的不断发展,需要展示的内容也越来越丰富,这导致网页上能展示的内容越来越多.当内容堆积影响了用户体验,就需考虑如何提升用户体验.在这一系列的改动中,“回到顶部”的功能成为了一个经典. 1.页面布 ...
- httpd配置.md
httpd-2.2 配置 监听端口和IP 配置文件: Listen [IP:]PORT 省略IP表示为0.0.0.0 Listen指令可重复出现多次 修改监听socket,重启服务进程方可生效 可以监 ...
- Mysql完全手册(笔记一,底层与内置函数)
1.MySQL由五个主子系统组成.协同工作,这五个主子系统是: (1)查询引擎 (2)存储管理器 (3)缓冲管理器 (4)事务管理器 (5)恢复管理器 查询引擎: 这个子系统包含三个相互关联的部件: ...
- docfx预热中
奋战了几个月,docfx终于有些像样了. 预览文档: http://aspnet.github.io/docfx/ 源代码正在准备开源中 Nuget包很快会发布 FAQ: Q: docfx是什么? A ...
- Splay
#include <cstdio> #include <iostream> using namespace std; *1e5;//nil表示不存在的节点 ][],flag[] ...
- 即使用ADO.NET,也要轻量级实体映射,比Dapper和Ormlite均快
不管出于什么原因,有时候框架人员摒弃了NH或EF,而使用原生数据库访问对象. 为了优美的编程,用上我写的轻量级映射扩展方法吧 目的:将SqlDataReader自动转换成T类型 代码如下: /// & ...
- 一分钟读懂MySQL分布式消息的处理
在很多MYSQL环境中,对于MYSQL的分布式事物处理一直是个难题,在当前互联网环境中,大多数应用系统是基于SOA的很多复杂接口之间的调用,并且事物之间的处理优先级也是有先后的,所以对于实际入库的数据 ...
- PRINCE2七大原则
我们先来回顾一下,PRINCE2七大原则分别是持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第三个原则:明确定义的角色和职责. 项目离不开人员,错误的人来了,合适的人没 ...
- NCBI database download
ascp -T -l 200M -i ~/.aspera/connect/etc/asperaweb_id_dsa.openssh --host=ftp-private.ncbi.nlm.nih.go ...
- 【Alpha版本】冲刺随笔汇总
[Alpha版本]冲刺-Day1 [Alpha版本]冲刺-Day2 [Alpha版本]冲刺-Day3 [Alpha版本]冲刺-Day4 [Alpha版本]冲刺-Day5 [Alpha版本]冲刺-Day ...