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 是不能相互访问的,
采用这种方式,跨域不行,但同一子域可以,如: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');
这三种方式都是同样的效果。
设置后,我在sub1和sub2下测试后,发现不起作用,而且奇怪的是,每在一个子域下设置session后,在另一个子域下调用session后,发现session会被清空。测试代码如下
sub1.php
<?php
session_set_cookie_params(1800 , '/', '.mydomain.com');
session_start();
print_r($_SESSION);
$_SESSION['sub1'] = 'sub1';
print_r($_SESSION);
?>
sub2.php
<?php
session_set_cookie_params(1800 , '/', '.mydomain.com');
session_start();
print_r($_SESSION);
$_SESSION['sub2'] = 'sub2';
print_r($_SESSION);
?>
百度+google了半天,发现原来是suhosin的问题。 这也是偶然看到的 https://bugs.php.net/bug.php?id=43682
这里是suhosin 的官网,好像是韩国人开发的 http://www.hardened-php.net/suhosin/configuration.html
suhosin是增加php安全的服务器扩展,比如加密session在服务器上的存储文件,加密cookie等。那么suhosin如何对session保护的呢?
suhosin.session.cryptdocroot
官方解释:Flag that decides if the transparent session encryption key depends on the Documentroot field.
suhosin.cookie.cryptdocroot
官方解释:Flag that decides if the transparent cookie encryption key depends on the Documentroot field.
只要把这两个关闭就可以了,但是这里关闭的方法要注意了
我用
ini_set("suhosin.session.cryptdocroot", "Off");
ini_set("suhosin.cookie.cryptdocroot", "Off");
没有效果,为什么呢?
因为这两个配置根本不在php.ini里(查找看到是在/etc/php5/conf.d/suhosin.ini)
打开suhosin.ini
suhosin.session.cryptdocroot = off
suhosin.cookie.cryptdocroot = off
关闭后,重启apache,成功
----------------------------------------------------------------------------------------------------
第二个目标的实现可以使用数据库来保存SESSION 数据,这样各个服务器就可以方便地访问同一个数据源,获取相同的SESSION 数据了;或者是通过文件共享方式,如 NFS 方式(我的其他文章有如何配置nfs)
如果用数据库存储session数据的话,可能会有遗留问题,就是如果网站的访问量很大的话,SESSION 的读写会频繁地对数据库进行操作,可以把这个放在memcache中。存放在数据库里的前面有文章实现了。把数据库和memcache结合的思路,前面有了。如果单独用memcache存放session不太好,最好和数据库结合操作。
2)跨域解决
思路:用iframe解决,但是ff不支持,所以需要前面加上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);
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