Codeigniter 中的命中计数器 [英] Hit Counter in Codeigniter
本文介绍了Codeigniter 中的命中计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
(一步一步)
- 将
counter.txt
放在APPPATH 中.'日志/计数器.txt'
- 在
APPPATH 中设置
;counter_helper.php
.'helpers/counter_helper.php' - 在
APPPATH 中自动加载新创建的帮助程序.'config/autoload.php'
文件; - 在
APPPATH 中制作
MY_Controller.php
.'core/MY_Controller.php' - 任何控制器都应该扩展
MY_Controller
而不是CI_Controller
; - 在页面上使用:
<?php echo $this->count_visitor;?>
- 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;?>
帮手:
<?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屋!
查看全文