检查会话在IE中不起作用

时间:2012-02-04 08:08:53

标签: javascript jquery internet-explorer

加载页面时,我需要检查会话。只有在有会话时才加载页面。

我在js中添加一个代码并从该页面的body onload调用,这对FF工作正常,但在IE中却没有。

我在Js中的代码

checkSession= function(){
    var sessionVal = false;
    $.ajax( {
        url : 'checksession.html',
        async : false,
        success : function(data) {
            if (data.trim() == 'true') {
                sessionVal = true;
            }
        }

    });
    return sessionVal;
};
function redirectLogin(){
    if (!checkSession()) {
        jAlert("Oops... Your session has expired, please re-login...");
        window.location = cdContextPath + '/login.html';
    }   
}

在jsp中

<body onload="redirectLogin();">

任何解决方案?

1 个答案:

答案 0 :(得分:1)

我建议采用不同的方式。

var checkSession = function(){
    $.ajax({
        url : 'checksession.html',
        dataType : 'json',
        async : false,
        success : function(data) {
            if (data.validSession != true) {
                redirectLogin();
            }
        }
    });
}

function redirectLogin(){
    jAlert("Oops... Your session has expired, please re-login...");
    window.location = cdContextPath + '/login.html';
}

$(document).ready(checkSession);

然后用JSON发回一个响应(注意,这只是一个模型/示例):

<?php

function checkSession() {
    if (yourSessionCheckStuff) {
        return 'true';
    }

    return 'false';
}

echo '{"validSession" : ' + checkSession() + '}';

// Same as the echo above, but uses json_encode()
// You only need one or the other, and I'd used
// the below.

$json = new stdClass();
$json->validSession = checkSession();

echo json_encode($json);

?>

修改

我创建了一个工作测试用例。参见:

http://jfcoder.com/test/index.php

然后点击要切换的链接。这在IE9中没有问题。

这是使其有效的代码。请注意,$_GET和会话检查只是证明重定向的替身。我还将jAlert()替换为常规alert()

<强>的index.php

<?php

session_start();

if ($_GET['emulateloggedin'] == 'true') {
    $_SESSION['loggedin'] = true;
} else {
    $_SESSION['loggedin'] = false;
}

?>
<html>
<head>
<title>Login Redirect Test</title>
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var checkSession = function(){
    $.ajax({
        url : 'checkSession.php',
        dataType : 'json',
        async : false,
        success : function(data) {
            // Unless the returned value is true, redirect.
            if (data.validSession != 'true') {
                redirectLogin();
            }

            // You may also want to initialize any of the page
            // routines here, after the check is finished.

            // Just a suggestion, you could return the amount
            // of time the session has left and save that
            // locally. Then you could add a "You will be
            // logged out unless..." message that prompts
            // the user to continue the session or logout,
            // and automatically redirect when the time
            // runs out.
        }
    });
}

function redirectLogin(){
    alert("Oops... Your session has expired, please re-login...");
    window.location = 'login.html';
}

$(document).ready(checkSession);
</script> 
</head>
<body>
<p>If you see this, you are "logged in". <a href="index.php?emulateloggedin=false">Logout?</a></p>
</body>
</html>

<强>的login.html

<html>
<head>
<title>Login Redirect Test</title>
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
</script> 
</head>
<body>
<p>If you see this, you are "logged out". <a href="index.php?emulateloggedin=true">Login?</a></p>
</body>
</html>

<强> checkSession.php

<?php

session_start();

function checkSession() {
    if ($_SESSION['loggedin'] == true) {
        return 'true';
    }

    return 'false';
}

$json = new stdClass();
$json->validSession = checkSession();

echo json_encode($json);

?>