WordPress.org

Ready to get started?Download WordPress

Forums

Track Visitors using Codex & jQuery (1 post)

  1. Archie22is
    Member
    Posted 1 year ago #

    Hi guys,

    This is what I wish to accomplish:

    A visitor visits my site and views a post:
    - visitor ip is obtained and stored
    - a counter is added to a database with unique post id

    But if the visitor views the post again within 24 hours:
    - the counter must not increment
    - obvisouly this will be based on the fact that the visitor ip address has just been logged in the past 24 hours

    Again:
    If the visitor view another post:
    - the same process should occur
    - store ip address and add a counter increment

    At the moment i have the post displayed as links on the front page, so the script i have is run from the links. This will be the case on the site as well... i am no so interested in the posts being viewed, but rather on the post links clicked from the home page.

    The script I have is below (not fully functional - the logic is wrong, please assist):

    function updateCustomerCount() {
    
    	global $wpdb;
    
    	$post_id = $_POST['id']; // customer id
    
    	// update ip address to reduce redundant click count
    	$remote_ip = $_SERVER['REMOTE_ADDR'];
    
    	if ($remote_ip){
    		$results = $wpdb->get_results("SELECT IP_Address FROM wp_logged_ip WHERE IP_Address="
    				. $remote_ip . "AND CustomerId = ". $post_id . "AND Date = " . date("Y-m-d h:i:s") );
    
    		if(!$results){
    			$wpdb->insert('wp_logged_ip', array('IP_Address' => $remote_ip, 'CustomerId' => $post_id, 'Date' => date("Y-m-d h:i:s")));
    
    			// update customer count
    			$wpdb->insert('wp_customercount', array('customerid' => $post_id, 'date' => date("Y-m-d h:i:s")));
    		}
    	}
    
    }

    Shout if you need any clarification.

Topic Closed

This topic has been closed to new replies.

About this Topic