2

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.

7
  • Where do you expect the file to be written? On the client side or on the server side? PHP will run and therefore log on the server. Commented Jul 2, 2015 at 13:49
  • Try putting a full path in the filename fopen("log.txt","a") like fopen("/logs/log.txt","a") then you will know exactly where the file should be located Commented Jul 2, 2015 at 13:56
  • do you call logThis in gameReader.php ? also since you use a relative path in fopen("log.txt" you may have log.txt in multiple folders Commented Jul 2, 2015 at 13:57
  • The function works fine when it is called in the pages (index.php, tab1.php etc) but when I call it from gameReader.php it doesn't add to the file. gameReader.php is the only file I load with JavaScript. It has these lines in it. define('INCLUDE_CHECK',true); include 'php/functions.php'; logThis('gameReader up!'); Commented Jul 2, 2015 at 15:12
  • @birdspider yes I call it in gameReader.php It'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. Commented Jul 2, 2015 at 16:46

1 Answer 1

1

You seem to have various other php related problems.

Try running with error_reporting(E_ALL); ini_set('display_errors',1),

further use php -l (linter) on your php files.

I the current git version gameReader.php cannot be included at all because of:

Fatal error: Cannot redeclare emptyClass::addladderWinsCoin() in ./gameReader.php on line 87 (1*)

therefor no logThis

(1*): which means you declare the method addladderWinsCoin 2 times, which is not correct

EDIT:

you can use a variant of the following to check php (assuming linux and php-cli is installed)

find . -name '*.php' -exec php -nl {} \;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot these 2 lines have changed everything error_reporting(E_ALL); ini_set('display_errors',1). I can now see where things are going wrong. I suppose I should of asked how to properly use the error_log, thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.