AJAX 调用成功后注销用户
logout user after successful AJAX call
我有一个如下所示的 ajax 代码,其中我想在特定时间段不活动后注销页面。下面的代码在文件 mno.php 中(javascript 和 php)。我的 login/logout 代码在文件 mno.php.
中
我认为我需要在 A 行 处进行更改。我尝试使用 {'logout'}
而不是 {action:'logout'}
,但它仍然无法正常工作。我的登录和注销代码在 mno.php.
内
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout") {
unset($_SESSION['admin']);
header('location: /abc/mno.php.php');
exit();
}
?>
<script>
jQuery(document).ready(function ($) {
let lastActivity = <?php echo time(); ?>;
let now = <?php echo time(); ?>;
let logoutAfter = 10;
let timer = setInterval(function () {
now++;
let delta = now - lastActivity;
console.log(delta);
if (delta > logoutAfter) {
clearInterval(timer);
//DO AJAX REQUEST TO close.php
$.ajax({
url: "/abc/mno.php",
type: 'GET',
data: {action:'logout'}, // Line A
success: function(data){
console.log(data); // Line B
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
}
}, 1000);
});
</script>
问题陈述:
我想知道我需要在 A 行或 php 中进行哪些更改,以便在 10 秒不活动或我们设置的任何时间后注销页面。
编辑 1: 我将 console.log(数据) 放在 B 行.在控制台上,我收到登录页面的 HTML,但该页面仍未注销。我的登录页面代码在mno.php里面。在执行 console.log(data) 时,我在控制台上得到以下信息:
<form action="/abc/mno.php" method="post">
<div style='width:400px;'>
<input type="hidden" id="user_login" name="user_login" value="1">
<fieldset>
<legend>Login</legend>
<div>
<label for="user_name">User Name</label>
<input type="text" name="user_name">
</div>
<div>
<label for="user_pass">Password</label>
<input type="password" name="user_pass">
</div>
<div>
<button type="submit">Login</button>
</div>
</fieldset>
</div>
</form>
编辑 2: 我已将 /mno.php
替换为 /abc/mno.php
我尝试了确切的代码,在给定的时间后它起作用了。 ajax url 是文件 url 本身(包括 php 和 ajax 调用)。
test.php :
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout") {
echo 'logged out';
}
?>
<script src="js/jquery-min.js"></script>
<script>
jQuery(document).ready(function ($) {
let lastActivity = <?php echo time(); ?>;
let now = <?php echo time(); ?>;
let logoutAfter = 10;
let timer = setInterval(function () {
now++;
let delta = now - lastActivity;
console.log(delta);
if (delta > logoutAfter) {
clearInterval(timer);
//DO AJAX REQUEST TO close.php
$.ajax({
url: "test.php",
type: 'GET',
data: {action:'logout'}, // Line A
success: function(data){
console.log(data); // Line B
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
console.log(error);
console.log(status);
console.log(xhr);
}
});
}
}, 1000);
});
</script>
我从 php 获得 'logged out' 以及文件的其余内容。
所以你应该能够执行你的注销功能。
也许检查你的 url 是否正确。
由于您在处理后使用了 ajax,响应必须 return 返回到 ajax,现在您的代码正在发回 html 数据,即 mno.php
因为你不能从你的 php 代码重定向。而是像下面那样做:
您的 ajax 代码:
$.ajax({
url: "test.php",
type: 'GET',
data: {
action: 'logout'
}, // Line A
success: function(data) {
//checking if data coming from php is "success"
if (data === "success") {
//to redirect
window.location.href = "login_page_to_redirect";
}else{
alert("error");
}
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
console.log(error);
console.log(status);
console.log(xhr);
}
});
您的 php 代码:
if(isset($_GET['action']) && $_GET['action'] == "logout") {
unset($_SESSION['pageadmin']);
//this will send back to ajax
echo "success";
}else{
echo "error";
}
我有一个如下所示的 ajax 代码,其中我想在特定时间段不活动后注销页面。下面的代码在文件 mno.php 中(javascript 和 php)。我的 login/logout 代码在文件 mno.php.
中我认为我需要在 A 行 处进行更改。我尝试使用 {'logout'}
而不是 {action:'logout'}
,但它仍然无法正常工作。我的登录和注销代码在 mno.php.
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout") {
unset($_SESSION['admin']);
header('location: /abc/mno.php.php');
exit();
}
?>
<script>
jQuery(document).ready(function ($) {
let lastActivity = <?php echo time(); ?>;
let now = <?php echo time(); ?>;
let logoutAfter = 10;
let timer = setInterval(function () {
now++;
let delta = now - lastActivity;
console.log(delta);
if (delta > logoutAfter) {
clearInterval(timer);
//DO AJAX REQUEST TO close.php
$.ajax({
url: "/abc/mno.php",
type: 'GET',
data: {action:'logout'}, // Line A
success: function(data){
console.log(data); // Line B
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
}
}, 1000);
});
</script>
问题陈述:
我想知道我需要在 A 行或 php 中进行哪些更改,以便在 10 秒不活动或我们设置的任何时间后注销页面。
编辑 1: 我将 console.log(数据) 放在 B 行.在控制台上,我收到登录页面的 HTML,但该页面仍未注销。我的登录页面代码在mno.php里面。在执行 console.log(data) 时,我在控制台上得到以下信息:
<form action="/abc/mno.php" method="post">
<div style='width:400px;'>
<input type="hidden" id="user_login" name="user_login" value="1">
<fieldset>
<legend>Login</legend>
<div>
<label for="user_name">User Name</label>
<input type="text" name="user_name">
</div>
<div>
<label for="user_pass">Password</label>
<input type="password" name="user_pass">
</div>
<div>
<button type="submit">Login</button>
</div>
</fieldset>
</div>
</form>
编辑 2: 我已将 /mno.php
替换为 /abc/mno.php
我尝试了确切的代码,在给定的时间后它起作用了。 ajax url 是文件 url 本身(包括 php 和 ajax 调用)。
test.php :
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout") {
echo 'logged out';
}
?>
<script src="js/jquery-min.js"></script>
<script>
jQuery(document).ready(function ($) {
let lastActivity = <?php echo time(); ?>;
let now = <?php echo time(); ?>;
let logoutAfter = 10;
let timer = setInterval(function () {
now++;
let delta = now - lastActivity;
console.log(delta);
if (delta > logoutAfter) {
clearInterval(timer);
//DO AJAX REQUEST TO close.php
$.ajax({
url: "test.php",
type: 'GET',
data: {action:'logout'}, // Line A
success: function(data){
console.log(data); // Line B
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
console.log(error);
console.log(status);
console.log(xhr);
}
});
}
}, 1000);
});
</script>
我从 php 获得 'logged out' 以及文件的其余内容。 所以你应该能够执行你的注销功能。 也许检查你的 url 是否正确。
由于您在处理后使用了 ajax,响应必须 return 返回到 ajax,现在您的代码正在发回 html 数据,即 mno.php
因为你不能从你的 php 代码重定向。而是像下面那样做:
您的 ajax 代码:
$.ajax({
url: "test.php",
type: 'GET',
data: {
action: 'logout'
}, // Line A
success: function(data) {
//checking if data coming from php is "success"
if (data === "success") {
//to redirect
window.location.href = "login_page_to_redirect";
}else{
alert("error");
}
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
console.log(error);
console.log(status);
console.log(xhr);
}
});
您的 php 代码:
if(isset($_GET['action']) && $_GET['action'] == "logout") {
unset($_SESSION['pageadmin']);
//this will send back to ajax
echo "success";
}else{
echo "error";
}