Redirect on "or die"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Redirect on "or die"

Post by jackpf »

I've just posted a use for it.

And I don't redirect the user either. I call trigger_error() which loads up the error template and displays an error message. I personally think that's a better way of handling errors than redirecting, since you can preserve post data and whatnot.

I still think you guys are wrong... :P
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Redirect on "or die"

Post by Eran »

you've posted the tutorial "error then die" use which is not useful in a production environment.
If you're referring to apache error template, that's not error handling and should be a last resort. There could multiple sources of failure and you want to customize the error page you show the user according to the source (registered user / administrator / anonymous visitor etc.) as well as the cleanup process.
Part of the cleanup is preserving environment data (such as POST data) and logging it silently. Having the webserver redirect automatically on error is the same as redirecting manually so there is no difference. In either case, you mostly wouldn't the user to be able to refresh and cause the same error again.
I still think you guys are wrong...
Since all of us devil's advocates are thinking the same, and seeing as we are all experienced developers, you'll have to be a little more detailed in your reasoning to convince us..
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Redirect on "or die"

Post by jackpf »

I don't mean apache's error template. I mean a template I have specifically created for errors.

Here, here's an example:

Code: Select all

fopen('somefile.txt') or trigger_error('The file "somefile.txt" cannot be opened...');
 
//and then in my config or whatever
 
set_error_handler('error_function');
 
function error_function($err_no, $err_msg, $err_file, $err_line)
{
    if(is_in_debug_mode)
    {
        //just display the error...
    }
    else if($err_no == E_USER_WARNING)
    {
        //load up the template
        $tpl = new template('error');
 
        $tpl->assign_vars(array(
        'error_message' => $err_msg
        ));
 
        echo $tpl->compile('error_message_function');
 
        return exit(-1);
    }
}
This is just a simple example, but is basically...basically how I handle errors.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Redirect on "or die"

Post by Eran »

That's right, it's a simple example. While the error handling is not bad, there is no clean up done in this example. Any time you need something beyond simply showing an error page (which is, most of the time), the OR operator is not an option. The clean-up is case-specific usually, and can't be handled by a generic error function.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Redirect on "or die"

Post by jackpf »

True...but the entire script ends like 0.000001 seconds after the "or" statement...so I doubt any unclosed resources are going to cause too much of a memory leak...

But yeah, I see your point. In most cases though, yeah, I only want to display an error message...so the "or" operator is fine. For cases where I need to make sure the function didn't fail in order to process something, I'll assign its value to a variable and check it evaluates to true...probably like you do?

But yeah, I see your point.
Post Reply