Codeigniter 中的命中计数器 [英] Hit Counter in Codeigniter

查看:31
本文介绍了Codeigniter 中的命中计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

(一步一步)

  1. counter.txt 放在 APPPATH 中.'日志/计数器.txt'
  2. APPPATH 中设置 counter_helper.php .'helpers/counter_helper.php';
  3. APPPATH 中自动加载新创建的帮助程序.'config/autoload.php' 文件;
  4. APPPATH 中制作 MY_Controller.php .'core/MY_Controller.php'
  5. 任何控制器都应该扩展 MY_Controller 而不是 CI_Controller;
  6. 在页面上使用:<?php echo $this->count_visitor;?>
  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;?>

帮手:

<?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];
    }
}

核心:

<?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 */

控制器:

<?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); 
 }
}

视图:

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

代码返回如下错误:

推荐答案

当我加载了帮助程序 $this->load->helper('counter');

应用程序 > 核心 > MY_Controller.php

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();
    }   
}

这篇关于Codeigniter 中的命中计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆