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.
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
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:
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.
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().