如何在ActiveX控件中使用Internet Explorer的Cookie?

时间:2011-02-16 04:07:22

标签: internet-explorer activex xmlhttprequest

MSXML2.XMLHTTP.3.0对象如何连接到同一会话?我希望自己的ActiveX控件能够表现出这种行为。

index.php:(服务器端语言不相关):

<?php session_start(); session_write_close(); ?>
<html>
<head>
<title>Disp Test</title>
<script type="text/javascript">
window.onload = function()
{
    var divJS;
    var objXHR;

    divJS = document.getElementById('js');

    objXHR = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    objXHR.open("GET", "ajax.php", false);
    objXHR.send();
    divJS.innerHTML = objXHR.responseText;
}
</script>
</head>
<body>
<p>Your session ID: <?php echo session_id(); ?></p>

<div id="js">
Please enable Javascript.
</div>

</body>
</html>

ajax.php:

<?php session_start(); session_write_close();?>
Your session ID from AJAX: <?php echo session_id(); ?>

结果:

Your session ID: d2ljvbjllsdlc51rsq5naiffc2

Your session ID from AJAX: d2ljvbjllsdlc51rsq5naiffc2

1 个答案:

答案 0 :(得分:1)

XMLHTTP对象可能是共享cookie的单例。我实现了自己的ActiveX,它只是创建了一个XMLHTTP对象,当我在示例中替换objXHR时,只是工作(下面的伪代码):

IXMLHTTPRequest* pX;
hr = CoCreateInstance(CLSID_XMLHTTP, NULL, CLSCTX_INPROC_SERVER, IID_IXMLHTTPRequest, (void**)&pX);
if(SUCCEEDED(hr))
{
    hr = pX->open("GET", "ajax.php", false);
    if(SUCCEEDED(hr))
    {
        hr = pX->send();
        if(SUCCEEDED(hr))
        {
            VariantInit(pVarResult);
            V_VT(pVarResult) = VT_BSTR;
            hr = pX->get_responseText(&V_BSTR(pVarResult));
            if(!SUCCEEDED(hr))
            {
                VariantClear(pVarResult);
            }
        }
    }

    pX->Release();
}

return S_OK;