简体   繁体   English

使用Ajax从JavaScript访问安全Cookie和仅HTTP Cookie时出现问题

[英]Problems using ajax to access secure and httponly cookies from javascript

My website had no security on cookies and some cookies were set by javascript. 我的网站在cookie上没有安全性,并且某些cookie是由javascript设置的。 For security reasons, I had to add secure and httponly flags and so to adapt my javascript. 出于安全原因,我必须添加安全和httponly标志,以便适应我的JavaScript。 The only way I found was to use ajax. 我发现的唯一方法是使用Ajax。

I wrote a php script called by javascript thru ajax to set cookies. 我写了一个由javascript通过ajax调用的php脚本来设置cookie。 As I know javascript is monotask and asynchronous tasks are scheduled after the synchronous ones. 据我所知,javascript是monotask,异步任务是在同步任务之后进行调度的。 So I had to modify my code to wait for ajax return before calling next pages. 因此,我不得不修改代码以等待ajax返回,然后再调用下一页。 For example (see javascript below), when action is 'RELOAD', if a form tag is found on page, it's submitted, if not, a simple reload is done. 例如(请参见下面的javascript),当操作为“ RELOAD”时,如果在页面上找到表单标签,则将其提交,否则,将进行简单的重新加载。

javascript : javascript:

function setMultiCookie(prm, action) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            setMultiCookieRet(action);
        }
    };
    xhr.open("POST", "setMultiCookies.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("prm="+encodeURIComponent(prm));
}
function setMultiCookieRet(action) {
    switch(action) {
        case 'RELOAD':
            var form = document.getElementsByTagName('form');
            if (form.length > 0)
                form[0].submit();
            else
                location.reload(true);
        break;
        case 'NONE':
        break;
        default:
            window.open(action,'_self');
    }
}

php script setMultiCookies.php : php脚本setMultiCookies.php:

<?php
header("Content-Type: text/plain");
if ($_POST["prm"] == '')
    exit;
$prm = explode('µ', $_POST["prm"]);
foreach ($prm as $parms) {
    list($name, $value) = explode('§', $parms, 2);
    if ($value == "-unSetCookie-")
        setcookie($name, '', 1, "/", "", TRUE, TRUE);
    else
        setcookie($name, $value, 0, "/", "", TRUE, TRUE);
}
?>

Example of a call to set cookie var1 to val1 and var2 to val2, to unset val3 and to reload page after that : 调用示例,将cookie var1设置为val1并将var2设置为val2,取消设置val3并在此之后重新加载页面:

<span onClick="setMultiCookie('var1§val1µvar2§val2µval3§-unSetCookie-', 'RELOAD');" class="button">

It works most of the time but in some cases, especially with form tags, it doesn't. 它在大多数时间都有效,但是在某些情况下,尤其是对于表单标签,则无效。 I don't know what happens. 我不知道会发生什么 I'm sure php script works. 我确定php脚本有效。 I've made some trace displays and I've tested that php setcookie return is set to true. 我进行了一些跟踪显示,并测试了php setcookie return设置为true。 But cookies are not set. 但是未设置cookie。 I suppose something happens before set is done. 我想在设置完成之前会发生一些事情。 I had that problem earlier when I loaded a page in javascript synchronous part. 我在javascript同步部分加载页面时遇到了这个问题。

I would be glad if you know what happens or if you know some way to investigate. 如果您知道会发生什么或知道某种调查方法,我将很高兴。

OK. 好。 Meanwhile I solved my problem. 同时我解决了我的问题。 My example was not a good one, because it works with a span tag. 我的示例不是一个很好的示例,因为它可以使用span标签。 I don't know why, but it doesn't work with button tags. 我不知道为什么,但不适用于按钮标签。 Anyway I replaced button tags by span tags and every thing's allright. 无论如何,我用span标签替换了按钮标签,然后一切正常。

Thank's to SLaks and Kaddath for having a look on my question. 感谢SLaks和Kaddath对我的问题的解答。 My website is an intranet one and needs an AD login to access (written in PHP too). 我的网站是一个Intranet网站,需要AD登录才能访问(也用PHP编写)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM