How would I go about changing where PHP logs it's syntax errors? I've got multiple people working on the same server, and I want to be able to send my own logs to a custom file so that I'm not looking at every one else's errors.
I can do this:
error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log','path/to/mylogfile.log');
And that works for some errors, but for actual syntax errors, the logs are still being sent to the master /var/log/httpd/error_log. How do I make the syntax errors get sent to my custom log file?
Since the file cannot be correctly parsed, the ini_set function is not executed either and neither is the new error log set. You need to set php_value error_log /path/to/myfile.log in an .htaccess file or the global server config (I'm assuming that you're using Apache).
Given that syntax errors prevent PHP from running you will not be able to set log paths via PHP. It's a chicken before the egg situation.
Alternatives would be:
Change the error log at the webserver level
Enable output of error messages to the browser
I'd suggest the latter.
Related
I am having an issue when I have a php application that is returning an internal server error (500) however nothing is showing up in the error log.
Now I know there are error with what I am trying to run, I know I have missing some files and what not but something should show in the apache error log (otherwise how are I supposed to know exactly what I am missing).
I created a test script is errors it in under the same vhost configuration and those error show up fine so everything seems configured right as far as php/apache. Are there certain php errors that does show up in the error log (php is configure to display any type of notice, warning, , error, fatal error, etc...)?
This is running on ubunut 10.04 with the standard apache and php from the ubuntu repo with apt-get.
Scan your source files to find #.
From php documentation site
Currently the "#" error-control operator prefix will even disable
error reporting for critical errors that will terminate script
execution. Among other things, this means that if you use "#" to
suppress errors from a certain function and either it isn't available
or has been mistyped, the script will die right there with no
indication as to why.
Copy and paste the following into a new .htaccess file and place it on your website's root folder :
php_flag display_errors on
php_flag display_startup_errors on
Errors will be shown directly in your page.
That's the best way to debug quickly but don't use it for long time because it could be a security breach.
If you still have 500 error and no logs you can try to execute from command line:
php -f file.php
it will not work exactly like in a browser (from server) but if there is syntax error in your code, you will see error message in console.
Maybe something turns off error output. (I understand that you are trying to say that other scripts properly output their errors to the errorlog?)
You could start debugging the script by determining where it exits the script (start by adding a echo 1; exit; to the first line of the script and checking whether the browser outputs 1 and then move that line down).
In the past, I had no error logs in two cases:
The user under which Apache was running had no permissions to modify php_error_log file.
Error 500 occurred because of bad configuration of .htaccess, for example wrong rewrite module settings. In this situation errors are logged to Apache error_log file.
For Symfony projects, be sure to check files in the project'es app/logs
More details available on this post :
How to debug 500 Error in Symfony 2
Btw, other frameworks or CMS share this kind of behaviour.
Here is another reason why errors might not be visible:
I had the same issue. In my case, I had copied the source from a production environment. Hence the ENVIRONMENT variable defined in index.php was set to 'production'. This caused error_reporting to be set to 0 (no logging). Just set it to 'development' and you should start seeing error messages in apache log.
Turned out the 500 was due to a semi colon missing in database config :-)
Another case which happened to me, is I did a CURL to some of my pages, and got internal server error and nothing was in the apache logs, even when I enabled all error reporting.
My problem was that in the CURL I set
curl_setopt($CR, CURLOPT_FAILONERROR, true);
Which then didn't show me my error, though there was one, this happened because the error was on a framework level and not a PHP one, so it didn't appear in the logs.
You need to enable the PHP error log.
This is due to some random glitch in the web server when you have a php error, it throws a 500 internal error (i have the same issue).
If you look in the PHP error log, you should find your solution.
see here in the doc of how to enable it in the php.ini
Be sure your file permissions are correct. If apache doesn't have permission to read the file then it can't write to the log.
What happened for me when this was an issue, was that the site had used too much memory, so I'm guessing that it couldn't write to an error log or displayed the error. For clarity, it was a Wordpress site that did this. Upping the memory limit on the server showed the site again.
SOLVED
I struggled with this and later on, I realized that I was working on PHP 5.6, so I upgraded to PHP 7.0, then I released there were comments placed by git for conflicting codes. I found something like this in my code <<<<<<<< But solved it.
I have an Apache2 web server with PHP 5.5 installed.
My default PHP settings is display_error = 0 (I don't need globally displayed errors) but I need it on in specific PHP files.
I tried with:
error_reporting(E_ALL);
ini_set("display_errors", 1);
and it's not working.
Can someone tell me how can I make it show errors in specific PHP files?
i am try to force some error writing some no syntax logic and not showing error...
To show parse errors in PHP you have to put this on your php.ini
display_errors = on
My advice is to avoid displaying errors on production servers but log them all. So you can later inspect and fix bugs from yoursite-error.log file.
You should be concerned if your applications has warnings, errors etc. IMHO it is a bad idea to focus your attention only on few files instead of them all.
Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.
Is there a PHP function or some other way of obtaining the PHP error log as a string?
I need this because I cannot access the error log of a site I am running on someone else's server. - He offered to email me the error log, but that isn't exactly convenient.
Is there some way I could output the error log to a PHP page?
I realize that viewing the entire server's error log is not really going to happen for me. However, I know you can do something like this to email a manual error_log call to yourself:
error_log('A really bad error', 3, 'me#myemail.com');
Is it possible to configure a page to email errors to you instead of displaying them?
On a badly secured server, yes. But on most servers there are two users: apache and [ you ]. You don't have access to the server logs, since they are owned by the apache user (or whichever server you're using).
However, you could probably try it:
echo file_get_contents('/var/log/httpd/error_log');
Note: that's the default location on a RedHat-based apache server. It may be different
Update To reflect the updated question
No, you cannot view the error log with error_log - it is a one-way process that gets handled by the webserver. It only writes the log, but you cannot read it.
You can probably display the errors with this:
ini_set('display_errors', 'On');
error_reporting(E_ALL);
You could even use set_error_handler to handle all warnings and notices (for example, to mail them). But that's pretty much all you can do.
I am having an issue when I have a php application that is returning an internal server error (500) however nothing is showing up in the error log.
Now I know there are error with what I am trying to run, I know I have missing some files and what not but something should show in the apache error log (otherwise how are I supposed to know exactly what I am missing).
I created a test script is errors it in under the same vhost configuration and those error show up fine so everything seems configured right as far as php/apache. Are there certain php errors that does show up in the error log (php is configure to display any type of notice, warning, , error, fatal error, etc...)?
This is running on ubunut 10.04 with the standard apache and php from the ubuntu repo with apt-get.
Scan your source files to find #.
From php documentation site
Currently the "#" error-control operator prefix will even disable
error reporting for critical errors that will terminate script
execution. Among other things, this means that if you use "#" to
suppress errors from a certain function and either it isn't available
or has been mistyped, the script will die right there with no
indication as to why.
Copy and paste the following into a new .htaccess file and place it on your website's root folder :
php_flag display_errors on
php_flag display_startup_errors on
Errors will be shown directly in your page.
That's the best way to debug quickly but don't use it for long time because it could be a security breach.
If you still have 500 error and no logs you can try to execute from command line:
php -f file.php
it will not work exactly like in a browser (from server) but if there is syntax error in your code, you will see error message in console.
Maybe something turns off error output. (I understand that you are trying to say that other scripts properly output their errors to the errorlog?)
You could start debugging the script by determining where it exits the script (start by adding a echo 1; exit; to the first line of the script and checking whether the browser outputs 1 and then move that line down).
In the past, I had no error logs in two cases:
The user under which Apache was running had no permissions to modify php_error_log file.
Error 500 occurred because of bad configuration of .htaccess, for example wrong rewrite module settings. In this situation errors are logged to Apache error_log file.
For Symfony projects, be sure to check files in the project'es app/logs
More details available on this post :
How to debug 500 Error in Symfony 2
Btw, other frameworks or CMS share this kind of behaviour.
Here is another reason why errors might not be visible:
I had the same issue. In my case, I had copied the source from a production environment. Hence the ENVIRONMENT variable defined in index.php was set to 'production'. This caused error_reporting to be set to 0 (no logging). Just set it to 'development' and you should start seeing error messages in apache log.
Turned out the 500 was due to a semi colon missing in database config :-)
Another case which happened to me, is I did a CURL to some of my pages, and got internal server error and nothing was in the apache logs, even when I enabled all error reporting.
My problem was that in the CURL I set
curl_setopt($CR, CURLOPT_FAILONERROR, true);
Which then didn't show me my error, though there was one, this happened because the error was on a framework level and not a PHP one, so it didn't appear in the logs.
You need to enable the PHP error log.
This is due to some random glitch in the web server when you have a php error, it throws a 500 internal error (i have the same issue).
If you look in the PHP error log, you should find your solution.
see here in the doc of how to enable it in the php.ini
Be sure your file permissions are correct. If apache doesn't have permission to read the file then it can't write to the log.
What happened for me when this was an issue, was that the site had used too much memory, so I'm guessing that it couldn't write to an error log or displayed the error. For clarity, it was a Wordpress site that did this. Upping the memory limit on the server showed the site again.
SOLVED
I struggled with this and later on, I realized that I was working on PHP 5.6, so I upgraded to PHP 7.0, then I released there were comments placed by git for conflicting codes. I found something like this in my code <<<<<<<< But solved it.
I want to chceck files before I'll connect domain on my server. I've added them in direct admin, I've uploaded files - but I don't know how to get to them.
I have 4 domains connected, but i have acces only to one. I saw that it's possible to include file from higher level
include('../../panele-podlogowe.eu/adm/img/edit.gif')
That gave me correct image. But when I've done:
include('../../panele-podlogowe.eu/index.php');
Nothing happens (I've turned Error reporting on).
When I did something like:
include('../../panele-podlogowe.eu/index.php');
echo 'Failed?';
Nothing prints out... What the heck?
Solution:
ini_set("display_errors", "stdout");
Review the PHP error log. That usually, even under default settings, shows what's the problem.
Enable error reporting so php will print what is wrong. Add this at the top of your file.
error_reporting(E_ALL);
Otherwise:
Check the access permissions for that file
Double-check the file you are including for syntax errors. (if the include causes php to crash/segfault you might not get any output)