概述
在前两篇文章中我们学习了如何编写一个插件和连接数据库,这两个学会了基本简单的插件都可以自己编写了。今天我们就来编写一个基本的插件一个IP地址记录插件,就是当有人访问你网站的时候会自动把访问者IP和访问页和IP所对应的地址机录下来。让我们start吧。
一、创建插件构造文件
首先创建一个文件夹命名为“Visits”,在里面新建一个Plugin.php并在文件中创建一个类名称为“Visits_Plugin”实现Typecho_Plugin_Interface接口,分别编写接口的函数。这里我就不多说了不会的看我之前的文章。到这里我们的文件和构造都搞好了开始编写功能。
二、数据库处理
首先获取数据库处理器和前缀,在文件中创建一个静态的函数用于创建数据库
这里先定义一下数据库需要存什么数据字段名称是什么。
我定义了几个字段分别是id,ip,time,location,page代表id,ip地址,访问时间,ip对应的位置信息,访问了哪一页
定义好后开始写SQL语句如果不会写的可以用工具生成sql语句,开始创建数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| //判断数据表不存在创建数据表 public static function installDB() { $db = Typecho_Db::get(); $prefix = self::$db->getPrefix();
$sql = "CREATE TABLE IF NOT EXISTS `" . $prefix . "visits` ( `id` int(255) UNSIGNED NOT NULL AUTO_INCREMENT, `ip` varchar(255) NOT NULL, `time` varchar(255) NOT NULL, `location` varchar(255) NOT NULL, `page` varchar(255) NOT NULL, PRIMARY KEY (`id`) )ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $db->query($sql, Typecho_Db::WRITE); }
//插入数据到服务器 public static function insertData($ip, $time, $locaion, $page) { $db = Typecho_Db::get(); $prefix = $db->getPrefix(); $data = []; $data['ip'] = $ip; $data['time'] = $time; $data['location'] = $locaion; $data['page'] = $page; $db->query($db->insert($prefix . 'visits')->rows($data)); }
|
三、编写函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| //获取IP地址位置的函数 public static function getIPLocation($ip) { $body = file_get_contents("http://whois.pconline.com.cn/ip.jsp?ip=$ip"); return iconv('gb2312', 'utf-8', $body); }
public static function get_http_type() { $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; return $http_type; }
//用于给别人调用的接口函数 public static function render() { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; if (!$ip) $ip = $_SERVER['REMOTE_ADDR']; $time = strval(time()); $location = self::getIPLocation($ip); $page = self::get_http_type() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?' . $_SERVER['QUERY_STRING']; self::insertData($ip, date('Y年m月d日_H:m:i',$time), $location, $page); }
|
四、编写简单的后台页面这里也是直接贴代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?php // include 'common.php'; include 'header.php'; include 'menu.php'; ?>
<style> .list input { border: none; font-size: 15px; }
.list .head th { border-bottom: 2px solid #eee; }
.list tbody td { border-bottom: 1px solid #eee; }
.list .content { height: 50px; }
</style>
<head> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> </head>
<?php $prefix = $db->getPrefix(); $vists = $db->fetchAll( $db->select()->from($prefix . 'visits')->order($prefix . 'visits.id', Typecho_Db::SORT_DESC));
?>
<div class="main" align="center"> <p> <h2>管理网站访问者位置信息</h2></p> <p> <table class="list"> <colgroup> <col width="50"/> <col width="100"/> <col width="150"/> <col width="250"/> <col width="200"/> </colgroup>
<thead> <tr class="head" style="height: 50px"> <th>ID</th> <th>IP</th> <th>位置</th> <th>访问页面</th> <th>时间</th> </tr> </thead>
<tbody> <?php foreach ($vists as $vist): ?> <tr align="center" class="content"> <td><label><?php echo $vist['id']; ?></label></td> <td><label><?php echo $vist['ip']; ?></label></td> <td><label><?php echo $vist['location']; ?></label></td> <td><label><?php echo $vist['page']; ?></label></td> <td><label><?php echo $vist['time']; ?></label></td> </tr> <?php endforeach; ?>
</tbody> </table> </p>
</div>
|
五、使用插件
把本插件放置在usr/plugins目录后启用本插件,并在你当前主题的header.php中加入
1
| <?php Visits_Plugin::render(); ?>
|
即可在后台中查看访问者的信息
源码已经开源放到Gitee上了