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.

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

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法