User behavior record based on ThinkPHP

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to record the behavior of each user and the operation time on the Operation node,
// + ----------------------------------------------------------------------
// | PHP @ [thirty years in the east and thirty years in the Hexi District, do not bully teenagers poor.]
// + ----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yaonies.com All rights reserved.
// + ----------------------------------------------------------------------
// | Author: PHP @ enchanting
// + ----------------------------------------------------------------------
/**
+ ------------------------------------------------------------------------------
* User-based operation record verification
+ ------------------------------------------------------------------------------
* @ Category ORG
* @ Package ORG
* @ Subpackage Msj
* @ Author PHP @
* @ Version 1.0
+ ------------------------------------------------------------------------------
*/
// Add settings to the configuration file
// 'Operation _ on' => true, // enable user logging
// 'Operation _ member' => 'learn _ member ',
// 'Operation _ type' => 'web', // web, website, and interface respectively
// 'Operation _ MEMBER_ID '=> 'Member _ id', // if the session is obtained in the background, the get and post request values are obtained directly if the interface is used.
/*
----------------------------------------------------------
Create table if not exists 'msj _ operation_log '(
'Operation _ log' mediumint (8) unsigned not null AUTO_INCREMENT COMMENT 'Operation record primary key ',
'Operation _ uid' mediumint (4) not null default '0' comment' operator/If the interface returns-1, the interface requestor is NOT recorded at the moment ',
'Operation _ node' char (50) COLLATE utf8_bin not null default ''comment' operation node ',
'Operation _ ip' mediumtext COLLATE utf8_bin not null comment' records the operation ip address, province, city, and other information ',
'Operation _ time' int (10) not null default '0' COMMENT 'Operation time ',
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 @ enchanting \ r \ n @ since 2014-5-4'

*/
Class Operation {

Private $ operation_on; // operation record switch
Public $ error; // error message

/**
* @ Todo verify whether record is enabled
*/
Public function _ construct (){
$ This-> operation_on = C ('Operation _ on ');
If ($ this-> operation_on === false ){
Return false;
}
}

/**
* @ Todo get the client IP Address
* @ Param integer $ type return type 0 return IP address 1 return IPV4 address number
* @ 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 '];
}
// Valid IP address verification
$ Long = sprintf ("% u", ip2long ($ ip ));
$ Ip = $ long? Array ($ ip, $ long): array ('0. 0.0.0 ', 0 );
Return $ ip [$ type];
}

/**
* @ Todo: Check whether the table exists. If not, create a new table.
*/
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 record primary key ',
'Operation _ uid' mediumint (4) not null default '0' comment' operator/If the interface returns-1, the interface requestor is NOT recorded at the moment ',
'Operation _ node' char (50) COLLATE utf8_bin not null default ''comment' operation node ',
'Operation _ ip' mediumtext COLLATE utf8_bin not null comment' records the operation ip address, province, city, and other information ',
'Operation _ time' int (10) not null default '0' COMMENT 'Operation time ',
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 write operation log
*/
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': // Reserved
$ Uid =-1; // operation logs of the interface do not record the operator
Break;
Default:
$ Uid =-2;
Break;
}

$ Db_name = C ('db _ name ');
$ Table_prefix = C ('db _ prefix ');
Import ('@. ORG. Msj. iplocation'); // import the IpLocation class
$ Ip = new IpLocation (); // instantiate the class
$ Ip_info = $ Ip-> getlocation ($ this-> getClientIp (); // obtain the location of an Ip address
$ 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 ){
// Insert failed write log
Log: write ("uid: {$ uid },". 'node :'. $ _ SERVER ['request _ URI ']. ', operation_ip :'. serialize ($ ip_info ). ', time :'. date ('Y-m-d H: I: s', $ time ));
}

}

/**
* @ Todo query operation logs
* @ Param array $ map currently only supports user ID query.
*/
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 (); // instantiate the class
$ Ip_info = $ Ip-> getlocation ($ this-> getClientIp (); // obtain the location of an Ip address
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'] = "region :". $ info ['region']. ', city :'. $ info ['country']. ', IP :'. $ info ['IP'];
}
Return $ log_list;
} Else {
Return false;
}
}

Public function _ destruct (){
$ This-> operation_on = false;
$ This-> error = '';
}


}

// Query the list;
Import ('@. ORG. Msj. operation ');
$ Operation_obj = new Operation ();
$ Log_list = $ operation_obj-> logList (array ('Member _ id' => 2086 ));

// Record logs

$ Operation_obj-> writeLog ();

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.