title: wordpress plugin visit counter not working properly
body: <p>I'm trying to make a plugin for wordpress visitor counter and limit counts of visitor. I wrote this code, but counter variable increments 4 times or 5 times every time I refresh the page. Basically plugin is for free visits after free visits user should be logged in to visit website. Please check the code.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
      $count=0;
  
      function ip_get(){
  
          if(!empty($_SERVER['HTTP_CLIENT_IP'])){
              //ip from share internet
              $new_ip = $_SERVER['HTTP_CLIENT_IP'];
          }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
              //ip pass from proxy
              $new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
          }else{
              $new_ip = $_SERVER['REMOTE_ADDR'];
          }
  
          global $wpdb;
          $table_name=$wpdb-&gt;prefix.'wbs_user_ips';
  
          $results = $wpdb-&gt;get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
          if(empty($results)){
              $wpdb-&gt;insert($table_name,
                  array(
                        'user_ip'=&gt;$new_ip,      
                        'user_count'=&gt;'1',      
                        'user_status'=&gt;'0',    
  
                  )
                  );
          }
  
          if(!empty($results))     // Checking if $results have some values or not
          {  
              $count=0;
              foreach($results as $row){
                  $ip= $row-&gt;user_ip;
                  $count=$row-&gt;user_count;
              }
  
              if($ip==$new_ip){
                  echo $count++;
  
                  $wpdb-&gt;query($wpdb-&gt;prepare("UPDATE $table_name 
                              SET user_count=".$count."
                              WHERE user_ip = %s",$ip));
              }   
          } 
      }
  ?&gt;
  </code></pre>