title: Hit Counter in Codeigniter
body: <p>I have the code below:</p>
  
  <p>(Step by step) </p>
  
  <ol>
  <li>Put <code>counter.txt</code> in <code>APPPATH . 'logs/counter.txt'</code></li>
  <li>Make <code>counter_helper.php</code> set in <code>APPPATH . 'helpers/counter_helper.php'</code>;</li>
  <li>Autoload newly created helper in <code>APPPATH . 'config/autoload.php'</code> file;</li>
  <li>Make <code>MY_Controller.php</code> in <code>APPPATH . 'core/MY_Controller.php'</code></li>
  <li>Any controller should extend <code>MY_Controller</code> instead of <code>CI_Controller</code>;</li>
  <li>Echo it on page with: <code>&lt;?php echo $this-&gt;count_visitor;?&gt;</code></li>
  </ol>
  
  <p>The Helper :</p>
  
  <pre><code>&lt;?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];
      }
  }
  </code></pre>
  
  <p>The Core :</p>
  
  <pre><code>&lt;?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-&gt;count_visitor = count_visitor();
     }   
   }
  /* End of file MY_Controller.php */
  /* Location: ./application/core/MY_Controller.php */
  </code></pre>
  
  <p>The Controller :</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
   class Home extends MY_Controller {
   public function index() {
   $data=array('isi'      =&gt;'home/index_home');
  $this-&gt;load-&gt;view('layout/wrapper',$data); 
   }
  }
  </code></pre>
  
  <p>The View :</p>
  
  <pre><code>&lt;?php echo $this-&gt;count_visitor;?&gt;
  </code></pre>
  
  <p>The code return an error like below :
  <a href="https://i.stack.imgur.com/uhZ9H.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uhZ9H.jpg" alt="enter image description here"></a></p>