跨域、跨子域,跨服务器读取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 是不能相互访问的,

    采用这种方式,跨域不行,但同一子域可以,如: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页面上;

Js代码   收藏代码
  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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Session { //mysql的主机地址 const db_host = "localhost"; //需要第三方指定ip地址 //数据库用户名 const db_user = "root"; //需要第三方指定自己的用户名 //数据库密码 const db_pwd = ""; //需要第三方指定自己的库据库密码 //数据库 const db_name = "thinkphp"; //需要第三方指定数据库 //数据库表 const db_table = "tbl_session"; //需要第三方指定数据表 //mysql-handle private $db_handle; //session-lifetime private $lifeTime; function open($savePath, $sessName) { // get session-lifetime $this--->lifeTime = get_cfg_var("session.gc_maxlifetime"); // open database-connection $db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd); $dbSel = @mysql_select_db(self::db_name, $db_handle); // return success if(!$db_handle || !$dbSel) return false; $this->db_handle = $db_handle; return true; } function close() { $this->gc(ini_get('session.gc_maxlifetime')); // close database-connection return @mysql_close($this->db_handle); } function read($sessID) { // fetch session-data $res = @mysql_query("SELECT session_data AS d FROM ".self::db_table." WHERE session_id = '$sessID' AND session_expires > ".time(), $this->db_handle); // return data or an empty string at failure if($row = @mysql_fetch_assoc($res)) return $row['d']; return ""; } function write($sessID, $sessData) { // new session-expire-time $newExp = time() + $this->lifeTime; // is a session with this id in the database? $res = @mysql_query("SELECT * FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle); // if yes, if(@mysql_num_rows($res)) { // ...update session-data @mysql_query("UPDATE ".self::db_table." SET session_expires = '$newExp', session_data = '$sessData' WHERE session_id = '$sessID'", $this->db_handle); // if something happened, return true if(@mysql_affected_rows($this->db_handle)) return true; } else // if no session-data was found, { // create a new row @mysql_query("INSERT INTO ".self::db_table." ( session_id, session_expires, session_data) VALUES( '$sessID', '$newExp', '$sessData')", $this->db_handle); // if row was created, return true if(@mysql_affected_rows($this->db_handle)) return true; } // an unknown error occured return false; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值