ZEND2:跨域会话

时间:2017-09-01 13:09:47

标签: php session cookies zend-framework2 session-cookies

我正在尝试在子域上使用会话数据。

EX:

www.mywebsite.com

test.mywebsite.com

当我尝试将www.mywebsite.com的会话数据用于子域时,无法访问所有会话信息。

设置会话

public function homeAction($value='')
{
    $domain = eregi_replace('^WWW', '', "$_SERVER[HTTP_HOST]");
    $subdomain = substr($domain,0);

    $request = $this->getRequest()->getHeaders()->get('Set-Cookie')->foo = 'bar';
    $response = $this->getResponse()->getCookie()->baz = 'test';


    $userSession = new Container('userinfo');
    $userSession->email = 'admin@admin.in'; // write session 
    $userSession->password = 'e10adc3949ba59abbe56e057f20f883eaa';

    $dmn_link = 'http://app'    . $subdomain . '/auth';
    $www_link = 'http://www' . $domain . '/career';

    echo "<br/><a target=\"_blank\" href=\"$www_link\">$www_link</a>\n";
    echo "<br/><a target=\"_blank\" href=\"$dmn_link\">$dmn_link</a>\n";


    echo "<pre>";

    print_r($_COOKIE);

    print_r($_SESSION);
}

在主页HomeAction中的同一域中打印会话

Array
(
    [PHPSESSID] => pgguqil2e87h0hn3risov330m3
    [_ga] => GA1.2.1895217383.1504264473
    [_gid] => GA1.2.1845702714.1504264473
    [__zlcmid] => iIgiLC7vUKt6CC
)
Array
(
    [__ZF] => Array
        (
            [_REQUEST_ACCESS_TIME] => 1504271027.5384
        )

    [userinfo] => Zend\Stdlib\ArrayObject Object
        (
            [storage:protected] => Array
                (
                    [email] => admin@admin.in
                    [password] => e10adc3949ba59abbe56e057f20f883eaa
                )

            [flag:protected] => 2
            [iteratorClass:protected] => ArrayIterator
            [protectedProperties:protected] => Array
                (
                    [0] => storage
                    [1] => flag
                    [2] => iteratorClass
                    [3] => protectedProperties
                )

        )

)

当我尝试在子域上使用数据时,我无法获取会话数据

1 个答案:

答案 0 :(得分:1)

您应该考虑在其他域名的网址中使用Cookie或include参数。

为你的参考。见Cross-Domain Cookies

Cross domain PHP Sessions

这可能会有所帮助:Secure and Flexible Cross-Domain Sessions