Martin
Martin

Reputation: 6015

How can I use Internet Explorer's Cookies in my ActiveX control?

How does the MSXML2.XMLHTTP.3.0 object connect to the same session? I would like my own ActiveX control to exhibit this behavior.

index.php: (the server-side language is not relevant):

<?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(); ?>

The result:

Your session ID: d2ljvbjllsdlc51rsq5naiffc2

Your session ID from AJAX: d2ljvbjllsdlc51rsq5naiffc2

Upvotes: 2

Views: 1314

Answers (1)

Martin
Martin

Reputation: 6015

Perhaps the XMLHTTP object is a singleton who shares cookies. I implemented my own ActiveX which simply creates an XMLHTTP object, and it just works when I replace objXHR in the sample (pseudocode below):

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;

Upvotes: 1

Related Questions