简体   繁体   English

如何在我的php语句中正确执行此javascript?

[英]How can I properly execute this javascript within my php statement?

The following code will only be called when a session has timed out. 仅当会话超时时才会调用以下代码。 I basically want to display a confirm dialog with javascript to make the user pick if he wants to remain online or not. 我基本上想要显示一个带有javascript的确认对话框,以便用户选择是否要保持在线状态。 My problem is that the php that closes the session inside the javascript seems to be executed even if the confirm is true. 我的问题是,即使确认为真,关闭javascript内部会话的php似乎也会被执行。

if (isset($_SESSION['username']) && isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 5)) {
    ?>
    <script type="text/javascript">
    if (confirm("You were logged off due to inactivity, if you would like to remain logged in click OK.")) {
        header("Location:/");
    } else {
    <?php
    connect();
    if(!connect()) {
        die('Could not connect: ' . mysql_error());
        header( "refresh:3; url=/index.php" );
    }
    mysql_select_db('www');
    $user = $_SESSION['username'];
    mysql_query("UPDATE users SET online='0'
    WHERE username='$user'");
    session_unset();
    session_destroy();
    ?>
    }
    </script>
<?php
}
$_SESSION['last_activity'] = time(); // update last activity time stamp
?>

The PHP code is run before the page is loaded by the browser. PHP代码在浏览器加载页面之前运行。 JavaScript is run after the page is loaded by the browser. 浏览器加载页面运行JavaScript。 You cannot just mix them like this and expect everything to somehow happily work. 你不能只是像这样混合它们,并期望一切都以某种方式愉快地工作。 See HTTP , AJAX and WebSockets . 请参阅HTTPAJAXWebSockets

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

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