3

I have the code below:

(Step by step)

  1. Put counter.txt in APPPATH . 'logs/counter.txt'
  2. Make counter_helper.php set in APPPATH . 'helpers/counter_helper.php';
  3. Autoload newly created helper in APPPATH . 'config/autoload.php' file;
  4. Make MY_Controller.php in APPPATH . 'core/MY_Controller.php'
  5. Any controller should extend MY_Controller instead of CI_Controller;
  6. Echo it on page with: <?php echo $this->count_visitor;?>

The Helper :

<?php defined('BASEPATH') OR exit('No direct script access allowed.');

if ( ! function_exists('count_visitor')) {
    function count_visitor()
    {
        $filecounter=(APPPATH . 'logs/counter.txt');
        $kunjungan=file($filecounter);
        $kunjungan[0]++;
        $file=fopen($filecounter, 'w');
        fputs($file, $kunjungan[0]);
        fclose($file);
        return $kunjungan[0];
    }
}

The Core :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller
 {
  public $count_visitor;
  public function __construct()
   {
     parent::__construct();
      $this->count_visitor = count_visitor();
   }   
 }
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */

The Controller :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 class Home extends MY_Controller {
 public function index() {
 $data=array('isi'      =>'home/index_home');
$this->load->view('layout/wrapper',$data); 
 }
}

The View :

<?php echo $this->count_visitor;?>

The code return an error like below : enter image description here

BGKND
  • 53
  • 1
  • 9
ariyanti dwiastuti
  • 95
  • 1
  • 3
  • 10
  • 1
    can you please paste helper related code from your `/config/autoload.php`? – Vishal Patel Jul 28 '15 at 18:25
  • 2
    Yes, I wrote that [instruction](http://stackoverflow.com/questions/29751089/visitor-counter-and-online-support-with-codeigniter/29753156#answer-29753156) and it is tested and perfectly working. Don't forget to load helper. And to rename MY_Controller.php file like here I just wrote. – Tpojka Jul 28 '15 at 22:35
  • yeah... it's work. i just forgot to load the helper in the autoload. – ariyanti dwiastuti Jul 29 '15 at 11:29

2 Answers2

2

I got it to work fine when I loaded the helper $this->load->helper('counter');

application > core > MY_Controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
    public $count_visitor;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('counter');
        $this->count_visitor = count_visitor();
    }   
}
0

Yes, must load the helper:

$this->load->helper('counter');

or

config/autoload.php: $autoload['helper'] = array('counter');

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46