zoukankan      html  css  js  c++  java
  • 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协议。

    首先想到就是通过JS操作Cookie并让两个不同域的cookie能够相互访问,这样就可达到了上述的效果,具体实现过程大致可分以下两个步骤:

    1、在A系统下成功登录后,利用JS动态创建一个隐藏的iframe,通过iframe的src属性将A域下的cookie值作为get参数重定向到B系统下b.jsp页面上;

    [javascript] view plaincopyprint?
     
    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
  • 相关阅读:
    javascript 阻止多次点击造成的轮播混乱
    javascript切换效果
    关于bxslider在点击左右按钮之后不能自动切换的问题解决
    javascript 多图无缝切换
    javascript 多图无缝切换
    javascript 切换动画
    javascript无缝全屏轮播
    jquery css3 手机菜单动画综合版
    jquery模仿css3延迟效果
    Mysql的存储过程总结
  • 原文地址:https://www.cnblogs.com/roam/p/4468373.html
Copyright © 2011-2022 走看看