Reprinted (16/05/23) based on thinkphp user behavior records.

Source: Internet
Author: User
<span id="Label3"></p><p><?php<br>// +----------------------------------------------------------------------<br>// | [email protected] Doer [30 years east of the east 30 years hexi, mo Bullying Juvenile poor.!]<br>// +----------------------------------------------------------------------<br>// | Copyright (c) http://www.yaonies.com All rights reserved.<br>// +----------------------------------------------------------------------<br>// | Author: [email protected] Doer <[email protected]><br>// +----------------------------------------------------------------------<br>/**<br>+------------------------------------------------------------------------------<br>* User-based Action Record Validation class<br>+------------------------------------------------------------------------------<br>* @category ORG<br>* @package ORG<br>* @subpackage MSJ<br>* @author [email protected] doer <[email protected]><br>* @version 1.0<br>+------------------------------------------------------------------------------<br>*/<br>Configuration file Increase settings<br>' operation_on ' =>true,//turn on user logging<br>' Operation_member ' = ' learn_member ',<br>' Operation_type ' = ' web ',//web,interface is the website, and interface<br>' operation_member_id ' = ' member_id ',//if the background takes session, if the interface takes the value of the Get,post request directly<br>/*<br>-- --------------------------------------------------------<br>CREATE TABLE IF not EXISTS ' Msj_operation_log ' (<br>' Operation_log ' mediumint (8) unsigned not NULL auto_increment COMMENT ' operation Record primary key ',<br>' Operation_uid ' mediumint (4) not NULL DEFAULT ' 0 ' COMMENT ' operator/if interface is returned-1 does not record interface requestor ',<br>' Operation_node ' char (COLLATE) utf8_bin not NULL DEFAULT ' COMMENT ' Operation node ',<br>' Operation_ip ' mediumtext COLLATE utf8_bin not NULL COMMENT ' record operation ip, provinces, and other information ',<br>' operation_time ' int (ten) not NULL DEFAULT ' 0 ' COMMENT ' Operation time ',<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 [email protected] doer \r\[email protected] 2014-5-4 ‘<br>*/<br>Class Operation {<br> <br>Private $operation _on;//operation Record switch<br>Public $error;//error Message<br> <br>/**<br>* @todo Verify that the record is turned on<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 Get client IP address<br>* @param integer $type return type 0 return IP address 1 return IPV4 address number<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 Address legal authentication<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 the detection table exists and creates a new table if it does not exist<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 ' operation Record primary key ',<br>' Operation_uid ' mediumint (4) not NULL DEFAULT ' 0 ' COMMENT ' operator/if interface is returned-1 does not record interface requestor ',<br>' Operation_node ' char (COLLATE) utf8_bin not NULL DEFAULT ' COMMENT ' Operation node ',<br>' Operation_ip ' mediumtext COLLATE utf8_bin not NULL COMMENT ' record operation ip, provinces, and other information ',<br>' operation_time ' int (ten) not NULL DEFAULT ' 0 ' COMMENT ' Operation time ',<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 [email protected] doer \r\[email protected] 2014-5-4 ‘";<br>$db->execute ($sql);<br>}<br> <br>/**<br>* @todo Write operation log<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 '://reserved<br>$uid = Operation log for -1;//interface is temporarily not logged operator<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 ');//importing iplocation class<br>$Ip = new Iplocation (); Instantiating classes<br>$ip _info = $Ip->getlocation ($this->getclientip ()); Get where an IP address is located<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>Insert failed Write log<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 Query Operation log<br>* @param array $map queries that currently only support user IDS.<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 (); Instantiating classes<br>$ip _info = $Ip->getlocation ($this->getclientip ()); Get where an IP address is located<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 '] = "region:". $info [' area ']. ', city: '. $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>Check list;<br>Import (' @.org.msj.operation ');<br>$operation _obj = new Operation ();<br>$log _list = $operation _obj->loglist (array (' member_id ' =>2086);<br>Record log<br>$operation _obj->writelog ();</p><p><p>Reprinted (16/05/23) based on thinkphp user behavior records.</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.