本文實例講述了CI(CodeIgniter)簡單統計訪問人數實現方法。分享給大家供大家參考,具體如下:
廢話不說,先上代碼:
控制器文件:
/application/controllers/hello.php 如下:
<?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);
}
}
視圖文件:
/application/views/test_views.php 如下:
<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>
運行結果如下圖所示:

更多關於CodeIgniter框架相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》
希望本文所述對大家基於CodeIgniter框架的PHP程序設計有所幫助。