Yii 1.1: usercounter

Simple user counter
19 followers

This extension is a simple user counter. It's a port of the pCounter from Andreas Droesch.

The counter supports the following data:

  • users online
  • total user of today
  • total user of yesterday
  • total user overall
  • maximum user at a day
  • date for the maximum

Example

See documentation page

Documentation

Requirements

  • Yii 1.0 or above

Installation

  • Extract the release file under protected/extensions
  • Insert this code in the config/main.php:
// application components
    'components'=>array(
 
        // ...
 
        // UserCounter
        'counter' => array(
            'class' => 'UserCounter',
        ),
    ),

Initialization

// Execute the counter before you use the getter-funtions!
Yii::app()->counter->refresh();

Usage

See the following code example:

online: <?php echo Yii::app()->counter->getOnline(); ?><br />
today: <?php echo Yii::app()->counter->getToday(); ?><br />
yesterday: <?php echo Yii::app()->counter->getYesterday(); ?><br />
total: <?php echo Yii::app()->counter->getTotal(); ?><br />
maximum: <?php echo Yii::app()->counter->getMaximal(); ?><br />
date for maximum: <?php echo date('d.m.Y', Yii::app()->counter->getMaximalTime()); ?>

Result

online: 9
today: 17
yesterday: 28
total: 1203
maximum: 32
date for maximum: 17.10.2009

Change Log

September 10, 2009

  • Initial release.

Total 10 comments

#13664 report it
alif at 2013/06/14 06:02pm
Add max_time in table pcounter_save

if occurs discrepancy on 'Date for maximum' the possibility be caused because date of maximum not already saved in database. try to update the table

if table 'pcounter_save' it already exists quite insert it

INSERT INTO pcounter_save (save_name, save_value) VALUES ('max_time', 2455527);

if table not exists

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS pcounter_save ( save_name varchar(10) NOT NULL, save_value int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO pcounter_save (save_name, save_value) VALUES ('day_time', 2455527), ('max_count', 0), ('max_time', 2455527), ('counter', 0), ('yesterday', 0);

CREATE TABLE IF NOT EXISTS pcounter_users ( user_ip varchar(39) NOT NULL, user_time int(10) unsigned NOT NULL, UNIQUE KEY user_ip (user_ip) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO pcounter_users (user_ip, user_time) VALUES ('''127.0.0.1''', 1290821670);

#10437 report it
jiaming at 2012/10/27 11:56pm
Is there a way we can get the user id of each online user?

Is there a way we can get the user id of each online user? Thanks so much for the great extension.

#9038 report it
haryanto at 2012/07/16 12:04am
Nice

I try put in inside folder 'extensions' doesnt work. And i try put it inside folder 'components', its work.

#3969 report it
fouss at 2011/05/24 06:41am
working....

Greate extention............. working!

#2847 report it
ahmad at 2011/02/16 03:14am
Add new feature

I Add to this powerful class the ability to draw image for each digit, instead of return the number as text only. change the following functions to

public function getTotal(); public function getOnline(); public function getToday(); public function getYesterday(); public function getMaximal();

/**
     * Getter für die Anzahl aller Besucher.
     **/
    public function getTotal($drawValue=false,$style='',$ext='')
    {
        if ($drawValue)
            $this->drawValue($this->user_total,$style,$ext);
        else
            return $this->user_total;
    }
 
    /**
     * Getter für die Anzahl der User, die gerade Online sind.
     **/
    public function getOnline($drawValue=false,$style='',$ext='')
    {
        if ($drawValue)
            $this->drawValue($this->user_online,$style,$ext);
        else
            return $this->user_online;
    }
 
    /**
     * Getter für die Anzahl der User, die heute online waren.
     **/
    public function getToday($drawValue=false,$style='',$ext='')
    {
        if ($drawValue)
            $this->drawValue($this->user_today,$style,$ext);
        else
            return $this->user_today;
    }
 
    /**
     * Getter für die Anzahl der User, die Gestern online waren.
     **/
    public function getYesterday($drawValue=false,$style='',$ext='')
    {
        if ($drawValue)
            $this->drawValue($this->user_yesterday,$style,$ext);
        else
            return $this->user_yesterday;
    }
 
    /**
     * Getter für die maximale Anzahl der User, die an einem Tag online war.
     **/
    public function getMaximal($drawValue=false,$style='',$ext='')
    {
        if ($drawValue)
            $this->drawValue($this->user_max_count,$style,$ext);
        else
            return $this->user_max_count;
    }
 
    /**
     * Getter für den Zeitpunkt, an dem die maximale Anzahl der User online war.
     **/
    public function getMaximalTime()
    {
        return $this->user_time;
    }
 
    /**
     * Draw image for each digit.
     * $value: the number to be drawn.
     * $st: style name.
     * $ex: digit image extension.
     */
    public function drawValue($value,$st,$ex)
    {
        $base_url = "images/";
        $default_style = 'blue';
        $style      = (strlen(trim($st)) > 0) ? $st : $default_style;
        $style_dir  = 'counterstyles/' . $style . '/';
        $default_ext = 'gif';
        $ext        = (strlen(trim($ex)) > 0) ? $ex : $default_ext;
 
        /* Print out Javascript code and exit */
        $len = strlen($value);
        for ($i=0;$i<$len;$i++)
        {
            echo '<img src="'.$base_url . $style_dir . substr($value,$i,1) . '.' . $ext .'" border="0">';
        }
    }

you can call the function as the following: <?php echo Yii::app()->counter->getOnline(true,'style name','digit image extension'); ?> or as usual, return the number as text: <?php echo Yii::app()->counter->getOnline(); ?>

you can download style for digit images from here. extract the zipped file to images folder on you project root, so "your_project/images/counterstyles"

#2212 report it
mithereal at 2010/11/26 08:41pm
there was no sql file included, here it is

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS pcounter_save ( save_name varchar(10) NOT NULL, save_value int(10) unsigned NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO pcounter_save (save_name, save_value) VALUES ('day_time', 2455527), ('max_count', 0), ('counter', 0), ('yesterday', 0);

CREATE TABLE IF NOT EXISTS pcounter_users ( user_ip varchar(39) NOT NULL, user_time int(10) unsigned NOT NULL, UNIQUE KEY user_ip (user_ip) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO pcounter_users (user_ip, user_time) VALUES ('''127.0.0.1''', 1290821670);

#2086 report it
zaccaria at 2010/11/09 04:27pm
licence

This extension can be used for licence control.

You can check at the login the number of user logged in and forbid the login if there are more user logged in respect the user allowed by licence.

Nice job, indeed

#71 report it
swafsarl at 2010/09/24 12:30pm
Here u go

CREATE TABLE pcounter_users ( user_ip VARCHAR(39) NOT NULL, user_time INT(10) UNSIGNED NOT NULL, UNIQUE KEY user_ip (user_ip)) ENGINE = myisam;

CREATE TABLE pcounter_save ( save_name VARCHAR(10) NOT NULL, save_value INT(10) UNSIGNED NOT NULL) ENGINE = myisam;

#1156 report it
jerry2801 at 2009/11/20 10:01am
schema problem?

table: pcounter_users

table: pcounter_save

can you supply above table schema example?

#1165 report it
Gia Duy at 2009/11/19 11:41am
Great

simple but powerful :)

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: g3ck0
  • Category: Logging
  • Votes: +21 / -2
  • Downloaded: 3,123 times
  • Created on: Sep 10, 2009
  • Last updated: Oct 24, 2009