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

zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Redirect on "or die"

Post by zunebuggy »

Instead of or die(mysql_error()); is there a way I can send my or die to a friendly page that tells the user "Something went wrong. Please wait a few minutes and try again."? Can you show me a basic example how to redirect to a MySQL error page like this?

Thank you.
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 »

Code: Select all

$result = mysql_query($query);
if($result === false) {
    header("Location: error.php");
} else { 
    // process query results
}
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 »

Or

Code: Select all

mysql_query('...') or your_header_function('Location: page.php');
I personally have a redirect function that calls die() itself...so if you want to use less code, then you can do that :)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Redirect on "or die"

Post by Darhazer »

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 understand why that makes it a "bad habbit"...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redirect on "or die"

Post by Benjamin »

:arrow: Moved to PHP - Code
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 is one thing, another is that die() has no part in error handling. It might be useful for debugging, but you can debug without making the habit of killing your scripts on every error.
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 »

yeah, I agree that die() shouldn't be used for handling errors. But I don't understand why using "or" is bad...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redirect on "or die"

Post by Benjamin »

jackpf wrote:yeah, I agree that die() shouldn't be used for handling errors. But I don't understand why using "or" is bad...
Can you show an example code snippet where using OR is the best solution?
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 »

Code: Select all

$fh = fopen('somefile.txt') or trigger_error('Could not open this file...', E_USER_ERROR);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redirect on "or die"

Post by Benjamin »

It seems to me that there would be code following that line which would depend on the file handle. The Errors would cascade.

Maybe:

Code: Select all

 
$fh = fopen('somefile.txt') or throw new Exception('Could not open this file...');
 
I don't know, something about it seems "off", but it could be because I have never used OR in that context.
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 »

But the code below wouldn't even be executed since I called trigger_error(), from which I would stop the script from parsing and show the user and error ;)

I honestly don't see anything wrong with it. I think it's a great operator for reducing the amount of space code takes up.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Redirect on "or die"

Post by Darhazer »

Less lines do not mean better code.
The code is better when it's easy to understand it. or is not obvious operator; because of this sometime it is used in boolean expression when || should be used, or as in the posted code in that topic - in the return expression.

Additionally, the "or" error handling does not allow a second line of code, and this is not good. What if fwrite failed and you want to close the handler before throwing exception (I bet the ones who use 'or die' or 'or trigger_error' do not care about opened handles, memory leaks and so on)
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 find it easy to read.

Also, in most cases all you want to do is call your own user defined error function. The only time I ever use it is when I want to execute one function if another returns false. In all other cases I use the boolean "or" (||) operator.

And I doubt having a file handle open for that extra milli-second or however long PHP takes to echo an error message would be too much of a problem...
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 »

personally, I've never seen the OR operator in production code, only in basic tutorials. I can't really think of a good use for it either.

And as I've written previously, you don't want your script dying whenever an error occurs, that's not error handling. So regardless if you throw a fatal error, use die() inline or in a function, that's not good practice. What Darhazer referred to - performing some cleanup before directing to an error page is the common practice.
Post Reply