Home >Backend Development >PHP Tutorial >CI (CodeIgniter) simple method to count the number of visitors. Original, cicodeigniter_PHP tutorial
This article describes the CI (CodeIgniter) simple statistical visitor count implementation method. Share it with everyone for your reference, the details are as follows:
Without further ado, let’s start with the code:
Controller file:
/application/controllers/hello.php is as follows:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Hello extends CI_Controller { public function index() { echo "Hello CodeIgniter!"; } public function showval($name){ //访问路径:http://localhost/ci/index.php/hello/showval/Tom $this->name=$name; @$num=file_get_contents('./num.txt');//加上@屏蔽警告提示(第一次运行没有TXT文件会有警告提示) $num=$num?$num:0; $num++; $arr=array('v_name'=>$name,'v_num'=>$num); $re=fopen('./num.txt','w'); fwrite($re,$num); fclose($re); $this->load->view('test_views',$arr); } }
View file:
/application/views/test_views.php is as follows:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>访问统计</title> </head> <body> <?php echo $v_name;?>是第 <?php echo $v_num;?> 位访问者 </body> </html>
The running results are as shown below:
Readers who are interested in more content related to the CodeIgniter framework can check out the special topic on this site: "Introduction to codeigniter tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.