会員制サイトのアクセスログを記録する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// app/Vendor/AccessLogs.php class AccessLogs{ function write_log($username) { $dir = LOGS . 'accesslogs/'; if (!file_exists($dir)) { mkdir($dir); } $msg = date('Y-m-d H:i:s') . ',"' . $username . '"' . ',"' . env('REQUEST_URI') . '"' . ',"' . env('HTTP_REFERER') . '"' . ',"' . env('HTTP_USER_AGENT') . '"' . ',"' . env('REMOTE_ADDR') . '"' . "\n"; $filename = LOGS . 'accesslogs/' . date('Ymd') . '.csv'; $log = new File($filename); $log->append($msg); return true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// app/Controller/AppController.php App::import('Vendor', 'AccessLogs'); class AppController extends Controller { .... public function beforeFilter() { $username = $this->Auth->user('username'); //会員のユーザ名を取得 $this->access = new AccessLogs(); $this->access->write_log($username); .... } .... } |
参考サイト
CakePHP 2.3 ログイン、操作履歴、アクセスログ出力 | エス技研
http://blog.s-giken.net/102.html