Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Feb 2006
    Location
    Arizona, USA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Better way to stop script then DIE()?

    Hi guys, I'd like to know what you do when you have to stop a script, except without using die().

    As you know, as soon as php parses a die(), nothing else happens on that page. For most pages this may be fine. But on some pages, you still need to have, say, a sidebar and foot displayed. But if the script dies before the footer is displayed, it won't show up, thus leaving your page half-rendered.

    The only way I know to get around this is to wrap ALL your code in wide IF statements such as:

    if (condition) {

    LOTS of page code

    } else {

    your error

    }
    In this code, because I don't use a die statement, the rest of the page renders beyond my error message.

    Even this is acceptable for some pages, smaller pages with not much code.
    But then, in larger pages, that have multiple conditional statements strewn about, keeping track of 3, 4 or 10 of these huge conditions becomes a huge pain. So lets say your page is made up of:

    -------------------
    header
    -------------------

    body

    -------------------
    footer
    -------------------

    You cannot have any DIE statements within the header or body because the body or footer won't display. And also, through this page you may have as many as 5 or more error conditions in which case you want to display an error, and then render the rest of the page.

    What kind of strategy do you employ for error management like this? In a theoretical situation, lets say you have 3 mysql calls in various parts of the page, but if the call fails, you need to stop with an error. The usual syntax is:

    mysql_query("find cool stuff") or die("There was an error getting stuff!");

    What would you do instead?

    Thanks.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I'm no PHP expert, but I'd say the general lesson here is to avoid procedural code. If you write scripts as objects, then you have the flexibility to use return statements to halt the execution of the code at any point in time.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, you could use OO to abstract it,(ah, brothercake just beat me to it) or, depending on the error you could redirect the use to another page which tells the user what the error was (pass an error number in the get string).

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    trigger_error()
    try, throw and catch - exceptions (PHP5 only)
    return (functions/methods)
    register_shutdown_function()
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    if you want the entire page to load, then just echo out your error messages....

    but if you eliminate the errors in your code, this would not be a problem...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I know there is an exit() but Im not sure what it does... maybe it ends the entire page? or maybe it just ends the php. Not sure...




    ILLINI
    Why not thank me?

    http://adamsworld.name

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Exit() is an alais of die().

  • #8
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts
    ohh, ok. Like I said i wasnt sure what it was



    ILLINI
    Why not thank me?

    http://adamsworld.name

  • #9
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by chump2877
    but if you eliminate the errors in your code, this would not be a problem...
    Not all errors come from the code.

  • #10
    New Coder
    Join Date
    Feb 2006
    Location
    Arizona, USA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, not all errors come from code. Let me give you an example in point.

    This page is a simple page for users to answer a question form.
    There are many checks such as:

    are they logged in? If not, stop
    have they already answered this form? if so, stop
    are there various other conditions? if so, stop

    During these checks, there are maybe 5 or more mysql calls. Simply because I want good programming form, I make sure each call has a die() in case the call fails for whatever reason, so I can handle the error rather then an ugly php error.

    I hear what you're saying about object oriented code and classes. While that sounds really good. Wouldn't it be similar like:

    Run functionname() or die("function failed")
    ??

    Note that most "functions" would also output text in themselves. So I'm not sure exactly how to go about this. Because my code would end up looking like this:

    run function 1 or stop somehow
    run function 2 or stop somehow
    run function 3 or stop somehow

    So then on any particular error, I would have to prevent other function calls from running?

    Maybe I'm just making this overly complicated

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    ...so I can handle the error...
    but die() is not handling the error , its over-reacting

    Procedural or OO is not the point (you can call die() from within a class method as well) , die() is great for development but for production you should catch and handle the errors , I agree if's are an ugly solution to this and some form of abstraction (functions or classes) help.

    The main point however is that there should be no reason to die() anywhere in production code anyway, you can predict where and when an error is likely to occur and handle it, in worst cases (like db connection not working or catching malicious code injecton attempts) a simple header() to a sorry page should suffice. (OK I sometimes use die() with a nasty message for code injections )

    The only fatal cases I can think of are file-read/write operations failing or basic SQL errors etc all of which should be cleaned up during development.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #12
    New Coder dragon's Avatar
    Join Date
    May 2003
    Location
    Florida
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about something like this:

    PHP Code:
    echo $header;

    switch(
    $action)
    {
      case(
    'login'):
        if(isset(
    $all_needed_variables))
           
    login($all_needed_variables);
        else
           
    bad_login();
        break;
      case(
    'show_item')
        if(isset(
    $all_needed_variables))
           
    show_item($all_needed_variables);
        else
           
    request_item_to_show();
        break;
      default:
        echo 
    $all_popular_stuff;
    }

    echo 
    $footer

  • #13
    New Coder
    Join Date
    Feb 2006
    Location
    Arizona, USA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I guess maybe I'm not giving all the details.

    The case statement would be good, but under certain cases I would even need multiple cases to run, not just any particular one.
    I will do some research and think about the suggestions thus far.

    Remember that my code is just an "included" page. Meaning the header, footer, navigation and everything is outside my own little script. This leaves using header:location out. As it would return "headers already sent by...".

    Anyhow, I'll write back here with whatever I think up.
    And yes I agree that all code can be "cleaned up" in development. But it is still good practice to handle every possible error condition properly.
    I don't mind die() for all but the most requent errors. Such as "you are not logged in!". This is not a code-based error, so it obviously needs to be handled without a die(). But you all have given me some ideas about that.

    Thanks

  • #14
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Remember that my code is just an "included" page. Meaning the header, footer, navigation and everything is outside my own little script. This leaves using header:location out. As it would return "headers already sent by...".
    Code:
    // put this in a global file:
    ob_start();
    
    //use this when you want to redirect, wrap it in a function to suit your needs:
    header("Location: $your_url");
    ob_end_flush();


    I don't mind die() for all but the most requent errors. Such as "you are not logged in!". This is not a code-based error, so it obviously needs to be handled without a die(). But you all have given me some ideas about that.
    if user not logged in, redirect to a URL with part of the url they were trying to access URL encoded .. then they can login and be redirected to that URL (but you must make sure it is a valid one somehow or could lead someone to be redirected to somewhere unsafe..)

  • #15
    New Coder
    Join Date
    Feb 2006
    Location
    Arizona, USA
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've been doing some reading about trigger_error, set_error_handler and so forth. Not sure I really want to use my own global error handler. Only because, remember, my script is only in include in someone else's page. So I don't want to suddenly handler all their errors as well.
    My error handling has to be self-contained.

    As for error conditions, this not being my page, I don't have control over it. Some idiot could get in there and delete a file, screw up the database, change a file name; any number of things could cause errors in my code, even if I coded it perfectly in dev.

    Try, catch is a good alternative, but unfortuneatly it is pretty much the same as using big if-then conditions. Think about it

    try {
    some stuff that hopefully doesn't fail, if good, next try
    try {
    next level of code that hopefully doesn't fail
    try {
    possibly many many levels before script is done
    } catch {
    oh man failed 3rd try
    }
    } catch {
    oh man failed 2nd try
    }
    } catch {
    oh dang you had a 1st level error
    }


    As you can see, it can get just as ugly as big if-then conditions.
    I don't know what I'm looking for fellas, i guess I just want it similar to this idea:

    codeline 1
    codeline 2
    codeline 3
    maybefailcondition or GOTO ERRORTHING
    rest of code here
    //
    //any more code here will NOT execute if ERRORTHING is ever called.

    function ERRORTHING {
    echo some errors
    // now need to make sure no other script is executing in my include
    // but that the rest of the page is rendered beyond my include
    }


    I program like Basic and Visual Basic and languages that have a way to just jettison the code anywhere I like. But php always wants to keep executing. And indeed it needs to, but ONLY after my own script, and not within.

    I figure I could also just create a boolean error flag, so if the flag is set, no code executes, but that seems rediculous cause then all my code as to look like

    if (!$errflag) { keep coding }

    I would have to make very small chunks of IF statements for each possible error-creating code. That just gets ugly too.

    Where is my thinking going wrong?
    I have not ridden the possibility of wrapping more stuff into classes, but at this point I cannot go rewrite all my code. But I will do that in any new projects. I'm sure it will make things easier.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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