【问题标题】:Uploading a file from window.onbeforeunload()从 window.onbeforeunload() 上传文件
【发布时间】:2017-04-01 01:43:03
【问题描述】:

在我的应用程序中,我需要确保如果用户离开应用程序(通过关闭选项卡、关闭浏览器或导航到另一个页面),如果他们尝试返回应用程序(使用返回按钮、历史记录或键入链接),他们将被定向到一个让他们重新登录的屏幕。

我想我已经解决了这一切:

  1. 在每个页面上,检查我的会话是否可用。
  2. 如果是,请继续。如果没有,请重定向到“您必须重新登录”。页面。
  3. 在 window.onbeforeunload() 事件中,运行 destroy.php 脚本。
  4. destroy 脚本会终止会话。

但我无法从 onbeforeunload() 事件中加载我的销毁脚本。

这是开始的索引文件:

<?php

/***********************
INDEX for CloseTab test
************************/

// Start the session
echo "<pre>";
echo "Starting session now...";
session_start();

// Put something in the variable that we can get on another page
$_SESSION["name"] = "Joe";
echo "\nJust set session var 'name' to 'Joe'.";

?>

<!-- Open that other page -->
<a href= <?php echo "home.php"; ?> > Click here to open Home page</a>

这里是主页,只是为了有地方去测试会话:

    <head>

        <!-- ***********************
        HOME for CloseTab test
        ************************** -->

        <!-- Link to jQuery file  -->
        <script   src="https://code.jquery.com/jquery-3.1.1.min.js"   
              integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   
              crossorigin="anonymous"></script>

        <!-- Link to our custom js code.. -->
        <script type="text/javascript" src=<?php echo "my_scripts.js></script> 

    </head>

    <?php

    // Start the session
    session_start();

    // See if there is any indication that a session is not available.
    // Test with closing tab, closing browser, navigating away from page.
    // We want to see this message if the user is coming here from anywhere except the index page.
    // Test back button, typing the URL, browser history.
    if ( (!isset($_SESSION)) || (session_status() == PHP_SESSION_NONE) || (count($_SESSION) < 1) ) {
        echo "<pre>";
        echo "Your session is not longer active.  Log in again.";
        echo "\n\nPretending to go to a login page...";
        exit;
    }

    ?>

    <!-- If the session is good, we MUST have come from index page, and can access the session var. -->
    <div class="container">
        <h1>Home Page</h1>
        <h2>Here is the name from the session var: <?php echo $_SESSION["name"]; ?> </h2>
    </div>

<div>
    <h3><a href= <?php echo "destroy.php"; ?> > Click here to log out</a></h3>
</div>

</html> 

这是我的 javascript 代码:

/***********************
my_scripts.js
************************/

console.log("in js file");

// Before a page unloads, run the destroy code
window.onbeforeunload = function() {
    console.log("inside onbeforeunload");             // I get this to print

    // Try to execute with AJAX call                  // Nothing in the middle seems to be running
    $.ajax({
        type: "GET",
        url: "destroy.php",
        async: false                     
     });

    // Try with jQuery syntax
    $("container").load("destroy.php");

   console.log("Got this far");                       // I get this to print
}

这是我要加载的销毁代码:

<?php

/******************************************************************
Destroy
- Clears the session var and destroys the session if user leaves app.
******************************************************************/

// Unset all of the session variables and destroy the session.
session_start();
$_SESSION = array();
session_destroy();

?>

<script type="text/javascript">
    // Place message in the console that we can see even if the window closes.
    console.log("DONE!  Session destroyed.")
</script>

【问题讨论】:

标签: javascript jquery session


【解决方案1】:

你需要小心onbeforeunload。浏览器不会让你在这个事件中做太多事情。

如果要进行 AJAX 调用,需要添加async: false。通常不鼓励这样做,但在这里,如果调用是异步的,那么浏览器可能会在调用完成之前完成事件并关闭页面。

$.ajax({
    type: "GET",
    url: "destroy.php",
    async: false
 });

【讨论】:

  • 非常感谢您考虑这个问题。但这仍然行不通。当我离开然后点击“返回”按钮时,会话仍然存在,并且我从未收到“DONE Sessiondestroyed”消息。所以它仍然没有触发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-08
  • 2014-04-02
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多