I’m working in a php plugin for a php code generator. The thing is that I need to redirect a page to another when a php compiler error occurs.
For instance, when a “PHP syntax error” occurs I want to redirect that page to another and show part of the code (5 lines above, 5 lines bellow) where the error is located. By now, I have the last part but I can’t find the way to do the redirection task.
I can’t redirect using php because with this I’ll be assuming that there is no error on the code and was successfully compiled. I want to catch PHP parse error. This kind of errors only produce some output on stderr.
I’ll take a look on “core - Apache HTTP”
Syntax errors are not of catchable by an user error handler (thing that your question reveals you want, in a very naive way)
Im sorry but cant do that, this is a limitation of PHP.
Another possibility is to use eval to parse your code - that builds a “frame” around your code and you can just check the return code of eval: PHP: eval - Manual
Yes, actually I was trying with eval() function but is quite slow and if no error, runs the code and can produce critical situations, not a solution for me.
Finally I change the default log file of the apache php.ini and every time an error occurs, it is logged and updates a page that runs a php script that parses this log obtaining the file path, error line and error message. Using that path it opens the file and read 5 lines above and bellow of the error line and finally highlight the code to show on screen.
I can just strongly recommened you to use the set_error_handler function instead - that makes your applications portable and much more useful. Setting the log of apache to something different is IMO just a “workaround” - especially when the functions available through the PHP API do the same!
Yes.
I want to catch syntax errors, not common errors. I agree with the fact that my solution isn’t the best, but for now, it works fine.
I Still looking for some better way.
I would then recommened you to make a sub-request to the apache server, calling your php script which will execute the custom php-script (use fopen, readfile whatever for it, maybe you can also give PHP: virtual - Manual a chance, but for that you will have to use something like ob_start to fetch the output).
You can then parse the output of the sub-request to detect syntax-errors too - which will then even give you the info where the error happened!