【问题标题】:Check if variable is set, and then execute the function accordingly检查是否设置了变量,然后相应地执行函数
【发布时间】:2014-01-27 00:09:57
【问题描述】:

我正在制作一个观看计数系统。但问题是,每当用户重新加载页面时,视图就会增加。
为了阻止它,我使用了会话变量。
代码如下:

    if ($_SESSION['var'] == NULL){
    $start = "UPDATE table SET views = views+1 WHERE value = $value";
        $_SESSION['var'] = true;
}  

如果页面被刷新,会话变量保持不变并且条件失败并且没有任何反应。

问题:
但是,如果页面关闭并重新打开,浏览量不会增加,而应该增加。
我究竟做错了什么?

我已经编写了session_start() 和一个 PDO 查询来执行该函数。

【问题讨论】:

  • 当有人登录时需要增加计数,注销或关闭浏览器后需要销毁会话。
  • 您需要更精确地了解计数器的过程(为什么需要关闭和重新打开等,不同的使用情况),并重新增加计数器。您可以使用时间戳,或者实际上它们是许多解决方案,仅取决于您的需要。
  • 向我们展示您的完整代码,包括您在哪里定义 $value。如果我不知道hole 在哪里,那么回答是没有意义的。 前奏!!!

标签: php mysql session session-variables


【解决方案1】:

为了实现您的目标,您需要在浏览器选项卡/窗口关闭时销毁会话。因此,您需要一个 Ajax 请求,它会在关闭窗口时破坏会话参数。

引自本文:http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

在您的 Web 根目录中,创建 js/check_browser_close.js。

/**
 * This javascript file checks for the brower/browser tab action.
 * It is based on the file menstioned by Daniel Melo.
 * Reference: http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed
 */
var validNavigation = false;

function wireUpEvents() {
  /**
   * For a list of events that triggers onbeforeunload on IE
   * check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
   *
   * onbeforeunload for IE and chrome
   * check http://stackoverflow.com/questions/1802930/setting-onbeforeunload-on-body-element-in-chrome-and-ie-using-jquery
   */
  var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
  var leave_message = 'You sure you want to leave?'
  function goodbye(e) {
    if (!validNavigation) {
      if (dont_confirm_leave!==1) {
        if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = leave_message;
        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
        //return works for Chrome and Safari
        return leave_message;
      }
    }
  }
  window.onbeforeunload=goodbye;

  // Attach the event keypress to exclude the F5 refresh
  $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });

}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
  wireUpEvents();
});

还在您的 Web 根目录中创建以下 .html 以测试上述 Javascript 文件。

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="js/check_browser_close.js"></script>
  </head>
  <body>
    <h1>Eureka!</h1>
      <a href="http://www.google.com">Google</a>
      <a href="http://www.yahoo.com">Yahoo</a>
      <a href="http://ykyuen.wordpress.com">Eureka!</a>
  </body>
</html>

现在您需要修改代码以调用服务器端文件以使用 Ajax 销毁会话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2018-01-27
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    相关资源
    最近更新 更多