Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a logfile in php

Tags:

php

logfile

I want to create a logfile for my system to register/log every action they do inside the system. But I have no idea how to do it.

For example, I have this php code that does the login function.

public function hasAccess($username,$password){     $form = array();     $form['username'] = $username;     $form['password'] = $password;       $securityDAO = $this->getDAO('SecurityDAO');     $result = $securityDAO->hasAccess($form);     //var_dump($form);     //var_dump($result);     if($result[0]['success']=='1'){         $this->Session->add('user_id', $result[0]['id']);         //$this->Session->add('username', $result[0]['username']);         //$this->Session->add('roleid', $result[0]['roleid']);         return $this->status(0,true,'auth.success',$result);     }else{         return $this->status(0,false,'auth.failed',$result);     } } 

now I want to create a logfile entitled 'the date today', then when that functions is used for loging in, it will write that user has logged in, the same with other functions. But I only want a single file for every day.

Could anyone be kind enough to guide and teach me how I should do my codes?

like image 229
maecy m Avatar asked Nov 11 '13 04:11

maecy m


People also ask

How do I create a PHP error log?

Enable Error Logging in php. To log errors in PHP, open the php. ini file and uncomment/add the following lines of code. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Is there a PHP log?

Enabling the PHP Error LogLog messages can be generated manually by calling error_log() or automatically when notices, warnings, or errors come up during execution. By default, the error log in PHP is disabled. You can enable the error log in one of two ways: by editing php. ini or by using ini_set.


1 Answers

To write to a log file and make a new one each day, you could use date("j.n.Y") as part of the filename.

//Something to write to txt log $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.         "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.         "User: ".$username.PHP_EOL.         "-------------------------".PHP_EOL; //Save string to log, use FILE_APPEND to append. file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND); 

So you would place that within your hasAccess() method.

public function hasAccess($username,$password){     $form = array();     $form['username'] = $username;     $form['password'] = $password;      $securityDAO = $this->getDAO('SecurityDAO');     $result = $securityDAO->hasAccess($form);      //Write action to txt log     $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.             "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.             "User: ".$username.PHP_EOL.             "-------------------------".PHP_EOL;     //-     file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);      if($result[0]['success']=='1'){         $this->Session->add('user_id', $result[0]['id']);         //$this->Session->add('username', $result[0]['username']);         //$this->Session->add('roleid', $result[0]['roleid']);         return $this->status(0,true,'auth.success',$result);     }else{         return $this->status(0,false,'auth.failed',$result);     } } 
like image 163
Lawrence Cherone Avatar answered Sep 28 '22 01:09

Lawrence Cherone