سؤال

So I figured out how to generate my own PHP error handler and record all errors in a log file - not visible to the user. I even made a second log file, that only records UNIQUE errors.

Now I figured out how I can go about fatal errors, by calling register_shutdown_function('fatal_handler');

My fatal_handler utilized the same functions which my regular error handler uses, so that fatal errors get logged the exact same way.

But here is where things get weird: When I invoke a fatal error, I get all these errors from my error logging functions:

WARNING: file_put_contents(): open_basedir restriction in effect. File(errors.log) is not within the allowed path(s): ...

WARNING: file_put_contents(errors.log): failed to open stream: Operation not permitted

WARNING: fopen(unique_errors.log): failed to open stream: Operation not permitted

And then subsequently any fgets, fclose, and file_put_contents functions also fail with simmilar errors.

Why do these errors occur for the fatal_handler but not for the regular error handler? What do I have to do differently when logging fatal errors?

E D I T :

*Maybe I should mention that the log file I am trying to access resides in a sub folder of on of the paths that the open_base_dir restriction error message lists as an option. So the error doesn't make sense, because it is within an allowed path - I mean after all it works effortless when it's just a NOTICE, rather than a FATAL ERROR.*

So again my question: Why does file_put_contents() work on the regular error handler but not in the fatal error handler?

I assume I have to specify the path to the file differently in the fatal error handler, because events are happening at a different level or something like that maybe...? I really know nothing about php's inner workings... Does anyone have an opinion or idea?

هل كانت مفيدة؟

المحلول 2

found it!

and I thought I already tried it, but I must have been too tired yesterday - because I put a mistake in it.

so one of my accessible directories is:

/home/www/USER_ID/html/

my log file resides in

/home/www/USER_ID/html/MYSITE/SUBFOLDER/errors.log

the file triggering the error is

/home/www/USER_ID/html/MYSITE/index.php and all other files triggering errors are next to this file.

The functions for the error handlers are included in a file in

/home/www/USER_ID/html/MYSITE/SUBFOLDER/errorshandlers.php

for the regular error handler I can specify the log file as

$file = SUBFOLDER/errors.log;

but for the fatal error handler I have to say

$file = /home/www/USER_ID/html/MYSITE/SUBFOLDER/errors.log;

Then it'll work! :-)

نصائح أخرى

Though the PHP executable can access a lot of places by default. Such as the directory where PHP is installed, where the log files reside, the php.ini, the windows temp directory and so on...

You can restrict the access to directories where the scripts that get run can read/write. This is a security measure so even if a certain php script has an exploit, the rest of your computer isn't compromised. This is done with open_basedir.

I think (I'm not sure) you can set multiple directories in open_basedir. But my advise is to not set open_basedir to have access to the directory where php.ini resides. Because in that case you might just as well remove open_basedir.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top