1

Is there a way of echoing syntax errors and other php errors that occur during execution of a script through json?.

Say i have a log in form that submits data through JQuery ajax. By post method.

In my php file i get the values like so $_POST['username']; $_POST['password'];. Then check if the details are correct and echo a json encoded success message or error message if the details are wrong. To that point everything works out fine.

Now if i have a syntax error or any kind of error from php say i did not include a needed file properly in its collect path i do not get any warning : or fatal error: that i would get displayed on my page if do not use json to send data back to my page. This makes debugging hard. I have tried to use try{//code}catch (EXEPTION $e) then including the $e->message(); as part of the json encoded message am returning to my page but it still am am not able to echo the errors. Can someone please help me solve this?

  • Possible duplicate of [Detect bad json data in PHP json\_decode()?](https://stackoverflow.com/questions/2348152/detect-bad-json-data-in-php-json-decode) – Ryosaku Jul 29 '17 at 19:14
  • I looked at that question and its not solving my problem..i need to echo syntax errors,warnings and fatal errors – pickman murimi Jul 29 '17 at 19:15
  • When your development-server is on linux, just run `tail -f /var/log/httpd/error_log` or `tail -f /var/log/apache2/error_log` in a seperate window. Saves you the trouble of writing all these extra code for error-reporting for every single AJAX-call – Peter van der Wal Jul 29 '17 at 19:23
  • @peter van der wal sad am not on linux. Am running apache on windows is there an equal to that on windows? – pickman murimi Jul 29 '17 at 19:26
  • Open PowerShell (included in Windows) and run `Get-Content C:\Path\To\error.log -Last 50 -Wait` – Peter van der Wal Jul 29 '17 at 19:30
  • Am using xammp.. And am getting 'get-content ' is not recognized as a command – pickman murimi Jul 29 '17 at 19:33
  • PowerShell not the old fashioned command prompt, see [the difference](https://stackoverflow.com/a/30888953/2903251). – Peter van der Wal Jul 29 '17 at 19:43
  • @Peter van der wal.. I have correctly configured my power shell i was making some error.. Thenks alot.. Your way works and its simple... If you dont mind add it as an answer to this question – pickman murimi Jul 29 '17 at 20:02

1 Answers1

0

Instead of trying to log PHP-errors to JavaScript and output them there, it is much easier to watch the error-log. You can open it in a seperate window, and have it automatically output any new errors appended to the log-file.

On Linux or Mac open a terminal and run

tail -f /var/log/apache2/error.log
// Or
tail -f /var/log/httpd/error_log
// Depending on your installation, error log may be in a different location

On Windows, open PowerShell (included default in Windows) and run

Get-Content C:\Path\To\error.log -Last 10 -Wait
// Location of the log-file is highly dependent on the used installation

Note that on a production-environment you are encouraged to hide all PHP-error-information to the browser at all (only to the error.log, at most tell your visitor 'Oops, something went wrong'), so writing this extra code for error-handling would be just for the sake of development.

Peter van der Wal
  • 10,558
  • 2
  • 20
  • 29