[PLUGIN] Hitcounter

Post Reply
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

I want to implement a plugin which counts visits of the page and
displays the total number of visits.

It all works fine for me, but for some reason, the dbjson class don't write the right visit number
in the file.

For example:
First visit:
Displayed Visit #1
Next number should be 2 but dbjson writes a 3 in the file...

Next visit:
Displayed Visit #3
dbjson writes 5 in the file...

Here's the plugin:
http://www.itwerkstatt.omdriebigs-gspan ... ounter.zip

Maybe somebody can find the bug or give me a helpful hint?

Thank you very much!
dirtdiver2010
Master Bludit
Posts: 129
Joined: Fri Jan 15, 2016 6:07 pm
Has thanked: 1 time
Been thanked: 3 times

Maybe the visit is saved and he counts one for the save and one new?
I didn´t look at the code, because I didn´t understand enough... ;)
User avatar
diego
Site Admin
Posts: 773
Joined: Sat May 16, 2015 2:53 pm
Been thanked: 1 time
Contact:

Hi,
try with this:

Code: Select all

<?php

class pluginHitcounter extends Plugin {

	public function init()
	{
		$this->dbFields = array(
			'counter'=>0,
			'numberofchars'=>5,
			'text'=>'Anzahl Besucher'
		);
	}

	public function form()
	{
		global $Language;

		$html  = '<div>';
		$html .= '<label>'.$Language->get('text').'</label>';
		$html .= '<input name="text" id="jstext" type="text" value="'.$this->getDbField('text').'">';
		$html .= '</div>';

		$html .= '<div>';
		$html .= '<label>'.$Language->get('positions').'</label>';
		$html .= '<input name="numberofchars" id="jsnumberofchars" type="number" min="1" max="10" value="'.$this->getDbField('numberofchars').'">';
		$html .= '</div>';

		$html .= '<div>';
		$html .= '<label>'.$Language->get('current value').'</label>';
		$html .= '<input name="counter" id="jscounter" type="number" min="0" value="'.$this->getDbField('counter').'">';
		$html .= '</div>';

		return $html;
	}

	public function afterSiteLoad()
	{
		$this->increaseCounter();
	}

	public function siteSidebar()
	{
		$currentCount = $this->getDbField('counter');

		$html  = '<div class="plugin plugin-hitcounter">';

		// Print the label if not empty.
		$label = $this->getDbField('text');
		if( !empty($label) ) {
			$html .= '<h2>'.$label.'</h2>';
		}
		$html .= '<p>'.$this->createCounter($currentCount).'</p>';

 		$html .= '</div>';

		return $html;
	}

	private function increaseCounter() {
		$tmp = array();
		$tmp['counter'] 	= $this->getDbField('counter') + 1;
		$tmp['numberofchars']	= $this->getDbField('numberofchars');
		$tmp['text']		= $this->getDbField('text');
		$this->setDb($tmp);
	}

	private function createCounter($number) {
		$l = $this->getDbField('numberofchars');

		while (strlen($number) < $l)
		{
			$number = '0'.$number;
		}
		return $number;

	}
}
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Same problem. :(
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
User avatar
diego
Site Admin
Posts: 773
Joined: Sat May 16, 2015 2:53 pm
Been thanked: 1 time
Contact:

Change the method afterSiteLoad() for beforeSiteLoad()
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

Hmm, same error.
The weird thing is,
when I look at the sourcecode in the browser and refresh, the counter works correctly. :?:

I tracked the whole saving process:

Code: Select all


Current: 348
Next: 349
>>Entering function setDb in Plugin.class
After Sanitize value of counter: 349
Tmp Variable after loading Db from file
dbJSON Object
(
    [db] => Array
        (
            [counter] => 348
            [numberofchars] => 5
            [text] => Anzahl Besucher
        )

    [dbBackup] => Array
        (
            [counter] => 348
            [numberofchars] => 5
            [text] => Anzahl Besucher
        )

    [file] => /var/www/html/bludit/bl-content/databases/plugins/hitcounter/db.php
    [firstLine] => 1
)
1Setting array as new db in dbJSON Object
Tmp Variable after setting new array
dbJSON Object
(
    [db] => Array
        (
            [counter] => 349
            [numberofchars] => 5
            [text] => Anzahl Besucher
        )

    [dbBackup] => Array
        (
            [counter] => 348
            [numberofchars] => 5
            [text] => Anzahl Besucher
        )

    [file] => /var/www/html/bludit/bl-content/databases/plugins/hitcounter/db.php
    [firstLine] => 1
)
1Start saving
Entering save function of dbjson-class
Serialized Database: <?php defined('BLUDIT') or die('Bludit CMS.'); ?>
{
    "counter": "349",
    "numberofchars": "5",
    "text": "Anzahl Besucher"
}
But when I reload the page normally, it adds 2. For this example,
in db.php of the plugin is the value of the counter 350 :evil:

When I reload the page looking at the sourcecode, all works fine.
In db.php of the plugin is the value of counter 349 :shock:
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

Okay, I found a solution:

Code: Select all

(...)
	public function beforeSiteLoad() {
		$this->increaseCounter();
	}

	public function increaseCounter() {
		if (!isset($_SESSION['counterplugin'])) {
			$tmp = array();
			$tmp['counter'] = $this->getDbField('counter') + 1;
			$tmp['numberofchars'] = $this->getDbField('numberofchars');
			$tmp['text'] = $this->getDbField('text');
			$this->setDb($tmp);
			//Set Variable for this session so user cannot increase counter by pressing F5
			$_SESSION['counterplugin'] = 0;
		}
	}
(...)
Now it works.
Now I can make this plugin a little bit more beautiful and then upload in the repo.
Post Reply