0

I am making a view count system. But the problem is that whenever the person reloads the page, the view is incremented.
In order to stop it, I used a session variable.
The code is as follows:

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

If the page is refreshed, the session variable remains intact and the condition fails and nothing happens.

PROBLEM:
But if the page is closed and re-opened, the views don't increase whereas they should.
What am I doing wrong?

I have written session_start() and a PDO query to execute the function.

  • you need to increment count when anyone login and after logout or closing browser you need to destroy session. – Awlad Liton Jan 26 '14 at 15:58
  • You need to be more precise about the process of your counter( why you need close and re open etc., the different cases of use), and re increment the counter. You can play with a timestamp, or they are many solutions in fact,simply depending of you needs. – Mike Castro Demaria Jan 26 '14 at 16:06
  • Show us your full code including where you're defining `$value`. No sense putting in an answer, if I don't know where the `hole` is. **FORE!!!** – Funk Forty Niner Jan 26 '14 at 17:32

1 Answers1

0

In order to achieve what you are looking for, you need to destroy session on browser tab/window close. Therefore you need a Ajax request that destroys the session parameter on window close.

Quoting from this article: http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

In your web root, create the 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();
});

Also create the following .html in your web root to test the above Javascript file.

<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>

Now you need to modify the code to call you server side file to destroy the session using Ajax.

gurudeb
  • 1,856
  • 22
  • 29