- Published on
Simple PHP Code Igniter class to show the error log
- Authors
- Name
- Nico Bistolfi
- @nicobistolfi
Sometimes we need to access the php log, but is not available on the document root, so yo need to access by console or create a symlink to some part inside the public folder of your website. You may need to add some credentials in order to protect your logs.
This is a simple and safer way to access your php error log, you can add this controller inside your protected controllers using the php session or maybe a cookie generated by you with some encrypted data.
So here is the controller class to show the php log really easy for Code Igniter.
<?php
/**
* Php Log class
*
* Display the php log
*
* @category Log
* @author Nico Bistolfi
*/
class Log extends Controller {
private $logPath; //path to the php log
/**
* Class constructor
*/
function __construct(){
parent::Controller();
$this->logPath = ini_get('error_log');
}
/**
* index: Shows the php error log
* @access public
*/
public function index(){
if (@is_file($this->logPath)) {
echo nl2br(@file_get_contents($this->logPath));
} else {
echo 'The log cannot be found in the specified route '.$this->logPath;
}
exit;
}
/**
* delete: Deletes the php error log
* @access public
*/
public function delete(){
if (@is_file($this->logPath)) {
if (@unlink($this->logPath)) {
echo 'PHP Error Log deleted';
} else {
echo 'There has been an error trying to delete the PHP Error log '.$this->logPath;
}
} else {
echo 'The log cannot be found in the specified route '.$this->logPath.'.';
}
exit;
}
}
?>