用于记录每个用户的行为,和操作时间操作节点,<?php <br />
// +----------------------------------------------------------------------<br>
// | PHP@妖孽 [三十年河东三十年河西,莫欺少年穷.!]<br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2014 http://www.yaonies.com All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Author: PHP@妖孽 <msj><br>
// +----------------------------------------------------------------------<br>
/**<br>
+------------------------------------------------------------------------------<br>
* 基于用户的操作记录验证类<br>
+------------------------------------------------------------------------------<br>
* @category ORG<br>
* @package ORG<br>
* @subpackage Msj<br>
* @author PHP@妖孽 <msj><br>
* @version 1.0<br>
+------------------------------------------------------------------------------<br>
*/<br>
// 配置文件增加设置<br>
// 'OPERATION_ON'=>true,// 开启用户记录日志<br>
// 'OPERATION_MEMBER'=>'learn_member',<br>
// 'OPERATION_TYPE'=>'web',//分别为web,interface也就是网站,和接口<br>
// 'OPERATION_MEMBER_ID'=>'member_id', //如果后台就取session,如果接口就直接取get,post请求的值<br>
/*<br>
-- --------------------------------------------------------<br>
CREATE TABLE IF NOT EXISTS `msj_operation_log` (<br>
`operation_log` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '操作记录主键',<br>
`operation_uid` mediumint(4) NOT NULL DEFAULT '0' COMMENT '操作人/如果是接口返回-1暂不记录接口请求人',<br>
`operation_node` char(50) COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '操作节点',<br>
`operation_ip` mediumtext COLLATE utf8_bin NOT NULL COMMENT '记录操作IP,省市,等信息',<br>
`operation_time` int(10) NOT NULL DEFAULT '0' COMMENT '操作时间',<br>
PRIMARY KEY (`operation_log`),<br>
KEY `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)<br>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='@author PHP@妖孽\r\n@since 2014-5-4'<br>
<br>
*/<br>
class Operation {<br>
<br>
private $operation_on;//操作记录开关<br>
public $error;//错误信息<br>
<br>
/**<br>
* @todo 验证是否开启记录<br>
*/<br>
public function __construct(){<br>
$this->operation_on = C('OPERATION_ON');<br>
if($this->operation_on === false){<br>
return false;<br>
}<br>
}<br>
<br>
/**<br>
* @todo获取客户端IP地址<br>
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字<br>
* @return mixed<br>
*/<br>
private function getClientIp($type=0){<br>
$type = $type ? 1 : 0;<br>
static $ip = NULL;<br>
if ($ip !== NULL) return $ip[$type];<br>
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {<br>
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);<br>
$pos = array_search('unknown',$arr);<br>
if(false !== $pos) unset($arr[$pos]);<br>
$ip = trim($arr[0]);<br>
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {<br>
$ip = $_SERVER['HTTP_CLIENT_IP'];<br>
}elseif (isset($_SERVER['REMOTE_ADDR'])) {<br>
$ip = $_SERVER['REMOTE_ADDR'];<br>
}<br>
// IP地址合法验证<br>
$long = sprintf("%u",ip2long($ip));<br>
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);<br>
return $ip[$type];<br>
}<br>
<br>
/**<br>
* @todo 检测表是否存在,如果不存在则创建新表<br>
*/<br>
static public function checkTableIsExist(){<br>
$db = Db::getInstance(C('RBAC_DB_DSN'));<br>
$table_prefix = C('DB_PREFIX');<br>
$sql = "CREATE TABLE IF NOT EXISTS `{$table_prefix}msj_operation_log` (<br>
`operation_log` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '操作记录主键',<br>
`operation_uid` mediumint(4) NOT NULL DEFAULT '0' COMMENT '操作人/如果是接口返回-1暂不记录接口请求人',<br>
`operation_node` char(50) COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '操作节点',<br>
`operation_ip` mediumtext COLLATE utf8_bin NOT NULL COMMENT '记录操作IP,省市,等信息',<br>
`operation_time` int(10) NOT NULL DEFAULT '0' COMMENT '操作时间',<br>
PRIMARY KEY (`operation_log`),<br>
KEY `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)<br>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='@author PHP@妖孽\r\n@since 2014-5-4'";<br>
$db->execute($sql);<br>
}<br>
<br>
/**<br>
* @todo 写入操作日志<br>
*/<br>
public function writeLog(){<br>
(defined('NOW_TIME'))?$time = NOW_TIME: $time=time();<br>
<br>
switch (C('OPERATION_TYPE')){<br>
case 'web':<br>
$uid = session(C('OPERATION_MEMBER_ID'));<br>
$uid = ($uid)?$uid:0;<br>
break;<br>
case 'interface'://预留<br>
$uid = -1;//接口的操作日志暂时不记录操作人<br>
break;<br>
default:<br>
$uid = -2;<br>
break;<br>
}<br>
<br>
$db_name =C('DB_NAME') ;<br>
$table_prefix = C('DB_PREFIX');<br>
import('@.ORG.Msj.IpLocation');// 导入IpLocation类<br>
$Ip = new IpLocation(); // 实例化类<br>
$ip_info = $Ip->getlocation($this->getClientIp()); // 获取某个IP地址所在的位置<br>
$ip_info['country'] = iconv('gbk', 'utf-8', $ip_info['country']);<br>
$db = Db::getInstance(C('RBAC_DB_DSN'));<br>
$sql = "INSERT INTO `{$db_name}`.`{$table_prefix}msj_operation_log` (`operation_uid`, `operation_node`, `operation_ip`, `operation_time`) VALUES ('".$uid."','".$_SERVER['REQUEST_URI']."','".serialize($ip_info)."','".$time."');"; <br>
if($db->execute($sql) === false ){<br>
//插入失败写日志<br>
Log::write("uid:{$uid},".'node:'.$_SERVER['REQUEST_URI'].',operation_ip:'.serialize($ip_info).',time:'.date('Y-m-d H:i:s',$time));<br>
}<br>
<br>
}<br>
<br>
/**<br>
* @todo 查询操作日志<br>
* @param array $map 目前只支持用户id的查询.<br>
*/<br>
public function logList($map=array()){<br>
$db = Db::getInstance(C('RBAC_DB_DSN'));<br>
$member_table_name = C('OPERATION_MEMBER');<br>
$operation_table_name =C('DB_PREFIX').'msj_operation_log';<br>
$member_id = implode(',',$map);<br>
$sql = "(SELECT <br>
msj_operation_log.operation_log AS operation_log,<br>
msj_operation_log.operation_uid AS operation_uid,<br>
msj_operation_log.operation_node AS operation_node,<br>
msj_operation_log.operation_ip AS operation_ip,<br>
msj_operation_log.operation_time AS operation_time,<br>
Member.member_name AS member_name <br>
FROM<br>
{$operation_table_name} msj_operation_log <br>
JOIN {$member_table_name} Member <br>
ON msj_operation_log.operation_uid = Member.member_id <br>
WHERE (`member_id` IN('{$member_id}')))";<br>
$log_list = $db->query($sql);<br>
$Ip = new IpLocation(); // 实例化类<br>
$ip_info = $Ip->getlocation($this->getClientIp()); // 获取某个IP地址所在的位置<br>
if(!empty($log_list)){<br>
foreach($log_list as $key=>$val){<br>
$log_list[$key]['operation_time'] = date('Y-m-d H:i:s',$val['operation_time']);<br>
$info = unserialize($val['operation_ip']);<br>
$log_list[$key]['operation_ip'] = "地区:".$info['area'].',城市:'.$info['country'].',IP:'.$info['ip'];<br>
}<br>
return $log_list;<br>
}else{<br>
return false;<br>
}<br>
}<br>
<br>
public function __destruct(){<br>
$this->operation_on=false;<br>
$this->error ='';<br>
}<br>
<br>
<br>
}<br>
<br>
//查list;<br>
import('@.ORG.Msj.Operation');<br>
$operation_obj = new Operation();<br>
$log_list = $operation_obj->logList(array('member_id'=>2086));<br>
<br>
//记录日志<br>
<br>
$operation_obj->writeLog();</msj></msj>
AD:真正免费,域名+虚机+企业邮箱=0元