내가 다르게 제안 할 것입니다.
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);
?>
충분한 descrip 아니다 "제대로 동작하지 않습니다" 당신이 직면하고있는 문제의 무슨 일이야*? –
Internet Explorer에 오류가보고 되었습니까? 또한,'data.trim()'변수를'alert()'하면, 어떻게 생겼을까요? –
왜'if (! data.trim()! ''true ') {redirectLogin(); }'를 실행하고'checkSession'을 페이지로드시에 실행합니까? –