Commit f9592139 by Phạm Văn Đoan

cập nhật: lấy IP client để lưu vào log vào/ra

parent fc10bef1
...@@ -42,10 +42,11 @@ class Account extends CI_Controller ...@@ -42,10 +42,11 @@ class Account extends CI_Controller
if ($this->form_validation->run($this) == FALSE) { if ($this->form_validation->run($this) == FALSE) {
$this->load->view('backend/login_view'); $this->load->view('backend/login_view');
} else { } else {
$client_ip = trim($this->input->post('client_ip'));
$username = trim($this->input->post('txtUsername')); $username = trim($this->input->post('txtUsername'));
$password = MyHelper::genKeyCode(trim($this->input->post('txtPassword'))); $password = MyHelper::genKeyCode(trim($this->input->post('txtPassword')));
$checkUserAdmin = $this->admin_model->getUserAdminLogin($username, $password); $checkUserAdmin = $this->admin_model->getUserAdminLogin($username, $password);
//MyHelper::echoPreDie($checkUserAdmin); //
if ($checkUserAdmin) { if ($checkUserAdmin) {
// Thiết lập session // Thiết lập session
$sessionData = array( $sessionData = array(
...@@ -59,7 +60,7 @@ class Account extends CI_Controller ...@@ -59,7 +60,7 @@ class Account extends CI_Controller
); );
// Ghi log // Ghi log
$user_log_id = $this->userlog_model->add('LOGIN/LOGOUT', $this->input->post('txtUsername'), 'SUCCESS'); $user_log_id = $this->userlog_model->add('LOGIN/LOGOUT', $this->input->post('txtUsername'), 'SUCCESS', $client_ip);
$sessionData['user_log_id'] = $user_log_id; $sessionData['user_log_id'] = $user_log_id;
$sessionData['time_login'] = date('Y-m-d H:i:s'); $sessionData['time_login'] = date('Y-m-d H:i:s');
$this->session->set_userdata($sessionData); $this->session->set_userdata($sessionData);
...@@ -68,7 +69,7 @@ class Account extends CI_Controller ...@@ -68,7 +69,7 @@ class Account extends CI_Controller
redirect(base_url('backend/home/index')); redirect(base_url('backend/home/index'));
} else { } else {
// Ghi log // Ghi log
$this->userlog_model->add('LOGIN', $this->input->post('txtUsername'), 'FAILED'); $this->userlog_model->add('LOGIN', $this->input->post('txtUsername'), 'FAILED', $client_ip);
// Chuyển trang // Chuyển trang
$this->session->set_flashdata('loginErrorMsg', 'Đăng nhập không thành công!'); $this->session->set_flashdata('loginErrorMsg', 'Đăng nhập không thành công!');
......
...@@ -8,12 +8,22 @@ class Userlog_model extends CI_Model { ...@@ -8,12 +8,22 @@ class Userlog_model extends CI_Model {
date_default_timezone_set("Asia/Ho_Chi_Minh"); date_default_timezone_set("Asia/Ho_Chi_Minh");
} }
public function add($action, $account_input, $result, $type='BE'){ /**
* Hàm lưu log vào/ra hệ thống
*
* @param $action
* @param $account_input
* @param $result
* @param null $ip
* @param string $type
* @return mixed
*/
public function add($action, $account_input, $result, $ip=null, $type='BE'){
$dataInsert = array( $dataInsert = array(
'action' => $action, 'action' => $action,
'time_login' => date('Y-m-d H:i:s'), 'time_login' => date('Y-m-d H:i:s'),
'account_input' => $account_input, 'account_input' => $account_input,
'ip_address' => $this->input->ip_address(), 'ip_address' => $ip,
'user_agent' => $this->input->user_agent(), 'user_agent' => $this->input->user_agent(),
'type' => $type, 'type' => $type,
'result' => $result 'result' => $result
...@@ -23,6 +33,12 @@ class Userlog_model extends CI_Model { ...@@ -23,6 +33,12 @@ class Userlog_model extends CI_Model {
return $insert_id; return $insert_id;
} }
/**
* Hàm update thời gian sử dụng hệ thống
*
* @param $id
* @param $time_login
*/
public function update($id, $time_login){ public function update($id, $time_login){
$time_logout = date('Y-m-d H:i:s'); $time_logout = date('Y-m-d H:i:s');
$duration = (int)(strtotime($time_logout) - strtotime($time_login)); $duration = (int)(strtotime($time_logout) - strtotime($time_login));
...@@ -34,11 +50,22 @@ class Userlog_model extends CI_Model { ...@@ -34,11 +50,22 @@ class Userlog_model extends CI_Model {
$this->db->update($this->_table, $dataUpdate); $this->db->update($this->_table, $dataUpdate);
} }
/**
* Hàm xóa thông tin log vào/ra
*
* @param $id
*/
public function delete($id){ public function delete($id){
$this->db->where('id', $id); $this->db->where('id', $id);
$this->db->delete($this->_table); $this->db->delete($this->_table);
} }
/**
* Hàm lấy thông tin chi tiết log vào/ra
*
* @param $id
* @return mixed
*/
public function getById($id){ public function getById($id){
$this->db->select($this->_table.'.*'); $this->db->select($this->_table.'.*');
$this->db->from($this->_table); $this->db->from($this->_table);
...@@ -47,15 +74,25 @@ class Userlog_model extends CI_Model { ...@@ -47,15 +74,25 @@ class Userlog_model extends CI_Model {
return $query->result_array(); return $query->result_array();
} }
public function countAll($dtFrom=NULL, $dtTo=NULL, $filterByIP=NULL, $filterByAccount=NULL, $filterByResult=NULL){ /**
* Hàm lấy tổng số bản ghi log vào/ra
*
* @param null $dtFrom
* @param null $dtTo
* @param null $filterByIP
* @param null $filterByAccount
* @param null $filterByResult
* @return int
*/
public function countAll($dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByResult=null){
$this->db->select('COUNT(*) AS totalResults'); $this->db->select('COUNT(*) AS totalResults');
$this->db->from($this->_table); $this->db->from($this->_table);
if($dtFrom != NULL){$this->db->where('DATE(time_login) >=', $dtFrom);} if($dtFrom != null){$this->db->where('DATE(time_login) >=', $dtFrom);}
if($dtTo != NULL){$this->db->where('DATE(time_login) <=', $dtTo);} if($dtTo != null){$this->db->where('DATE(time_login) <=', $dtTo);}
if($filterByIP != NULL){$this->db->like('ip_address', $filterByIP);} if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
if($filterByAccount != NULL){$this->db->like('account_input', $filterByAccount);} if($filterByAccount != null){$this->db->like('account_input', $filterByAccount);}
if($filterByResult != NULL){$this->db->where('UPPER(result)', $filterByResult);} if($filterByResult != null){$this->db->where('UPPER(result)', $filterByResult);}
$query = $this->db->get(); $query = $this->db->get();
$result = $query->result_array(); $result = $query->result_array();
...@@ -66,15 +103,28 @@ class Userlog_model extends CI_Model { ...@@ -66,15 +103,28 @@ class Userlog_model extends CI_Model {
} }
} }
public function getPagination($record, $start, $dtFrom=NULL, $dtTo=NULL, $filterByIP=NULL, $filterByAccount=NULL, $filterByResult=NULL){ /**
* Hàm lấy dữ liệu phân trang log vào/ra
*
* @param $record
* @param $start
* @param null $dtFrom
* @param null $dtTo
* @param null $filterByIP
* @param null $filterByAccount
* @param null $filterByResult
*
* @return mixed
*/
public function getPagination($record, $start, $dtFrom=null, $dtTo=null, $filterByIP=null, $filterByAccount=null, $filterByResult=null){
$this->db->select($this->_table.'.*'); $this->db->select($this->_table.'.*');
$this->db->from($this->_table); $this->db->from($this->_table);
if($dtFrom != NULL){$this->db->where('DATE(time_login) >=', $dtFrom);} if($dtFrom != null){$this->db->where('DATE(time_login) >=', $dtFrom);}
if($dtTo != NULL){$this->db->where('DATE(time_login) <=', $dtTo);} if($dtTo != null){$this->db->where('DATE(time_login) <=', $dtTo);}
if($filterByIP != NULL){$this->db->like('ip_address', $filterByIP);} if($filterByIP != null){$this->db->like('ip_address', $filterByIP);}
if($filterByAccount != NULL){$this->db->like('account_input', $filterByAccount);} if($filterByAccount != null){$this->db->like('account_input', $filterByAccount);}
if($filterByResult != NULL){$this->db->where('UPPER(result)', $filterByResult);} if($filterByResult != null){$this->db->where('UPPER(result)', $filterByResult);}
$this->db->order_by($this->_table.'.id', 'desc'); $this->db->order_by($this->_table.'.id', 'desc');
$this->db->limit($record, $start); $this->db->limit($record, $start);
......
...@@ -31,7 +31,7 @@ $buttonLogin = array( ...@@ -31,7 +31,7 @@ $buttonLogin = array(
<meta http-equiv="refresh" content="1800" /> <meta http-equiv="refresh" content="1800" />
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>Đăng nhập CMS</title> <title>Đăng nhập CMS</title>
<link rel="shortcut icon" href="<?php echo base_url() ?>images/favicon.png" sizes="24x24" type="image/png" /> <link rel="shortcut icon" href="<?php echo base_url('images/favicon.png') ?>" sizes="24x24" type="image/png" />
<meta name="description" content="User login page"/> <meta name="description" content="User login page"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<!-- bootstrap & fontawesome --> <!-- bootstrap & fontawesome -->
...@@ -39,6 +39,7 @@ $buttonLogin = array( ...@@ -39,6 +39,7 @@ $buttonLogin = array(
<link rel="stylesheet" href="<?php echo base_url('public/assets/font-awesome/4.2.0/css/font-awesome.min.css') ?>"/> <link rel="stylesheet" href="<?php echo base_url('public/assets/font-awesome/4.2.0/css/font-awesome.min.css') ?>"/>
<!-- ace styles --> <!-- ace styles -->
<link rel="stylesheet" href="<?php echo base_url('public/assets/css/ace.min.css') ?>"/> <link rel="stylesheet" href="<?php echo base_url('public/assets/css/ace.min.css') ?>"/>
<script src="<?php echo base_url('public/assets/js/jquery.2.1.1.min.js') ?>"></script>
</head> </head>
...@@ -74,7 +75,7 @@ $buttonLogin = array( ...@@ -74,7 +75,7 @@ $buttonLogin = array(
?> ?>
<fieldset> <fieldset>
<div> <div>
<input type="hidden" name="client_ip" value=""> <input type="hidden" name="client_ip" id="client_ip" value="">
</div> </div>
<label class="block clearfix"> <label class="block clearfix">
<span class="block input-icon input-icon-right"> <span class="block input-icon input-icon-right">
...@@ -113,5 +114,14 @@ $buttonLogin = array( ...@@ -113,5 +114,14 @@ $buttonLogin = array(
<!-- /.main-content --> <!-- /.main-content -->
</div> </div>
<!-- /.main-container --> <!-- /.main-container -->
<script>
$.getJSON('https://jsonip.com/?callback=?', function(data) {
var ip = 'unknow';
if (data !== null) {
ip = data.ip;
}
$('#client_ip').val(ip);
});
</script>
</body> </body>
</html> </html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment