Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make visitor counter using ajax and php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.01k
    Comment on it

    Hello Reader's, If you want to get the records of visitors that visits your webpage then in this blog you can learn and build your own. Here we will develop it by using PHP and Ajax.


    Page counters are easy to get and install but if you want to track individual pages you need to develop your own. So let's get started doing this.
     

    Step 1. Open the page which you want to set the visitor counter and at the bottom of the page create an ajax function that will send a request to increment the count every time page open. And it's code will go like this:-

    <script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
    var placeID = '<?=$ViewPlace['Blog']['id']?>';
    
    $( document ).ready(function() {
          $.ajax({
          type: "POST",
          url: webUrl+'Place/ViewCountPlus',
          data: ({id: placeID}), 
          success: function(data) {            
        	  }  
          });
    });
    </script>

    In the code above first, you have to include the jquery file and next get the plage id. Now you have to send this id to another function name as ViewCountPlus inside Place controller. So every time when page load this function will send page id to this function for increment the count. 
     

    Step2. Create a controller with name Page.php and write a new function in it. And it will go like this:-

    public function ViewCountPlus()
    	{
    		$this->db->set('view_count', 'view_count+1', FALSE);
    		$this->db->where('place_id', $_POST['id']);
    		$this->db->update('place_sec_details');
    	}	

    In the above code the page id is get and table "place_sec_details"  with column name "view_count" is incremented by 1. You have to create a new column with int type that will keep the traffic records corresponding to a particular place.  Now you have got the visitor count and you can show this number in your same page and it's code will go like this:-

    <div class="col-md-12">
    		<div class=" text-center">
    			<ul>Views<br>
    			<?php echo $ViewPlace['Place']['view_count']; ?>
    			</ul>
    		</div>
    </div>

     Now when you run this code first time the counter will show count of zero and will start increasing when you open this page again and again. This code runs well and works good for me in my all pages.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: