Codeigniter 中的点击计数器
Hit Counter in Codeigniter
我有以下代码:
(一步一步)
- 将
counter.txt
放入APPPATH . 'logs/counter.txt'
- 在
APPPATH . 'helpers/counter_helper.php'
中设置counter_helper.php
;
- 在
APPPATH . 'config/autoload.php'
文件中自动加载新创建的助手;
- 在
APPPATH . 'core/MY_Controller.php'
中制作MY_Controller.php
- 任何控制器都应该扩展
MY_Controller
而不是 CI_Controller
;
- 在页面上回显:
<?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;?>
代码 return 如下错误:
当我加载助手时,它可以正常工作 $this->load->helper('counter');
应用>核心>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();
}
}
是的,必须加载助手:
$this->load->helper('counter');
或
config/autoload.php: $autoload['helper'] = array('counter');
我有以下代码:
(一步一步)
- 将
counter.txt
放入APPPATH . 'logs/counter.txt'
- 在
APPPATH . 'helpers/counter_helper.php'
中设置counter_helper.php
; - 在
APPPATH . 'config/autoload.php'
文件中自动加载新创建的助手; - 在
APPPATH . 'core/MY_Controller.php'
中制作 - 任何控制器都应该扩展
MY_Controller
而不是CI_Controller
; - 在页面上回显:
<?php echo $this->count_visitor;?>
MY_Controller.php
帮手:
<?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;?>
代码 return 如下错误:
当我加载助手时,它可以正常工作 $this->load->helper('counter');
应用>核心>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();
}
}
是的,必须加载助手:
$this->load->helper('counter');
或
config/autoload.php: $autoload['helper'] = array('counter');