springthorpeSoftware
asked on
500 Errors on PHP parse error.
Since last week, our server has started showing 500 errors when there is a PHP parse error instead of logging those errors. Host claims no changes to Windows Server or IIS and server had not been rebooted since well before the problem showed up. We tried rebooting the server, but still does the same thing. No changes to PHP installation (7.1.1) or to PHP.ini.
In testing, other PHP errors, such as calls to missing function and errors triggered manually in the code, are being logged correctly.
This is making it impossible to debug the PHP code so we need to get this fixed ASAP. Has anyone seen something like this? Does anyone know of a fix?
Log in via ftp and navigate to the plugins folder. Rename the plugins folder to plugin, or something. Then go to the admin to login. Once logged in, go back to the ftp and change the name back to plugins. Go back to the admin and turn on your plugins one by one and update them as you go until you find your culprit.
you are providing no information other than an error.
You have to have one URL to which you go and where you get this message. check the PHP script that is returning the error.
check as was suggested the logs
create a simple testme.php
and see if you get an error or information..
are you running a wordpressm or other CMS type items?
if you have individual PHP pages, not all should be eroring out, unless all are including a file that may have gotten corrupted.
You have to have one URL to which you go and where you get this message. check the PHP script that is returning the error.
check as was suggested the logs
create a simple testme.php
<?php
phppinfo();
?>
and see if you get an error or information..
are you running a wordpressm or other CMS type items?
if you have individual PHP pages, not all should be eroring out, unless all are including a file that may have gotten corrupted.
ASKER
More on this...
If the PHP parsing error is in an include file, the error is posted normally.
If it's in the page code, the 500 Error results, but only when that section of code is executed. For example, there was a misspelled field name in the "data validation before saving" part of the page. The page loaded normally, but threw 500 error when Save was clicked.
Robert - To my knowledge, there are no plug-in files for PHP. Are you referring to something else?
Arnold - No wordpress.
Bruce
If the PHP parsing error is in an include file, the error is posted normally.
If it's in the page code, the 500 Error results, but only when that section of code is executed. For example, there was a misspelled field name in the "data validation before saving" part of the page. The page loaded normally, but threw 500 error when Save was clicked.
Robert - To my knowledge, there are no plug-in files for PHP. Are you referring to something else?
Arnold - No wordpress.
Bruce
As far as the error logging goes, it's pretty much impossible for PHP to simply change its logging behavior without ANY changes, so -something- changed.
As far as the parse error goes, there are a few possibilities here.
1. You're evaluating/executing some dynamic PHP code (e.g. the actual PHP code executed partially or wholly comes from some source other than a file, like pulling a PHP code snippet from a database and running eval() on it) and that dynamic PHP code is failing. This would be a pretty rare case, but possible.
2. You have a PHP file that got corrupted somehow. For example, maybe you use source control to update your PHP files and there was a conflict that could not be automatically merged/resolved, so the PHP code file is in a state where it shows the before/after lines and it's up to you to manually edit and resolve them.
3. New code got added or was automatically included somehow (e.g. a script that executes all files in a given dir), like a plugin or something.
Usually a parse error will tell you which file has the problem. If it doesn't, then the easiest way to check is just to run a PHP lint check on each file. You go to the command line and execute "php -l <path to the PHP file>" (that parameter is a lowercase L for "lint"), which will just do a syntax check on that particular file and tell you if there are any parsing / syntax errors but will not actually execute the script.
If you have a lot of files to check, you can either dump a list of PHP files into a file and use a batch script to loop through them and run the lint check on each one, or if PHP has access to the binary, you can run a script like this one :
Just replace
checkForParseErrors(".", "/usr/bin/php")
with
checkForParseErrors("C:/Path/To/Your/Document/Root", "C:/Path/To/PHP/Executable/php.exe");
It should loop through all the PHP scripts and any scripts in subfolders, and run the lint check and show you the output. This can take a while if you have a lot of scripts.
As far as the parse error goes, there are a few possibilities here.
1. You're evaluating/executing some dynamic PHP code (e.g. the actual PHP code executed partially or wholly comes from some source other than a file, like pulling a PHP code snippet from a database and running eval() on it) and that dynamic PHP code is failing. This would be a pretty rare case, but possible.
2. You have a PHP file that got corrupted somehow. For example, maybe you use source control to update your PHP files and there was a conflict that could not be automatically merged/resolved, so the PHP code file is in a state where it shows the before/after lines and it's up to you to manually edit and resolve them.
3. New code got added or was automatically included somehow (e.g. a script that executes all files in a given dir), like a plugin or something.
Usually a parse error will tell you which file has the problem. If it doesn't, then the easiest way to check is just to run a PHP lint check on each file. You go to the command line and execute "php -l <path to the PHP file>" (that parameter is a lowercase L for "lint"), which will just do a syntax check on that particular file and tell you if there are any parsing / syntax errors but will not actually execute the script.
If you have a lot of files to check, you can either dump a list of PHP files into a file and use a batch script to loop through them and run the lint check on each one, or if PHP has access to the binary, you can run a script like this one :
<?php
set_time_limit(0);
checkForParseErrors(".", "/usr/bin/php");
function checkForParseErrors($dir, $php)
{
chdir($dir);
$subdirs = array();
$dh = opendir($dir);
while(($entry = readdir($dh)) !== false)
{
// Skip . and .. dirs
if(($entry == ".") || ($entry == "..")) { continue; }
// Make note of subdirs to recurse into
if(is_dir($entry)) { $subdirs[] = "{$dir}/{$entry}"; continue; }
// For .php files, run a PHP lint check
if(substr($entry,-4) == ".php")
{
echo "Checking {$dir}/{$entry}: ";
$result = shell_exec($php . " -l {$dir}/{$entry}");
if(strpos($result,"No syntax errors detected") === 0)
{
echo "OK\n";
}
else
{
echo $result . "\n";
}
}
}
closedir($dh);
// Process any subdirs
foreach($subdirs as $subdir)
{
checkForParseErrors($subdir, $php);
}
}
Just replace
checkForParseErrors(".", "/usr/bin/php")
with
checkForParseErrors("C:/Path/To/Your/Document/Root", "C:/Path/To/PHP/Executable/php.exe");
It should loop through all the PHP scripts and any scripts in subfolders, and run the lint check and show you the output. This can take a while if you have a lot of scripts.
In short, 500 is issued by IIS in your case when the response being output from script is not correct.
depending on how you access the page and what additional information is displayed in the browser beyond the fault.
Enabling the debug option, in PHP or within IIS, web.config could shed light on which file, which line which item is causing the fault or at least which item was evaluated before the error occurred.
if it fails on every single page, you have to determine what is common between/among these pages.
is this a project developed using an IDE Zend, Netbeans, etc.
teh IDE commonly presents an option to test it out, see if it works there.
depending on how you access the page and what additional information is displayed in the browser beyond the fault.
Enabling the debug option, in PHP or within IIS, web.config could shed light on which file, which line which item is causing the fault or at least which item was evaluated before the error occurred.
if it fails on every single page, you have to determine what is common between/among these pages.
is this a project developed using an IDE Zend, Netbeans, etc.
teh IDE commonly presents an option to test it out, see if it works there.
ASKER
Arnold:
Before this week, a PHP parsing error resulted in a 200 Error and the error was logged by PHP.
Will ask host about web.config.
Not happening on every page.
Not using an IDE Zend, Netbeans, etc.
As a review:
1. If misspelled field name, 500 error only when bad code is actually executed.
2. If it's a syntax error within the page, 500 results on load.
3. Errors are properly logged for missing/misnamed functions and when triggered manually.
Bruce
Before this week, a PHP parsing error resulted in a 200 Error and the error was logged by PHP.
Will ask host about web.config.
Not happening on every page.
Not using an IDE Zend, Netbeans, etc.
As a review:
1. If misspelled field name, 500 error only when bad code is actually executed.
2. If it's a syntax error within the page, 500 results on load.
3. Errors are properly logged for missing/misnamed functions and when triggered manually.
Bruce
Webhost updated IIS,php system wide??
Do you have a local version of the site setup that you can test?
Is this a shared hosting, or a VPS?
See if you can add error reporting to get a better idea on what or where the issue is
https://www.w3schools.com/Php/func_error_reporting.asp
When the 200 you were getting , were the errors written out?
an update/security fix might be blocking PHP from recording the error in the log.....
Do you have a local version of the site setup that you can test?
Is this a shared hosting, or a VPS?
See if you can add error reporting to get a better idea on what or where the issue is
https://www.w3schools.com/Php/func_error_reporting.asp
When the 200 you were getting , were the errors written out?
an update/security fix might be blocking PHP from recording the error in the log.....
Since you're using custom code (non-WordPress or non-other-CMS), then it's up to you to setup your logging yourself.
You'll have to arrange for all diagnostics to emit, which will likely default into Apache's error.log or equivalent... however you've setup Apache/PHP under Windows.
The error.log entry will tell you the file path + line number triggering the Fatal, then you just... fix the error...
You'll have to arrange for all diagnostics to emit, which will likely default into Apache's error.log or equivalent... however you've setup Apache/PHP under Windows.
The error.log entry will tell you the file path + line number triggering the Fatal, then you just... fix the error...
Have you checked the display_errors setting in the PHP INI file?
Is the error_log setting in PHP INI configured to a valid path?
Is the error_log setting in PHP INI configured to a valid path?
ASKER
To All -
Thank you for all of the feedback! We looked at each suggestion to see if was the cause.
The most common reply regarded error reporting being turned on in PHP. It was, but no errors were being logged.
Solution was found!
Somehow (no explanation provided), our host had changed the Windows folders privileges on the folder where the log was written to not allow writing by web users. When changed back, the errors were reported properly and the 500 Error disappeared.
To gr8gonzo - The error was in a snippet run by eval(). That suggestion was very helpful.
Bruce
Thank you for all of the feedback! We looked at each suggestion to see if was the cause.
The most common reply regarded error reporting being turned on in PHP. It was, but no errors were being logged.
Solution was found!
Somehow (no explanation provided), our host had changed the Windows folders privileges on the folder where the log was written to not allow writing by web users. When changed back, the errors were reported properly and the 500 Error disappeared.
To gr8gonzo - The error was in a snippet run by eval(). That suggestion was very helpful.
Bruce
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.