I've just started learning PHP and I'm trying to log data to a .txt file so I can see whats going on as the program runs.
I have created this function to write to a file.
functions.php
<?php
function logThis($textString){
$date = date_create();
$stamp = date_format($date, '[d-m][H:i:s] ');
$file = fopen("log.txt","a") or die ('fopen failed');
fwrite($file, $stamp . $textString . PHP_EOL);
fclose($file) or die ('fclose failed');
}
?>
I am then including this code into the pages i want to log like so
define('INCLUDE_CHECK',true);
include 'php/functions.php';
logThis('Great Success!');
logThis('Log up');
I have a file called gameReader.php which gets data from a database and formats it into JSON data to be displayed on the site. I am creating this file with the AJAX below
var values = []; // Array holding the values
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
games = JSON.parse(this.responseText);
console.log('Got '+values.length+' games');
for (var i=0; i<games.length; i++){
console.log(values[i]);
}
for(var i=0; i<values.length; i++){
console.log(rootValues[i]+' set.');
rootValues[i]=values[i];
}
};
oReq.open("get", "gameReader.php", true);
oReq.send();
When this code runs gameReader.php is created and runs but it will not log to the file. I'm sure it's something to do with how its being called but I'm not sure where to start.
Any help would be greatly appreciated.
So my question is a bit of a mess so I'll add some more.
The logging function works fine on every page I call it on apart from gameReader.php.
gameReader.php not a page but a php file that collects data from a database messes it around a bit and sends it out as a JSON array.
gameReader.php is the only file to be called with JavaScript
Here are the full files if it helps
index.php The logging script works fine here lines: 2, 3, 126, 127. This is also where the JavaScript is called line 139
functions.js this is the JavaScript that calls gameReader.php (is the JavaScript above)
gameReader.php This is where the problem is when this is run there is no output to log.txt in spite of lines 8, 9 and 10.
Also I'm sure gameReader.php is littered with errors, that's why i need the txt logging. I'm still learning.
fopen("log.txt","a")likefopen("/logs/log.txt","a")then you will know exactly where the file should be locatedlogThisingameReader.php? also since you use a relative path infopen("log.txt"you may havelog.txtin multiple foldersdefine('INCLUDE_CHECK',true);include 'php/functions.php';logThis('gameReader up!');gameReader.phpIt's in the same dir as the other files that call it, so relatively its in the same place. I'v added more info to my question to help.