我正在使用PHP和AJAX检查会话。我的php函数检查会话是否已设置,如果会话已设置,它将回显用户详细信息并将其传递给jquery ajax,如果会话未设置则会返回0到ajax然后它将重定向到我的主页,但它加载太多次像不可阻挡我不知道发生了什么我甚至没有任何循环。这是我的代码。
TIA。
JQUERY CODE:
$(document).ready(function(){
//iframe id
//iframe onload function
$("#navContent").load(function(){
//check the current session with ajax
$.ajax({
type: 'post',
url: 'http://localhost/impex/lib/uacctload.php',
success:function(data){
if (data == "0") {
alert('You are not authorized to view this page.');
console.log("|chkses|response| : no current session : Response : " +data);
//iframe redirection to homepage
$("#navContent").attr('src', "http://localhost/impex/home.html");
return false;
}
else {
console.log("Current Session Active : Response : OK" );
}
},
error:function(xhr, status, error){
var err = xhr.responseText;
alert("Cannot get current session. Contact IT for Support");
console.log(err);
}
});//ajax clossing
});//iframe onload closing
});//document ready closing
PHP代码:
<?php
require('getDB.php');
session_start();
if(isset($_SESSION['logged_username']) && isset($_SESSION['logged_userid'])){
$sesUname = $_SESSION['logged_username'];
$sesUID = $_SESSION['logged_userid'];
$queSession = "SELECT * FROM tbl_useraccount
WHERE acct_uname = '$sesUname' AND acct_id = '$sesUID'";
$result = mysqli_query($conDB, $queSession);
while ($userjson = mysqli_fetch_assoc($result)) {
$userData[] = $userjson;
}
header('Content-type: application/json');
echo json_encode($userData);
}
else
{
echo "0";
}
?>