Hi coding gurus! I'm trying to create a method of including a PHP script to my files that allows me to handle what happens when something goes wrong with the inclusion. Blank pages when something goes wrong is lame!
Not really necessary; It's an experiment more than a problem, I like the idea of covering everything I can. I also make thing harder on myself than I need to, because that seems to be a great way to learn things.
Let's say I put this code at the top of my files:
PHP Code:
if(include 'alpha.php')
{ echo 'Success'; } // In practical use I would initialize some objects or something.
else { die('Could not include vital script.'); }
So far it works like a require, but allowing me to say something when it doesn't work. However, I wanted to send the user something a bit more comprehensive and useful, than a little string.
I didn't want to do something like this either:
PHP Code:
if(include 'alpha.php')
{ echo 'Success'; }
else { die('<span style=\"ect\"> Formatted message. </span>'); }
In real-world use the above would be a big mess of code, as well as being just awful if I ever wanted to change the error message at all (As would the first solution, for that matter).
Hence, my ideal result would be to trigger a HTTP 500 error page, which as well as having all the information the user needs to know (Even if it's not technically a server error? I don't know.), is also set up to record the error and give the user some useful options (Go to home-page, retry page, ect). It didn't have to be a HTTP 500, I'm just using it for the purposes of trying this.
My first response was something to the effect of: (Error link for example purposes only)
PHP Code:
if(include 'alpha.php')
{ echo 'Success'; }
else { header('error.php?id=500&cause=include_failed'); }
This returns three warnings:
Warning: include(alpha.php) [function.include]: failed to open stream: No such file or directory in..
Warning: include() [function.include]: Failed opening 'alpha.php' for inclusion..
Warning: Cannot modify header information - headers already sent by.. (The include line)
Note: The file doesn't exists there, I'm testing the else by including a non-existent file. So basically it's just headers already sent by the include, which wasn't totally unexpected. (Not that I thought very hard when the headers would be sent here).
However, by accident I discovered this produces an error 500 if the file comes back false:
PHP Code:
if(@include 'alpha.php') // Notice the error silencer.
{ echo 'Success'; }
else { header('/'); }
Doesn't matter what the header link is, as long as a value exists the presence of the header call causes the HTTP 500. I also tried this with "display_errors" set to off in my php.ini (Yes, it's only on for development), and it produced a HTTP 500 even without the @ silencer.
It does make sense in a way, but not all errors send a HTTP 500 when silenced! If that were so I could be doing this simply with a
@require — I don't understand why these methods are behaving differently. Perhaps, fatal errors can't return a HTTP error, but others will? I would like to understand this behavior, if anyone knows!
One other issue I noticed with what I was trying to do, is when the include works but the script has errors, which was the other thing I wanted to catch. It will slip right through this check..
Consider I make
alpha.php this:
PHP Code:
<?php undefinedFunction(); ?>
Now the script will return a fatal error, but will also return true as being included. Since that is all
if(include 'alpha.php') checks for, I might as well be using a
file_exists or something. How can I catch when my include script is producing errors without hard-coding return false type things into every possible instance? Perhaps I could check for a value that might be hidden if errors choke up the result (I can see that working for fatal errors, but other errors getting past..), or wrap everything in alpha.php with some sort of error catch that returns false or true?
I thought it was time to see what other people knew about the subject. I have little experience. Please no "Your idea is terrible, just trust your includes to always work"; That's already my fall-back, I just want to be clever. I'm open to different approaches too.