I have the code below:
(Step by step)
- Put
counter.txt
inAPPPATH . 'logs/counter.txt'
- Make
counter_helper.php
set inAPPPATH . 'helpers/counter_helper.php'
; - Autoload newly created helper in
APPPATH . 'config/autoload.php'
file; - Make
MY_Controller.php
inAPPPATH . 'core/MY_Controller.php'
- Any controller should extend
MY_Controller
instead ofCI_Controller
; - 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 :