用于记录每个用户的行为,和操作时间操作节点,
true,// 开启用户记录日志
// 'operation_member'=>'learn_member',
// 'operation_type'=>'web',//分别为web,interface也就是网站,和接口
// 'operation_member_id'=>'member_id', //如果后台就取session,如果接口就直接取get,post请求的值
/*
-- --------------------------------------------------------
create table if not exists `msj_operation_log` (
`operation_log` mediumint(8) unsigned not null auto_increment comment '操作记录主键',
`operation_uid` mediumint(4) not null default '0' comment '操作人/如果是接口返回-1暂不记录接口请求人',
`operation_node` char(50) collate utf8_bin not null default '' comment '操作节点',
`operation_ip` mediumtext collate utf8_bin not null comment '记录操作ip,省市,等信息',
`operation_time` int(10) not null default '0' comment '操作时间',
primary key (`operation_log`),
key `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)
) engine=innodb default charset=utf8 collate=utf8_bin comment='@author php@妖孽\r\n@since 2014-5-4'
*/
class operation {
private $operation_on;//操作记录开关
public $error;//错误信息
/**
* @todo 验证是否开启记录
*/
public function __construct(){
$this->operation_on = c('operation_on');
if($this->operation_on === false){
return false;
}
}
/**
* @todo获取客户端ip地址
* @param integer $type 返回类型 0 返回ip地址 1 返回ipv4地址数字
* @return mixed
*/
private function getclientip($type=0){
$type = $type ? 1 : 0;
static $ip = null;
if ($ip !== null) return $ip[$type];
if (isset($_server['http_x_forwarded_for'])) {
$arr = explode(',', $_server['http_x_forwarded_for']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_server['http_client_ip'])) {
$ip = $_server['http_client_ip'];
}elseif (isset($_server['remote_addr'])) {
$ip = $_server['remote_addr'];
}
// ip地址合法验证
$long = sprintf(%u,ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
/**
* @todo 检测表是否存在,如果不存在则创建新表
*/
static public function checktableisexist(){
$db = db::getinstance(c('rbac_db_dsn'));
$table_prefix = c('db_prefix');
$sql = create table if not exists `{$table_prefix}msj_operation_log` (
`operation_log` mediumint(8) unsigned not null auto_increment comment '操作记录主键',
`operation_uid` mediumint(4) not null default '0' comment '操作人/如果是接口返回-1暂不记录接口请求人',
`operation_node` char(50) collate utf8_bin not null default '' comment '操作节点',
`operation_ip` mediumtext collate utf8_bin not null comment '记录操作ip,省市,等信息',
`operation_time` int(10) not null default '0' comment '操作时间',
primary key (`operation_log`),
key `index_uid_node` (`operation_uid`,`operation_node`,`operation_log`)
) engine=innodb default charset=utf8 collate=utf8_bin comment='@author php@妖孽\r\n@since 2014-5-4';
$db->execute($sql);
}
/**
* @todo 写入操作日志
*/
public function writelog(){
(defined('now_time'))?$time = now_time: $time=time();
switch (c('operation_type')){
case 'web':
$uid = session(c('operation_member_id'));
$uid = ($uid)?$uid:0;
break;
case 'interface'://预留
$uid = -1;//接口的操作日志暂时不记录操作人
break;
default:
$uid = -2;
break;
}
$db_name =c('db_name') ;
$table_prefix = c('db_prefix');
import('@.org.msj.iplocation');// 导入iplocation类
$ip = new iplocation(); // 实例化类
$ip_info = $ip->getlocation($this->getclientip()); // 获取某个ip地址所在的位置
$ip_info['country'] = iconv('gbk', 'utf-8', $ip_info['country']);
$db = db::getinstance(c('rbac_db_dsn'));
$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.');;
if($db->execute($sql) === false ){
//插入失败写日志
log::write(uid:{$uid},.'node:'.$_server['request_uri'].',operation_ip:'.serialize($ip_info).',time:'.date('y-m-d h:i:s',$time));
}
}
/**
* @todo 查询操作日志
* @param array $map 目前只支持用户id的查询.
*/
public function loglist($map=array()){
$db = db::getinstance(c('rbac_db_dsn'));
$member_table_name = c('operation_member');
$operation_table_name =c('db_prefix').'msj_operation_log';
$member_id = implode(',',$map);
$sql = (select
msj_operation_log.operation_log as operation_log,
msj_operation_log.operation_uid as operation_uid,
msj_operation_log.operation_node as operation_node,
msj_operation_log.operation_ip as operation_ip,
msj_operation_log.operation_time as operation_time,
member.member_name as member_name
from
{$operation_table_name} msj_operation_log
join {$member_table_name} member
on msj_operation_log.operation_uid = member.member_id
where (`member_id` in('{$member_id}')));
$log_list = $db->query($sql);
$ip = new iplocation(); // 实例化类
$ip_info = $ip->getlocation($this->getclientip()); // 获取某个ip地址所在的位置
if(!empty($log_list)){
foreach($log_list as $key=>$val){
$log_list[$key]['operation_time'] = date('y-m-d h:i:s',$val['operation_time']);
$info = unserialize($val['operation_ip']);
$log_list[$key]['operation_ip'] = 地区:.$info['area'].',城市:'.$info['country'].',ip:'.$info['ip'];
}
return $log_list;
}else{
return false;
}
}
public function __destruct(){
$this->operation_on=false;
$this->error ='';
}
}
//查list;
import('@.org.msj.operation');
$operation_obj = new operation();
$log_list = $operation_obj->loglist(array('member_id'=>2086));
//记录日志
$operation_obj->writelog();
ad:真正免费,域名+虚机+企业邮箱=0元