XMLHttpRequest 和唯一会话 ID PHP

发布于 2024-12-05 09:11:51 字数 1202 浏览 1 评论 0原文

我创建了一个javascript来通过PHP中的会话跟踪用户跨域,并用它知道用户是否在线,在javascript中我使用XMLHttpRequest并通过POST方法发送,通过函数setInterval来重复此跟踪,它有效对我来说很好,但是对于 XMLHttpRequest 中的每次调用,PHP 会话都会生成其他会话 ID,我只想要在线用户的唯一会话 ID。

PHP: header('Access-Control-Allow-Origin: *'); //允许跨域请求

Javascript:

var __url__rastreamento = "http://external-domain.com/rastreamento/"
var __rastreamento_tempo = 10000;
var __pagina__titulo = document.title;
var __url__requisicao = document.location.href.replace(window.location.protocol + "//" + window.location.host,"");
var __url__host = window.location.host; 

var __x__h__r;
if (window.XMLHttpRequest)
{//IE7+, Firefox, Chrome, Opera, Safari
    __x__h__r = new XMLHttpRequest();
}
else
{//IE6, IE5
    __x__h__r = new ActiveXObject("Microsoft.XMLHTTP");
}

function recarregaRastreamento(){
    __x__h__r.open("POST",__url__rastreamento ,true);
    __x__h__r.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    __x__h__r.send("titulo=" + escape(__pagina__titulo) + "&host=" +escape(__url__host) + "&requisicao=" + escape(__url__requisicao));
}

setInterval("recarregaRastreamento()", __rastreamento_tempo);

提前致谢

i created a javascript to track users cross-domain over the Session in PHP and with it know if the user is online, in the javascript i'm using XMLHttpRequest and send by POST Method, over a function setInterval to repeat this track, it works fine to me, but for each call in XMLHttpRequest the PHP Session generate other Session ID, i want just Unique Session ID by user online.

PHP: header('Access-Control-Allow-Origin: *'); //allow cross-domain request

Javascript:

var __url__rastreamento = "http://external-domain.com/rastreamento/"
var __rastreamento_tempo = 10000;
var __pagina__titulo = document.title;
var __url__requisicao = document.location.href.replace(window.location.protocol + "//" + window.location.host,"");
var __url__host = window.location.host; 

var __x__h__r;
if (window.XMLHttpRequest)
{//IE7+, Firefox, Chrome, Opera, Safari
    __x__h__r = new XMLHttpRequest();
}
else
{//IE6, IE5
    __x__h__r = new ActiveXObject("Microsoft.XMLHTTP");
}

function recarregaRastreamento(){
    __x__h__r.open("POST",__url__rastreamento ,true);
    __x__h__r.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    __x__h__r.send("titulo=" + escape(__pagina__titulo) + "&host=" +escape(__url__host) + "&requisicao=" + escape(__url__requisicao));
}

setInterval("recarregaRastreamento()", __rastreamento_tempo);

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-12-12 09:11:51

XMLHttpRequest 不允许跨源请求,直到XMLHttpRequest 级别 2。如果支持后者,则需要设置 withCredentials 属性 允许为跨域请求发送凭据。

Cross-origin requests are not allowed with XMLHttpRequests until XMLHttpRequest Level 2. And if the latter is supported, you need to set the withCredentials attribute to allow credentials being sent for cross-origin requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文