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页面上;

  1. var _frm = document.createElement("iframe");
  2. _frm.style.display="none";
  3. _frm.src = "http://www.222.com/setcookie.php?mycookie=xxxxx";//此处xxx最好编码
  4. document.body.appendChild(_frm);

2、在B系统的setcookie.php页面中来获取A系统中所传过来的cookie值,并将所获取到值写入用户的cookie中,当然域是自己的了,这样就简单的实现了cookie跨域的访问; 不过这其中有个问题需要注意,就是在IE浏览器下这样操作不能成功,需要在setocokie.php页面中设置P3P HTTP Header就可以解决了(具体詳細信息可以参考:http://www.w3.org/P3P/),P3P设置代码为:

 
     header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');//ecshop这么设置的
 
上面cp代码的含义
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

php 跨域、跨子域,跨服务器读取session的更多相关文章

  1. JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  2. 【转】JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  3. JQuery 的跨域方法 可跨任意网站

    JS的跨域问题,很多人在网上找其解决方法,教其用IFRAME去解决的文章很多,真有那么复杂吗?其实很简单的,如果你用JQUERY,一个GETJSON方法就搞定了,而且是一行代码搞定. 下面开始贴出方法 ...

  4. Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法

    同源是指同样的协议.域名.port,三者都同样才属于同域.不符合上述定义的请求,则称为跨域. 相信每一个开发者都曾遇到过跨域请求的情况,尽管情况不一样,但问题的本质都能够归为浏览器出于安全考虑下的同源 ...

  5. JSONP跨域和CORS跨域的区别

    跨域: 由于浏览器中的javascript的同源策略,同源策略会阻止一个域的JavaScript脚本和另一个域的内容进行交互. 同源:协议,域名,端口,三者有一个不同即为跨域. 解决跨域有以下多种方法 ...

  6. session跨域和ajax跨域名

    后台跨域和ajax跨域名: 后台跨域: www.baidu.com   主域名(一级域名一般以www开头) news.baidu.com   二级域名 (a.test.com和b.test.com有相 ...

  7. Api之Cors跨域以及其他跨域方式

    Web Api之Cors跨域以及其他跨域方式(三)   我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...

  8. SSO单点登录、跨域重定向、跨域设置Cookie、京东单点登录实例分析

    最近在研究SSO单点登录技术,其中有一种就是通过js的跨域设置cookie来达到单点登录目的的,下面就已京东商城为例来解释下跨域设置cookie的过程 涉及的关键知识点: 1.jquery ajax跨 ...

  9. 跨域问题解决方式(HttpClient安全跨域 &amp; jsonp跨域)

    1 错误场景 今天要把项目部署到外网的时候,出现了这种问题, 我把两个项目放到自己本机的tomcat下, 进行代码调试, 执行 都没有问题的, 一旦把我须要调用接口的项目B放到其它的server上, ...

随机推荐

  1. 并查集补集作法 codevs 1069 关押罪犯

    1069 关押罪犯 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description ...

  2. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  3. WCF学习笔记一

    Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...

  4. URL类

    java.net.URL类是对统一资源定位符(如http://www.lolcats.com)的抽象.它扩展了java.lang.Object,是一个final类.它采用策略模式,协议处理器(prot ...

  5. 【BZOJ 3051】【UOJ #57】【WC 2013】平面图

    http://www.lydsy.com/JudgeOnline/problem.php?id=3051 http://uoj.ac/problem/57 这道题需要平面图转对偶图,点定位,最小生成树 ...

  6. JavaScript中map函数和filter的简单举例(转)

    js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...

  7. 【BZOJ-4082】Surveillance 树链剖分 LCA + 贪心

    4082: [Wf2014]Surveillance Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 260  Solved: 100[Submit][ ...

  8. HttpClient 4.3 使用

    httpclient的api变化很快,本篇随笔记录自己使用4.3.6版本时所做的设置.版本虽然不是最新,但达到了目的就行. maven依赖: <dependency> <groupI ...

  9. #MySQL 5.7.8 支持Json类型

    As of MySQL 5.7.8, MySQL supports a native JSON data type that enables efficient access to data in J ...

  10. Protobuf for Python测试保存和读取文件

    安装pip, setuptools, and wheel 如果已经从python.org,安装啦Python 2 >=2.7.9 or Python 3 >=3.4 ,那么就已经有啦pip ...