本文实例讲述了CI(CodeIgniter)简单统计访问人数实现方法。分享给大家供大家参考,具体如下:
废话不说,先上代码:
控制器文件:
/application/controllers/hello.php 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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 如下:
1
2
3
4
5
6
7
8
9
|
<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框架的PHP程序设计有所帮助。