2012-02-04 11 views
0

ページを読み込むときにセッションを確認する必要があります。ページがロードされるのはセッションがあるときだけです。IEでチェックセッションが機能していない

jsにコードを追加し、そのページのbody onloadから呼び出されましたが、これはFFではうまく動作しますが、IEではうまく動作しません。

のJs

JSP

<body onload="redirectLogin();"> 

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'; 
    } 
} 

の私のコードすべてのソリューション?

+0

十分DESCRIPではありません "それは働いていません"あなたが直面している問題の何が起こるのですか*? –

+0

Internet Explorerでエラーが報告されましたか?また、 'data.trim()'変数を 'alert()'した場合、どうなっていますか? –

+0

あなたは 'if(!data.trim()!= 'true'){redirectLogin();を実行してください。 } ''と 'checkSession'をページの読み込みで実行しますか? –

答えて

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

?> 

EDIT

私は作業テストケースを作成しました。

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

?> 
+0

お返事ありがとう –

関連する問題