Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: SQL error VERY BASIC problem

  1. #11
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Personally I feel that it is tantamount to treason to use die() in a commercial environment unless it's for external access in a bandwidth limited situation.

    What I mean by that is that while it's almost invariably better to dynamically create or redirect to an error page rather than just executing die() when things go badly wrong, if you are using a PHP script to output data from, for instance, a database to a mobile device app with limited bandwidth - then it is often far more efficient to error handle the blank return data rather than cramming a whole error page down a bandwidth limited line...

    Just my two cents!

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Interrupting a page while it loads and giving an error message of any kind, especially with die() stopping any other content from loading is of course bad.

    But there are two cases where this is acceptable:
    1. If there is no legitimate reason for the user to be in a location (eg, hidden admin page), then all you're doing is annoying hackers. Of course it shouldn't be something a legitimate user could click by accident. For security, nothing is better than die().
    2. If you use it at the right part of the page and have a redirect.

    And maybe a third would involve a case where an endless loop is stopped using die() as a last resort. Any alternative is better than this, but I can imagine a situation where programming such an alternative would be difficult.


    The habit of tutorials to suggest using or die('Error message'); is a bad one, but the function is not useless.



    There's one other case when I've used die() that is a little complicated, but very useful if it happens to apply in your situation. If you're using OOP, objects can have a "destruct" function that is automatically activated when the script ends (unless you intentionally unset the objects earlier). This can be very useful. For example, I've used this for templates so that it automatically adds the </html> tag (along with other things, like Javascript that goes at the end of the page). The only problem with this is because it's something that is set early (by creating such an object) and not controlled at the end of the page, if you do need an exception (for example, skip those Javascripts, and manually use only the </html> tag), then you can use die() to stop those things from executing automatically-- die() stops execution immediately so nothing else is processed.


    The real problem with die() is that it creates broken pages (skipping lots of close tags for example). But that's a usage error, not a problem with the function itself.
    Last edited by djr33; 02-08-2012 at 05:04 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Also (@superdog):
    Bandwidth absolutely does not need to be an issue. How many bytes are needed, realistically, to say 'sorry, there was a problem?

  4. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think the idea was that if your account has 0 bandwidth left, then using die() to disable any output could stop extra charges. But two problems with that are 1) it's hard to know when that's the case (within PHP automatically), and 2) I think the requests count as bandwidth, even though they're very small.
    I can't ever imagine being in that situation, though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •