1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Needed help in adf.ly clone

Discussion in 'PHP' started by Wol, Jul 11, 2013.

  1. #1
    Hello.

    Needed help in adf.ly clone
    • Limit 1 view per ip in 24 hours
    analyzer.class.php
    
    <?php
    /*******************************************************************************
    * Link analytic system
    *
     
    ******************************************************************************/
     
    define('ANALYZER_CLASS_PHP_INCLUDED', true);
    define('GEOIP_LOC', realpath(dirname(__FILE__)) . '/geoip/');
    include_once GEOIP_LOC . 'geoip.php';
    include_once '../global.php';
     
    class Analyzer {
    protected $table = '';
    protected $db = null;
    protected $lid = 0;
    protected $oid = 0;
    protected $aid = 0;
    protected $cip = 0;
    protected $geo = null;
     
    protected $client = array(
    'ipaddr' => '0.0.0.0',
    'country' => '',
    'referrer' => ''
    );
     
    public function __construct($lid, $oid, $aid, $ref = '') {
    global $_GLOBAL;
    $this->table = $_GLOBAL['TABLES']['ANALYZER'];
    $this->db = System::getDB();
    $this->lid = $lid;
    $this->oid = $oid;
    $this->aid = $aid;
    $this->cip = mb_substr($this->ip(), 0, 15);
    $this->geo = geoip_open(GEOIP_LOC . 'GeoIP.dat', GEOIP_STANDARD);
    $this->client['referrer'] = $ref;
    }
     
    public function __destruct() {
    geoip_close($this->geo);
    }
     
    public function _record($adtype = 'none', $view_time = 0) {
    global $_GLOBAL;
    if ($this->_isCrawler() || !$this->db || !$this->isValid()) return;
    $_u = $this->isUnique();
     
    $this->client['ipaddr'] = $this->cip;
    $this->client['country'] = $this->country($this->client['ipaddr']);
     
    $shrinker_id = $this->db->getField($_GLOBAL['TABLES']['LINKS'], 'user', "`id`='{$this->lid}'");
     
    $sip = $this->db->getField($_GLOBAL['TABLES']['USERS'], 'ipaddr', "`id`='{$shrinker_id}'");
    if (!$_u && $this->client['ipaddr'] == $sip) {
    return;
    }
     
    try {
    $pu = parse_url($this->client['referrer']);
    if (CURRENT_HOSTNAME == $pu['host']) $this->client['referrer'] = 'IM, Applications, and Direct';
    } catch(Exception $e) { }
     
    $earned = 0;
    $adtype = strtolower($adtype);
    if ($adtype != 'none') {
    $earned = $this->getEarning(($_u ? 'u_' : 'r_') . $adtype, $this->client['country']);
    if ($adtype == 'top_banner') $earned *= .70;
    else if ($view_time < 6) $earned /= 6 - $view_time;
    }
     
    $rhits = $this->db->getField($this->table, 'hits', "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}'");
    if ($rhits > 3) $earned /= $rhits;
     
    $data = array_merge($this->client, array('lid' => $this->lid, 'oid' => $this->oid, 'aid' => $this->aid,
    'date' => date('Y-m-d'), 'earned' => $earned,
    'hits' => 1));
     
    $_p = false;
    if ($_u) {
    $_p = $this->db->insert($this->table, $data);
    } else {
    $_p = $this->db->update($this->table, array('earned' => 'earned+' . $earned, 'hits' => 'hits+1'),
    "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}' "
    . "AND `date`='" . date('Y-m-d') . "'",
    false);
    }
     
    if ($_p) {
    $data = array('views' => 'views+1', 'earned' => 'earned+' . $earned);
    $this->db->update($_GLOBAL['TABLES']['LINKS'], $data, "`id`='{$this->lid}'", false);
    $this->db->update($_GLOBAL['TABLES']['USERS'], array('available_earning' => 'available_earning+' . $earned),
    "`id`='{$shrinker_id}'", false);
    if ($this->aid) {
    $c = $this->db->getRows($_GLOBAL['TABLES']['CAMPAIGNS'], "`id`='{$this->aid}'", '', '1');
    if ($c['spent_today'] < $c['daily_budget'] && $c['daily_budget'] != 0) {
    $cdat = array('views_left'=>'views_left-1', 'spent_today'=>'spent_today+' . $earned);
    if ($c['views_left'] <= 1) {
    $cdat['status'] = 3;
    $cdat['views_left'] = '0';
    }
    $this->db->update($_GLOBAL['TABLES']['CAMPAIGNS'], $cdat, "`id`='{$this->aid}'", false);
    }
    }
    } else {
    System::log($this->db->error());
    }
    }
     
    public function _recordClick() {
    global $_GLOBAL;
    if (!$this->isValid()) return false;
    $earned = $this->getEarning(($this->isUnique(true) ? 'u' : 'r') . '_top_banner',
    $this->country($this->ip())) * .30;
    $cdat = array('total_clicks' => 'total_clicks+1', 'spent_today' => 'spent_today+' . $earned);
    $adat = array('earned' => 'earned+' . $earned, 'banner_click' => "'1'");
    return $this->db->update($_GLOBAL['TABLES']['CAMPAIGNS'], $cdat, "`id`='{$this->aid}'", false)
    && $this->db->update($this->table, $adat, "`lid`='{$this->lid}' AND `ipaddr`='{$this->cip}'", false)
    && System::getUser()->raiseEarning($earned);
    }
     
    public function isUnique($click = false) {
    $date = date('Y-m-d');
    $where = "`lid`='{$this->lid}' AND `aid`='{$this->aid}' AND `ipaddr`='{$this->cip}'"
    . ($click ? " AND `banner_click`='1'" : '');
    return !$this->db->rowCount($this->table, $where . " AND `date`='{$date}'");
    }
     
    public function isValid() {
    global $_GLOBAL;
    return $this->db->rowCount($_GLOBAL['TABLES']['LINKS'], "`id`='{$this->lid}' AND `user`='{$this->oid}'");
    }
     
    public function getEarning($adtype, $gccn) {
    global $_GLOBAL;
    $adtype = str_replace(' ', '_', strtolower($adtype));
    $pkgs = $this->db->getRows($_GLOBAL['TABLES']['PAYOUTS'], "`active`='1'");
    $prox = isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '';
    $eccn = $prox ? $pkgs[1] : $pkgs[0];
    if (!$prox) {
    for ($i = 0; $i < count($pkgs); $i++) {
    if ($pkgs[$i]['code'] == $gccn) {
    $eccn = $pkgs[$i];
    break;
    }
    }
    }
    return $eccn[$adtype] / 1000;
    }
     
    // ***
     
    private function _toUTF8($str) {
    $e = strtoupper(mb_detect_encoding($str));
    return $e == false || $e == 'UTF-8' || $e == 'ASCII' ? $str : iconv($e, 'UTF-8', $str);
    }
     
    // ***
     
    public function _isCrawler() {
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $crawlers = 'Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|' .
    'AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|' .
    'GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
    return preg_match("/{$crawlers}/", $ua) > 0;
    }
     
    public function _isProxy() {
    $hd = array('HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR',
    'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP',
    'HTTP_FORWARDED_FOR_IP', 'VIA', 'X_FORWARDED_FOR',
    'FORWARDED_FOR', 'X_FORWARDED', 'FORWARDED',
    'CLIENT_IP', 'FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION');
    $detected = false;
    foreach($hd as $i){
    if(isset($_SERVER[$i]) && $_SERVER[$i]) {
    return true;
    }
    }
    return in_array($_SERVER['REMOTE_PORT'], array(8080, 80, 6588, 8000, 3128, 553, 554))
    || @fsockopen($this->cip, 80, $errno, $errstr, 0.1);
    }
     
    public function ip() {
    $ip = $_SERVER['REMOTE_ADDR'];
     
    if (($ip == '127.0.0.1' || $ip == '::1' || $ip == $_SERVER['SERVER_ADDR'])
    && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') {
    $ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $ip = $ips[0];
    }
     
    return $ip;
    }
     
    public function country($ip) {
    return $this->_isProxy() ? 'A1' : geoip_country_code_by_addr($this->geo, $ip);
    }
     
    /*
    function browser() {
    $a = array('Windows', 'Mac', 'Linux', 'FreeBSD', 'DoCoMo', 'iPod', 'iPad', 'iPhone',
    'Android', 'Symbian', 'Nintendo', 'PlayStation');
    $a = $_SERVER['HTTP_USER_AGENT'];
    $b = get_browser($a, true);
    $o = '';
     
    foreach ($a as $b) {
    if (preg_match('/' . $b . '/', $u)) {
    $o = $b;
    break;
    }
    }
     
    return array('os' => $o, 'browser' => '' $b['parent']);
    }*/
    }
    PHP:
     
    Last edited by a moderator: Jul 11, 2013
    Wol, Jul 11, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    So are you having trouble getting started? errors on a particular line? what help do you need?
     
    sarahk, Jul 11, 2013 IP
  3. Wol

    Wol Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hello.

    features included:
    Limit 1 view per ip in 24 hours
    thanks
     
    Wol, Jul 11, 2013 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Yeah, I get that. This is a discussion forum where we discuss the problem you are having. So far we know nothing about how you have already tried to resolve it, what might be already built into the code and the approach you have already tried to take.

    My immediate concern would be that one view per ip would significantly disadvantage people in NZ, Singapore, India or who work for large companies as these people would all share an IP with other people. You might want to use cookies rather than the IP as the identifier. Or IP and user agent.

    We don't know how important it is to you to beat all the people who try to get around your system or if you are happy to have a success rate of, say, 80%
     
    sarahk, Jul 11, 2013 IP
    ryan_uk likes this.
  5. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #5
    Or anyone working for the company I do (over 330,000 employees load balanced across proxy servers, can have a new IP each second - yes, that bad a set up!). I would go with the cookie option, too. There are so many weird network configurations out there.
     
    ryan_uk, Jul 11, 2013 IP
    sarahk likes this.
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #6
    bearing in mind that a cookie set by IE can't be read by FF or Chrome or Opera or Safari etc. If people really want to get past your system they will.
     
    sarahk, Jul 11, 2013 IP