What is the best way to stop the execution of an entire script?

Discussion in 'PHP' started by Imozeb, Apr 22, 2010.

  1. #1
    I have a PHP script and to stop hackers I have put in some safe gaurds like

    PHP code:
    if($var == 'rick' || $var1 == 'bob')
    {
    }
    else
    {
    //execute code here
    }
    Code (markup):
    I want to know if there is a neater way to do this like 'die()' or something to stop the execution of the entire script. (I want to do this because I have alot of variables that need to be used to kill the script and a lot of if statements makes the code hard to understand.)

    So is their a simple PHP command that will stop the execution of the rest of the script?

    Thanks. :)
     
    Imozeb, Apr 22, 2010 IP
  2. molipoli

    molipoli Peon

    Messages:
    5
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    wouldn't a

    return false;

    work?
     
    molipoli, Apr 22, 2010 IP
  3. DEWebDev

    DEWebDev Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could just add an exit(); and that will stop the execution of the entire script.
     
    DEWebDev, Apr 22, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    die() or exit()

    But this is bad coding, your code should'nt depend on such functions to stop functionality, may seem like an easy option, but in the long run you may come accross problems (such as templating issues), as your ending execution of the whole script, when you could do an elseif an echo a error message and avoid these possible future problems.

    Also take alook at the Ternary Operator(s) -> http://www.totallyphp.co.uk/tutorials/using_if_else_ternary_operators.htm
     
    danx10, Apr 22, 2010 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    Instead of checking if positive why not just stop the script if they don't equal the values you want?

    
    if($var != 'rick' || $var1 != 'bob')
    {
         exit();
    }
    
    PHP:
     
    Silver89, Apr 22, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    As has already been said die and exit are a bad way to do this in your circumstances; at the very least use exit(VALUE) where value is non zero otherwise you are saying it was success!!!!! (you can safely ignore comments saying use exit())

    You could just restructure your if statements and you wouldnt need to worry about so much other than a single else. A program I (i bought it to save me time) use checks the User agent against a list and if its NOT in the banned ones it proceeds to the affiliate page, otherwise (its banned) it redirects to the homepage. Simple; but no die/exit involved.
     
    lukeg32, Apr 22, 2010 IP
  7. System 6 Hosting

    System 6 Hosting Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    die() is used in a lot of scripts to prevent backdoor entries etc. The correct entry point defines a global constant then all other scripts verify that constant is set or they die().
     
    System 6 Hosting, Apr 23, 2010 IP