Use Visitor IP as Owner Guid of annotation

I've tried to use visitor IP as owner guid of a annotation.

I need to do this, because i want to make unique visitor counter and i need the visitor IP

maybe like this,

create_annotation     (           $entity_guid,
              $name,
              $value,
              $value_type = '',
              $owner_guid = VISITOR IP for example : 127001,
              $access_id = ACCESS_PRIVATE
    )        

 

I've been succesful to do this and display no error,

but i want to ask , will it be dangerous ? , like my database became exploded , or fatal error in the future ?

 

  • Don't do it! Instead use something like this:

    // To make querying easier using 'group_by'
    $id = create_annotation($guid, 'page_view', $ip_address, '', 0, ACCESS_PUBLIC);
    
    // Keep a log of visitor information
    $page_view_info = [
      'url' => $url,
      'other' => 'stuff',
      'ip_address' => $ip_address,
      'annotation_id' => $id, // you can use this later to map it if needed
    ];
    create_annotation($guid, 'page_view_info', serialize($page_view_info), '', 0, ACCESS_PUBLIC);
    

    But all of this is quite inefficient. Why not use Piwik/Google Analytics for that?

  • As Piwik/Google Analytics was mentioned: https://elgg.org/plugins/384520.

    Though I personnally could understand that someone doesn't want some companies to be able to follow every step my members do.

  • I will make unique visitor counter for every user,


    maybe like ,user A has total 23 unique visitor, user B has ttotal 29 unique visitor, so I think use annotations is best way for my goal , coz I can count visitor for each user



    $options = array(
    'annotation_names' => array('unique visitor'),
    'owner_guid' => THE USERGUID,
    'count' => true
    );

  • I'll second the suggest to not store non-GUIDs as GUID values. Asks for big trouble.

  • @ismayl , why do  you use two  create_annotations?

    Okey this is my code , i hope someone can improve it , or simplify it

    $entity = get_entity($entity_guid);

        if ($entity) {

    //get Visitor IP
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }

    //Convert IP to integer
            $current_ip =  filter_var($ip, FILTER_SANITIZE_NUMBER_INT);
           $current_ip=substr($current_ip, -9);
            $current_ip=(int)$current_ip;
            
          //get the annotations 

            $options = array(
                'types' => array($entity->type),
                'guids' => array($entity_guid),
                'annotation_names' => array('views_counter'),
                'annotation_owner_guids' => array($current_ip)
            );

            if ($entity->getSubtype()) {
                $options['subtypes'] = array($entity->getSubtype());
            }

            $views_counter = elgg_get_annotations($options);
            $countviews = array();

    $countviews[$current_ip]=1;

    //in this case owner_guid = Visitor IP
            foreach($views_counter as $view){
                 $countviews[$view->owner_guid]=1;
            }
    $countviews = count($countviews);
        

    //if there are new IP create create_annotation
            if ($countviews>count($views_counter)) {
               create_annotation($entity->guid, 'views_counter', 1, 'integer', $current_ip, ACCESS_PUBLIC);
                
            }
            
        }

  • OK, i change my code now

    became

    return create_annotation($entity->guid, 'views_counter', $current_ip, 'integer', $user_guid, ACCESS_PUBLIC);
           

    And now my question: is it dangerous if i use $current_ip as value of annotation ?

     

  • Using IP as annotation value is fine. But I really don't follow this logic. If someone is on a shared IP, you will be counting views as unique every time user visits an entity page.
    If you use annotations, you can already determine unique visits by annotation owner and timestamp. Instead of storing the IP which tells you little, store a serialized array of useful information, or just set the value to 1 if you want to keep it simple.
    Here is how I log ad impressions and clicks https://github.com/hypeJunction/Elgg-ads/blob/master/classes/hypeJunction/Ads/Ad.php

  • Annotation->owner_guid just work if the visitor is logged in user ,right ? , what if someone visit my page from Google search?, so I think I have to save the visitor IP, but yes, I must solve the shared IP problem,

  • ooo , i want to ask again. is it safe to create annotations without owner_guid ?

    In my code, if someone (not logged in user) visit my site, his/her IP will be stored as annotations value , but the annotations has no owner guid, (because not logged in user has no user guid) , is it dangerous ?