title: long polling events
body: <p>I implement long polling on my web site using php. All work fine, but have one problem. When i  open several pages by one user, i have problem with recognize, what event i should to run on that page. If i block event after it work on one page, that event don't work on another. If i don't block, they continue working infinite on every page. If i check event on page that is worked, they still work again after refresh page. I missed right solution, can you help me?</p>
  
  <p>code not very good but working</p>
  
  <p>i just need an idea how to do this</p>
  
  <pre><code>function loadEventAll($uid=0){
      if (isset($_POST['event_log'])){
          $event_log = json_decode($_POST['event_log']); //here event already worked on page
      }
      else $event_log = array();
      for($i=0;$i&lt;25;$i++){            
          $now = time();
          $time = $now;//-$this-&gt;triggered_timeout;   
  
          //sql to cut already worked event
          $noid = array();
          foreach($event_log as $eve){
              if(isset($eve-&gt;id)) $noid[] = 'id&lt;&gt;'.$eve-&gt;id;
          }
          if (count($noid)) $noid = ' AND ('.implode(' AND ', $noid).') ';
          else $noid = '';   
  
          //don't try to understand this :)
          $q="SELECT * FROM ig_event WHERE (user_id='$uid' OR user_id=0) AND (((triggered+2&gt;=$time OR triggered=0) AND infinite=0) OR (infinite=1 AND triggered+10&lt;=$time)) AND ($now&lt;time_end or infinite=1) AND time_start&lt;=$time $noid";            
          $link = mysql_query($q); 
          $arr = array();
          if(!$link){ 
              sleep(1);
              continue; // if no event occurring start next iteration
          }
          if (mysql_num_rows($link)){   
              while ($ddd = mysql_fetch_assoc($link)){
                  $id = $ddd['id'];
                  if ($ddd['direct_call']!=""){ //call js function on page
                      $arr[] = $ddd['direct_call'].':'.$id.':'.$ddd['param'];
  
                      if ($ddd['success']!=1){
                          if (!$ddd['infinite']) $succ = ' success=1';else $succ='';
                          $ctime = time();
                          $q = "UPDATE ig_event SET $succ,triggered='$ctime' WHERE id='$id'";
                          mysql_query($q);
                      }
                  }
                  else{ // usually not used
                      if ($this-&gt;load-&gt;module($ddd['module'])){
                          if (method_exists($this-&gt;$ddd['module'], $ddd['action'])){
  
                              $time = time();                                
                              if (($tarr = $this-&gt;$ddd['module']-&gt;$ddd['action']($ddd['param'])) and !$ddd['infinite']){                                    
                                  $q = "UPDATE ig_event SET success=1 WHERE id='$id'";
                                  mysql_query($q);
                              }
                          }
                          else{
                              $q = "UPDATE ig_event SET success=1 WHERE id='$id'";
                              mysql_query($q);
                          }
                          $q = "UPDATE ig_event SET triggered='$time' WHERE id='$id'";
                          mysql_query($q);
                          if (!$ddd['infinite']) $arr[] = 'blank'.':'.$id.':';
                      }    
                  }
              }    
              return $arr;
          }  
          sleep(1);
      }  
      return $arr;
  }
  </code></pre>