dtmsaqtly798322992 2013-07-03 11:21
浏览 49

一个脚本,用于检查会话是否处于活动状态,防止所有其他脚本运行

I'm running the following javascript that check with serverside (keep.php) if user has a session. The script is running onload of every page: handler.js

var server = location.host;
var url = "http://" + server + "/MyHome2/php/";
var page = "keep.php";


document.onload = checkAuth();


function checkAuth()
{
var xmlhttp;

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

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var response = xmlhttp.responseText;
        if (response != "")
        {
            document.getElementById("linkSign").innerHTML = response + "|" ;

        }
        else
            document.getElementById("linkSign").innerHTML =  "Sign-in|" ;

    }
}

xmlhttp.open("POST",url + page,true);
xmlhttp.send();}

My server-side keep.php which echo back session is:

<?php


     session_start();

    if (isset($_SESSION['name']))
    {
        echo $_SESSION['name'];
    }

?>

This script is running in the begging of each html page to check if user has a session and if it does it grrets him. The problem that this script is messing with my other scripts! that used to work and are working without this handler.js

For example my login script which log-in a user is not working when user try to submit his credentials, noting happens!

var server = location.host;
var url = "http://" + server + "/MyHome2/php/";
var urlmain = "http://" + server + "/MyHome2/index.html";
var page = "authentication.php";


function loginUser(){
var str = "usr=" + encodeURIComponent(document.getElementById("user").value) + "&pwd=" + encodeURIComponent(document.getElementById("password").value);
checkUserExistance(str,str.length);}



function checkUserExistance(data,size){

var xmlhttp;

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

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var response = xmlhttp.responseText;
        responseHandler(response);
    }
}

xmlhttp.open("POST",url + page,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length",size);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(data);}

Why is the simple session check onload of each page is preventing mt other script from executing?!

  • 写回答

1条回答 默认 最新

  • dongxiao0449 2013-07-03 11:32
    关注

    Okay I've found my mistake :) Apparently the scripts are considered as a same scope although they are on different files.

    I have global variables with the same name and this is no no if I reference those scripts from the same html page.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线