duanchi5078 2013-12-29 08:52
浏览 20
已采纳

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

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. My problem is that the php that closes the session inside the javascript seems to be executed even if the confirm is true.

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
?>
  • 写回答

1条回答 默认 最新

  • duanmao1872 2013-12-29 08:54
    关注

    The PHP code is run before the page is loaded by the browser. JavaScript is run after the page is loaded by the browser. You cannot just mix them like this and expect everything to somehow happily work. See HTTP, AJAX and WebSockets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题