Skip to content

Instantly share code, notes, and snippets.

@Radiotechniman
Last active February 16, 2018 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radiotechniman/2fd57f82e51e033035fa0620d30580c7 to your computer and use it in GitHub Desktop.
Save Radiotechniman/2fd57f82e51e033035fa0620d30580c7 to your computer and use it in GitHub Desktop.
Simple php page to serve as a log for homey without the need for the actual simplelog app
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$dir = 'logs';
date_default_timezone_set("Europe/Amsterdam");
//here you set the time format the current setup will show YYYYMMDD HH:MM:SS
//please see the php date() documentation to change
$current_time = date('Ymd H:i:s' );
// if there is no "e" (entry) just type tywo dashes
if(!isset($_GET['e'])){
$txt = " -- ";}
else{
$txt = $_GET['e'];}
//if no "l" (log) is specified use the default log.txt
if(!isset($_GET['l'])){
$log_name = 'logs.txt';}
else{
//else use the logname+.txt as filename
$log_name = $_GET['l'] ;}
echo $current_time;
// create new directory with 744 permissions if it does not exist yet
// owner will be the user/group the PHP script is run under
// but thuis does not work properly yet, YMMV .
// run "ps aux | grep httpd" to see under which user the http daemon runs
// then make the logs sub-directory
// sudo chown phpuser:phpuser logs //phpusr depends on the user httpd runs under
// sudo chmod 755 logs
if ( !file_exists($dir) ) {
$oldmask = umask(0); // helpful when used in linux server
mkdir ($dir, 0744);
}
// set variables and write/add to file
$txt = "[".$current_time."] ".$txt ;
$myfile = file_put_contents($dir."/".$log_name.".txt", $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
$mybackup = file_put_contents($dir."/".$log_name.".bak", $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment