繁体   English   中英

成功 AJAX 调用后注销用户

[英]logout user after successful AJAX call

我有一个 ajax 代码,如下所示,我想在特定时间段不活动后注销页面。 下面的代码位于文件mno.php (javascript 和 php)中。 我的登录/注销代码在文件mno.php中。

我相信我需要在Line 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(data)放在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

我尝试了确切的代码,并且在给定的时间后它起作用了。 the ajax url is the file url itself (including php and ajax call).

测试.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 以及文件内容的 rest 中得到“注销”。 所以你应该能够执行你的注销 function。 也许检查您的 url 是否正确。

As you have used ajax after processing the response has to be return back to ajax and now your code is sending back the html data which is mno.php because you cannot redirect from your php code.Instead do like below:

您的 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";
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM