Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在从window.onbeforeunload()上载文件_Javascript_Jquery_Session - Fatal编程技术网

Javascript 正在从window.onbeforeunload()上载文件

Javascript 正在从window.onbeforeunload()上载文件,javascript,jquery,session,Javascript,Jquery,Session,在我的应用程序中,我需要确保如果用户离开应用程序(通过关闭选项卡、关闭浏览器或导航到另一个页面),如果他们试图返回应用程序(使用“后退”按钮、历史记录或键入链接),他们将被引导到一个屏幕,使他们重新登录 我想我已经解决了这些问题: 在每一页上,检查我的会话是否可用 如果是的话,继续前进。如果没有,请重定向到“您必须再次登录。”页面 在window.onbeforeunload()事件上,运行destroy.php脚本 销毁脚本将终止会话 但是我无法从onbeforeunload()事件加载销毁脚

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

我想我已经解决了这些问题:

  • 在每一页上,检查我的会话是否可用
  • 如果是的话,继续前进。如果没有,请重定向到“您必须再次登录。”页面
  • 在window.onbeforeunload()事件上,运行destroy.php脚本
  • 销毁脚本将终止会话
  • 但是我无法从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>
    
    以下是我尝试加载的销毁代码:

    
    //将消息放在控制台中,即使窗口关闭,我们也可以看到该消息。
    log(“完成!会话已销毁”)
    
    您需要在卸载前小心使用
    on
    。浏览器不允许您在此事件中执行太多操作

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


    可能是重复的,非常感谢您考虑这一点。但这仍然不起作用。当我离开并按下“后退”按钮时,会话仍然存在,并且我从未收到“已完成会话已销毁”消息。所以它仍然没有开火。
        <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> 
    
    /***********************
    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>
    
    $.ajax({
        type: "GET",
        url: "destroy.php",
        async: false
     });